Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear datatable filters?

Tags:

datatables

Im using custom filtering for my datatable using the method:

$.fn.dataTableExt.afnFiltering.push("custom filter function");

This function adds a filter to my datatable.

The problem is that when I use ajax to create an other datatable object, this filter persists and is applied to this other table that should have nothing to do with this filter. How do I clear the filter or bind it to the first datatable only?

like image 723
The D Merged Avatar asked Aug 04 '13 12:08

The D Merged


People also ask

How do I clear a search box in Datatable?

To reset a search filter just call search() again with an empty string. Similarly you can clear the value of an input by setting its value to an empty string. If you implement this functionality in page navigation then textbox will be cleared. But you are not able to show all records in the gridview.


2 Answers

If you are going to use the 1.10+ version of the datatable in the future, the use of the search plug-in document is shown below:

  • Search plug-in development

To reset the filter for version 1.10+, simply add any of the following;

  • $.fn.dataTable.ext.search = [];
  • $.fn.dataTable.ext.search.pop();

after this blocks you can add;

  • table.draw();
like image 27
omernaci Avatar answered Nov 09 '22 07:11

omernaci


if you make a push on $.fn.dataTableExt.afnFiltering, it means it's an array. So when you receive your data, you can remove the filter reference in this array by using :

delete  $.fn.dataTableExt.afnFiltering[index or key];

this method will set the element to undefined

or by using the splice method in javascript.

$.fn.dataTableExt.afnFiltering.splice(index,1);

this method will remove the element from the array. so

var index = $.fn.dataTableExt.afnFiltering.indexOf("custom filter function");
 $.fn.dataTableExt.afnFiltering.splice(index,1);

should resolve your problem (you can have precision here Javascript - remove an array item by value as indexOf is not supported by IE<9)

like image 88
scraaappy Avatar answered Nov 09 '22 07:11

scraaappy