admin管理员组

文章数量:1323707

I have got a problem with getting information from jQuery datatable. Here is the 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.

I have got a problem with getting information from jQuery datatable. Here is the 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.

Share Improve this question edited Aug 6, 2014 at 21:17 Hladeo asked Aug 6, 2014 at 19:37 HladeoHladeo 5771 gold badge7 silver badges16 bronze badges 1
  • What version? The api has changed recently. – isherwood Commented Aug 6, 2014 at 19:56
Add a ment  | 

4 Answers 4

Reset to default 5

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 e 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());
});

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/reference/api/column().cache()

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() );
});  

As @ivan.sim says, it's not so easy to get the full table data, although I finally found a solution which you can use to get values (including hidden inputs etc) from the full table including pages that are not displayed:

table.rows().every(function (rowIdx, tableLoop, rowLoop) {
    var data = this.node();
    console.log($(data).find('input').prop('checked'));
    console.log($(data).find('input[name=someField]').val());
});

本文标签: javascriptjQuery Datatablesretrieving information from other pagesStack Overflow