27

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?

2
  • 1
    Question is closed. I used method from plugin for datatables fnGetFilteredData() . datatables.net/plug-ins/api#how_to Commented Jun 25, 2011 at 15:18
  • 4
    If the question is closed, write your own answer and accept it after two days. Commented Jun 26, 2011 at 10:22

8 Answers 8

33

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'});
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that table.rows( {page:'current'} ).data() "sets order=current and search=applied" per datatables.net/reference/type/selector-modifier in case you wanted to just grab the rows that are visible for the page you're on.
20

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. :)

3 Comments

Does not work for me :( It only returns values on the current visible page. E.g. my table info line shows "Showing 1 to 10 of 45,048 entries (filtered from 45,248 total entries)" and the {"filter":"applied"} code only gives me 10 values.
I see that the fnRecordsDisplay() does give the correct value of 45,048 so there is a way to get all the filtered nodes/values. Does anyone know the REAL answer?
FYI - Found the problem - there are no TR nodes for rows on non-visible pages when bDeferRender is turned on. See similar issue here
7

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).

Comments

4

In current versions selector-modifier uses a slightly different set of properties.

var table = $('#example').DataTable();
var rows = table.rows({"search" : "applied"});

And you can iterate over the cell data like this:

table.rows({"search":"applied" }).every( function () {
    var data = this.data();
});

Some helpful links:

Comments

3

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.

Comments

0

For those who are interested, this is a concrete usecase.

/**
 * Select all the elements of the datatable which match the current user
 * search (or all if no search).
 */
function dtable_selectAll()
{
    idTable = 'myDataTable';
    var rows = $('#' + idTable).dataTable()
            .$('tr', {"filter":"applied"});
    var oTT = TableTools.fnGetInstance(idTable);
    $(rows).each(function (index, el){
        oTT.fnSelect(el);
    })
}

Hope it helps.

Comments

-1

Just for completeness, and as Yuri mentioned, there are "plugins" that will provide the filtered TR Nodes or data.

To use these plugins, you will need to paste the code from those links into your script file.

1 Comment

I can't seem find the plugin function in the API page, had it been removed?
-1

like if work for you

var data = []
table.rows({"search":"applied"}).every(()=>data.push(this.data()))
console.log(data)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.