jQuery Datatables - retrieving information from other pages

痴心易碎 提交于 2021-02-08 05:09:51

问题


I have got a problem with getting information from jQuery datatable. Here is the table:

table

I would like to get information stored in the table. I have tried to do this by this:

var languages=[];
var people=[];

$("select[name='languageID']").each(function(){
    languages.push( $(this).val() );
});

$("input:hidden[name='personID']").each(function(){
    people.push( $(this).val() );
});

but it is getting values from current chosen page. If I run this in the situation like on the screenshot, only values from page 2 would be pushed to the arrays. I have tried to check where values from page 1 are stored, but I could not find anything. I also looked for it on jQuery datatables homepage

Could anyone help?

Regards

EDIT:

so I should do something like this:

table.column( 0 ).cache( 'search' ).each( function ( d ) { /*action here*/ } ); 

?

But what should be in action?

When I debug table.column( 2 ).cache( 'search' ); I have got this:

[   choose language ", " choose language ", " choose language ", " choose language ", " choose language ", " choose language " ]

It is first option in "Chosen language" select. I think, that I need something, which would return html code from each cell in column, so that I can analyse it later.


回答1:


This one turns out harder than expected. At first, I try using the datatable data() API, but that returns the dropdown lists as HTML strings, which are not very useful. Then I come across this article on how datatable deals with form inputs. The trick is to recognize that your datatable object can retrieve nodes from within the document regardless of paging.

A live demo can be found on my jsfiddle.

EDIT: As per discussion with OP, the following codes can be utilized instead of relying on embedded column selectors:

table.$('select[name="languageID"]').each(function(index, value{
    languages.push($(value).val());
});



回答2:


In the legacy api, fnGetData should return what you need.

$(document).ready(function() {
  oTable = $('#example').dataTable();

  oTable.$('#myButton').click( function () {
    var data = oTable.fnGetData( this );
    // ... do something with the array / object of data for the row
  } );
});

In the new api (v1.10+), you'll probably want column().cache().

http://datatables.net/reference/api/column().cache()




回答3:


In my case, I should modify my code to:

var languages=[];
var people=[];

table
  .column(0)
  .nodes()
  .each(function(a){
    people.push( $(a).find("input:hidden[name='personID']").val() );
});

table
  .column(2)
  .nodes()
  .each(function(a){
    languages.push( $(a).find("select[name='languageID']").val() );
});  


来源:https://stackoverflow.com/questions/25168748/jquery-datatables-retrieving-information-from-other-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!