Custom search function in conflict with global search

Custom search function in conflict with global search

hiwkeviqihiwkeviqi Posts: 2Questions: 1Answers: 0
edited June 19 in Free community support

Link to test case: https://jsfiddle.net/w7rztLy5/
Description of problem:
I'm updating the code for a DataTable on my website which uses a custom search function. Previously, the code used DataTable.ext.search.push() and I would like to now use search().
In the test case, you will find a minimal example of both cases. The commented "initComplete" is the old code and the uncommented one is the new code.
With DataTable.ext.search.push(), I could use a custom function to filter the table and the user could then use the global search to further reduce the dataset.
With the new search api, my search function works fine but as soon as the user triggers a global search by clicking on the search field, all rows reappear and my custom search function is never called again even if I use table.draw().
I found nothing about the impact of the global search on this api in the search manual or the search api. I'm expecting the previously observed behavior to remain with this API.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,594Questions: 1Answers: 10,683 Site admin
    Answer ✓

    Hi,

    You could still use DataTable.ext.search.push(), but it is recommended you don't. The problem with it is that it is applied to all DataTables on the page, so you need extra logic if you want to limit it to just one.

    Instead, if you want to allow the regular global search and also a custom search function at the same time, use the "Fixed search" in the manual page you linked to. That allows you to add multiple search terms, keyed on the first parameter - e.g.:

    table.search.fixed('mySearchKey', (search) => {
      // do search logic
      return true; // or false
    });
    

    See search.fixed() for full details on that method. You can also use column().fixed.search() if you want to layer search terms for individual columns.

    Fixed search is what I use for ColumnControl.

    Updated jsfiddle using a fixed search term.

    Allan

  • hiwkeviqihiwkeviqi Posts: 2Questions: 1Answers: 0

    Thank you allan, sorry I missed this part.

Sign In or Register to comment.