(tip)Custom filters and multiple tables

(tip)Custom filters and multiple tables

crasxcrasx Posts: 3Questions: 0Answers: 0
edited June 2011 in General
Hi,
I recently hit a wall when working with datatables and needed a custom filter for a table based on a select value. This is simple using the example at http://datatables.net/examples/plug-ins/range_filtering.html
However I had a problem because I had multiple datatables on one page and only wanted the filter on one. I am not sure if there is an easy way to do this... maybe Alan knows?
However I found a workaround using the oSettings object that is passed to the filter function. The oSetting object has all of the datatable settings as well as the init parameters- even the ones that aren't part of datatables.
So, you can use this to your advantage by supplying a flag that the method can check.
IE:
[code]
$.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) {
if(oSettings.oInit.bCustomFilter){
console.log("YES!");// do whatever and return true or false
}else //return true because we don't want it filtered out
return true;
});

$('#table').dataTable({ "bCustomFilter":true});
[/code]

Is this logic correct Alan?

Thanks for a great plugin, I have been using it a lot for a project and will donate as soon as my bank account doesnt read $0 =]

Replies

  • allanallan Posts: 61,758Questions: 1Answers: 10,111 Site admin
    That's a good solution - I like it! :-). The way I've done it in the past is to have the filtering function check that the value of oSettings.nTable is a reference to the table you want to do range filtering on - but this works just as nicely.

    This I'm afraid is a limitation of the current DataTables code - custom filters are applied to all tables. This is something that I'll be looking to address in 2.x when I get there - until then a work around such as the one you propose is required.

    Regards,
    Allan
This discussion has been closed.