简体   繁体   中英

Jquery find function not working when paging allowed in datatables plugin

I successfully used the Datatables before in implementing other cases, but I am having a problem in looping through other rows when I have pagination enabled.

I am using a find function using Jquery to loop through the rows in the table but it only goes through the displayed rows and does not go to the second page of the datatable.

Here is a working fiddle https://jsfiddle.net/czarluc/5cm7rgvy/

This is the function I used.

function getTotalRows(){
  var counter = 0;
    $("#item_table").find('tr.tablerow').each(function (i, el) {
      counter += 1;
    });
  return counter
  }

There are a total of 20 rows but when I execute the function it only returns 5.

Are there any alternative functions or implementations available?

Edit: If I disabled pagination this will work fine, but I just want to see how to make it work with pagination enabled.

It seems I was using the wrong function. I'm putting this up incase anyone encounters the same problem.

To access the rows in the Datatable, use the rows().node() function instead of the Jquery find function then use the foreach function to loop through the rows. Also store the reference datable into a variable then use it to access the rows().node() function.

Here is the correct code.

var table = $("#item_table").DataTable({
    pageLength : 5,
    lengthMenu: [[5, 10, 20, 50], [5, 10, 20, 50]],
    "columnDefs": [ {"className": "text-center", "targets": "_all"} ]
  });

function getTotalRowsv2(){
  var counter = 0;
    $(table.rows().nodes()).each(function(i, el){
      counter += 1;
    });
  return counter;
  }

This function loops all through the rows regardless of pagination. It also returns 20 as intended. Here is the documentation link. https://datatables.net/reference/api/rows().nodes()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM