1
votes

I am currently working on a page for a web app that displays member data in a jQuery DataTable. I am building a custom plugin for the DataTable that allows for a wide range of filtering per table column.

My current task is to be able to retrieve filtered data from the DataTable, although not updating that data on the table. I know already it is possible to retrieve all filtered data by doing:

var data = $table.dataTable().$('tr', { filter: 'applied' });

This gets data for any text in the search box and any filters applied to columns. I am also aware this call gets the jQuery selectors of cells. That's what I need. But I need more...

My questions are:

  1. Can I get data by only applying what's in the search box, without any other current filters applied to columns? I tried:

    var data = $table.dataTable().$('tr', { search: 'applied' });

But that returns the same as { filter : 'applied' }.

  1. Can I target specific columns for getting data, such as:

    var data = $table.dataTable().$('tr', { filter: 'applied', columns: [1, 2, 5] });

  2. Can I target specific columns AND what's in the search box?

My plugin needs to able to keep track of data with various combinations of column/search filters applied.

1

1 Answers

3
votes

For DataTables 1.9

There is a fnFilter() API method but it applies filtering to the table which is not what you want.

Alternatively you may want to use fnGetData() to get the data for the whole table and filter it yourself.

For DataTables 1.10

There is a search() API method but it applies filtering to the table when used with draw() which is not what you want.

Also there is filter() API method. It is not exactly what you're looking for but very close, however you would need to perform the searching yourself.

You can retrieve the content of the search box as follows:

var searchVal = $('.dataTables_filter input', $table).val();

Then you would need to do the searching yourself, shown below is a simplistic approach which may not match how DataTables perform the search internally.

To search first and second column only, specify [0,1] to columns() method. If no parameters are specified, all columns will be searched instead.

var filteredData = $table.DataTable()
   .columns([0, 1])
   .data()
   .eq( 0 )
   .filter( function ( value, index ) {
       returrn (value.search(new RegExp(searchVal, "i")) !== -1)
          ? true : false;
   } );

According to the manual, variable filteredData would contain a new API instance with the values from the result set which passed the test in the callback.

To retrieve data for all or selected columns, use columns().data().