How to loop through all rows in DataTables jQuery?

前端 未结 4 1927
-上瘾入骨i
-上瘾入骨i 2020-12-25 12:54

I am using jquery plugin DataTables for building nice table

  var table = $(\'#example\').DataTable({
    \"data\": source
});
相关标签:
4条回答
  • 2020-12-25 13:19

    If you are using the legacy DataTables then you can get all the rows even the paginated ones, as shown below...

    table.fnGetNodes(); // table is the datatables object.
    

    So we can loop through the rows by using .each() method provided by jQuery.

    jQuery(table.fnGetNodes()).each(function () {
    // You can use `jQuery(this).` to access each row, and process it further.            
    });
    
    0 讨论(0)
  • 2020-12-25 13:32

    Datatables have an iterator for each row rows().every() with this referring to the context of the current row being iterated.

    tableName.rows().every(function(){
        console.log(this.data());
    });
    
    0 讨论(0)
  • 2020-12-25 13:37

    for example, this data has three fields UserID, UserName and isActive and we want to show only active users The following code will return all the rows.

    var data = $('#myDataTable').DataTable().rows.data();
    

    We will print only active users

    data.each(function (value, index) {
      if (value.isActive)
      {
         console.log(value.UserID);
         console.log(value.UserName);
      }
    });
    
    0 讨论(0)
  • 2020-12-25 13:38

    I finally found:

     var data = table.rows().data();
     data.each(function (value, index) {
         console.log(`For index ${index}, data value is ${value}`);
     });
    
    0 讨论(0)
提交回复
热议问题