20

It would be great if someone help me on the issue.

I am just trying to get the filtered result set from the Datatable.

Below is my code.

var filtered_row_data = $('#example').DataTable().column(1).search('186').data().unique().sort();

 console.log(JSON.stringify(filtered_row_data));

It just returns all the rows instead of filtered values.

I am using latest stable version of Datatable.

Can anyone please help on this?

Raja
  • 3,477
  • 12
  • 47
  • 89

2 Answers2

58

see dataTables selector-modifiers. You are looking for {filter : 'applied'} :

table.on('search.dt', function() {
    //number of filtered rows
    console.log(table.rows( { filter : 'applied'} ).nodes().length);
    //filtered rows data as arrays
    console.log(table.rows( { filter : 'applied'} ).data());                                  
})  

demo -> http://jsfiddle.net/h4wrmfx3/

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • its work but when i use it twice!, strange, when i hit search button and console it, its show me 10 record but when i hit search again its show me 3 (which is correct no). can you please help to fix this? – Rohit Sep 25 '18 at 11:55
  • @Rohit, can you reproduce this in a fiddle? Tried the answers fiddle above, it seems to work as expected. – davidkonrad Sep 25 '18 at 12:00
  • as per your suggestion this is my fiddle https://jsfiddle.net/RohitSavaliya2010/sz7w5uvr/1/ can you please check it? – Rohit Sep 25 '18 at 14:38
  • @Rohit, well, it does not work, just some code out of context :) You can grab the above http://jsfiddle.net/h4wrmfx3/ and add / modify then update or fork. – davidkonrad Sep 25 '18 at 14:45
  • because of not including ajax file its not work in js fiddle , rather then my code is work perfect & search is also work good but when i console console.log(srch_table.rows( { filter : 'applied'} ).nodes().length); is first show me 10 record which is default paging and when i hit search button second time its return me correct value of search count. hope u understand. – Rohit Sep 25 '18 at 15:03
  • @Rohit, but you have no search, you have actually turned filtering off. What you do is that you reload the entire dataset. You'll never a "applied" value which differs from total number of rows. – davidkonrad Sep 25 '18 at 15:14
  • @davidkonrad can I ask you, i was searching this for hours i was looking this documentation (https://datatables.net/reference/api/rows()) but i couldnt find any other docs that specify this, i was wondering where i can find this extra details :) – Manza Sep 10 '20 at 00:42
  • 1
    @Manza follow the link to https://datatables.net/reference/type/selector-modifier (the same as in the answer) – davidkonrad Sep 10 '20 at 05:45
0

if you are using server side filtering / searching, this is the only solution i found and it works: xhr event

$('#yourTable').on('xhr.dt', function ( e, settings, json, xhr ) {
        //the new data is here json.data
        console.log(json.data);
});
Adel Mourad
  • 1,351
  • 16
  • 13