Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery DataTables. How to get filtered (visible) rows

I have a button that apply filter to jquery datatable

$("#buttonFilter").button().click(function() {
                if (lboxColor.val() != null) {
                    jqTable.fnFilter($("option:selected", lboxColor).text(), 1);
                }
            });

It shows me for example 47 rows from 60. I tried .fnGetData() and fnGetNodes() but it shows me all rows, but not filtered. How could I get 47 rows?

like image 723
Yuri Avatar asked Jun 25 '11 14:06

Yuri


4 Answers

I've been searching for about an hour, for anyone using DataTables 1.10+ if you want to get the filtered (better term: "searched") rows:

var table = $('.table').DataTable({...});

function selectOnlyFiltered(){
   var filteredRows = table.rows({filter: 'applied'});
}
like image 169
Jack Avatar answered Oct 20 '22 18:10

Jack


For datatables 1.9 and later this solution works:

myDataTableHandle = $('#example1').dataTable(...);
...
...
var myFilteredRows = myDataTableHandle._('tr', {"filter":"applied"});

and you won't have to include a separate api plugin. :)

like image 25
RayLoveless Avatar answered Oct 20 '22 18:10

RayLoveless


Just in case you want a collection of nodes (DOM elements) just like fngetNodes(), you can use the '$' instead of the '_' like this table.$('tr', {"filter":"applied"});

the '_' returns a collection of 'TR' (html elements).

like image 39
Bedouin Avatar answered Oct 20 '22 16:10

Bedouin


For those struggling to get data from only one column AND only from visible rows: you can actually combine the "selectors" like this

var total = api
      .column(2, { search: 'applied' }) // <-- only 3rd column && only visible after applied search
      .data()
      .reduce(function (a, b) {
        return floatVal(a) + floatVal(b)
      }, 0)

I also notices several sources stating { page: 'current' } would be the right selector for currently visible rows, but this one actually gives you the rows visible after a possible pagination.

like image 37
David Otto Avatar answered Oct 20 '22 16:10

David Otto