Sorting a column resets my custom filter
Sorting a column resets my custom filter
I created a custom filter on a dataTable, like this:
[code] $('#pkfilter_type').change( function() {
oTable.fnFilter( this.value, 2 );
} );[/code]
It's a dropdown ("select" element) that filters a column from a few choices. However, when I click a header cell to sort by that column, it cancels the filter and shows every row of the table again. The dropdown still shows the item I filtered on, as if it was still applied.
Is this supposed to happen? How do I keep the filter on when other things in the table change?
If it's useful, here's the bit of my code that initialises the DataTable:
[code] oTable = $("#pokedex").dataTable( {
"aoColumns": [
{ "sType": "html", "bSearchable": false },
null,
{ "bSortable": false, "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
],
"bPaginate": false,
"bAutoWidth": false,
"bInfo": false,
"bProcessing": true, // display processing banner
"aaSorting": [], // prevent default sort
"asStripClasses": [], // remove odd/even classes
"bFilter": false,
} );[/code]
[code] $('#pkfilter_type').change( function() {
oTable.fnFilter( this.value, 2 );
} );[/code]
It's a dropdown ("select" element) that filters a column from a few choices. However, when I click a header cell to sort by that column, it cancels the filter and shows every row of the table again. The dropdown still shows the item I filtered on, as if it was still applied.
Is this supposed to happen? How do I keep the filter on when other things in the table change?
If it's useful, here's the bit of my code that initialises the DataTable:
[code] oTable = $("#pokedex").dataTable( {
"aoColumns": [
{ "sType": "html", "bSearchable": false },
null,
{ "bSortable": false, "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
{ "asSorting": ["desc","asc"], "bSearchable": false },
],
"bPaginate": false,
"bAutoWidth": false,
"bInfo": false,
"bProcessing": true, // display processing banner
"aaSorting": [], // prevent default sort
"asStripClasses": [], // remove odd/even classes
"bFilter": false,
} );[/code]
This discussion has been closed.
Replies
[code]"bFilter": false,[/code]
With that gone, everything works as expected. This is a bug, right? Is there a way around it? I just want my custom filters and want to hide the default one.
The way to keep filtering enabled, is leave bFilter in its default state, and simply remove the display of the filter box from sDom: http://datatables.net/usage/options#sDom
Allan
"Enable or disable filtering of data. Filtering in DataTables is "smart" in that it allows the end user to input multiple words (space separated) and will match a row containing those words, even if not in the order that was specified (this allow matching across multiple columns). Note that if you wish to use filtering in DataTables this must remain 'true' - to remove the default filtering input box and retain filtering abilities, please use sDom."
Allan
Well filtering itself actually DOES work, that's why it felt like a bug. It's only after trying to sort the table that the filtering stops working.
I'm also having a problem with the sDom method. I have this code:
[code]"sDom": '<"custom-filter"f>rt',[/code]
By my understanding, this should insert the filter into a div with class "custom-filter". However, I have a div with that class but DataTables instead adds a separate div with the class, rather than injecting it into the existing div. Maybe that's the intended behaviour. Is there a way to add the standard components to elements that are away from the table?
With your sDom code there - the filter should be inserted into the custom-filter div, but it also has it's own div (which is just part of the code), so you should get something like div.custom-filter > div.dataTables_filter > input (etc).
There is no method to put elements into the DOM in any position - just those available with sDom. However, it is trivial to allow DataTables to create its elements and then move them to where ever you want in the DOM using appendChild (etc).
Regards,
Allan