Custom filter reset after sort column

Custom filter reset after sort column

SjoerdvWSjoerdvW Posts: 3Questions: 1Answers: 0

I have a datatable with computer information.
I created a custom filter which is a checkbox to show only the computers with a low diskspace by using the $.fn.dataTable.ext.search API.

This works fine, however, after applying the filter & resorting the columns, the filter is reseted.
How can I solve this?

A example of this problem can be found here: http://jsfiddle.net/4kqL91az/

This question has an accepted answers - jump to answer

Answers

  • MSLtdMSLtd Posts: 56Questions: 5Answers: 4

    Hi @SjoerdvW, the simplest way I can think of for fixing this is by re-applying the filter every time the table draws. You can do this using a function called drawCallback.
    This function is called whenever the table is drawn which I believe includes when you sort by columns. You should check whether or not the check box is checked in this functions, and if that is the case you should run your custom search - Make sure you don't make a recursion doing this! If this doesn't work, or you need more help with this, just give me a shout. :smile:

  • SjoerdvWSjoerdvW Posts: 3Questions: 1Answers: 0

    Hi MSLtd,

    Thnx for youre response.
    I changed my filter to a function, and call it both on checkbox changed & in the callback (example can be found here: http://jsfiddle.net/4kqL91az/11/).

    However: this broke the .change call on the checkbox. Now, if i check it, and sort it by the columns it is applied. I guess it has something to do with a recursion (where you are warning me about), but not sure how to solve this.

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945
    Answer ✓

    The problem is you are pushing the search API, drawing the table, then popping the search API each time the checkbox is changed. This removes it when pother features are used. In this case you can probably just push it once and leave it. Take a look at this updated example:
    http://jsfiddle.net/L2us48ht/1/

    Kevin

  • SjoerdvWSjoerdvW Posts: 3Questions: 1Answers: 0

    great! Thanx for youre help

This discussion has been closed.