columns().search() question

columns().search() question

ewmantheiewmanthei Posts: 14Questions: 1Answers: 2
edited June 2016 in DataTables 1.10

The following code will find data within both columns 0 and 2. How can I find data within either columns 0 OR 2?

oTable.columns([0,2]).search(loadSearch, true).draw();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Unfortunately there is no API for that in DataTables at the moment. As you are seeing it applies the same filter to both selected columns.

    Being able to define a custom group of columns is something I'm looking into for the next major version of DataTables. The "global" search (i.e. all columns) is then just a special case of that. I have a slight concern about memory consumption (since its an extra array) and performance( since invalidation has another array to handle), but I think it is worth exploring.

    For now you would need a custom plug-in.

    Allan

  • ewmantheiewmanthei Posts: 14Questions: 1Answers: 2
    edited June 2016

    Allan,

    Thanks for the help. Could it be done somehow this way (either currently or in the future)?

    Start with:

    firstArray = oTable.columns([0]).search(loadSearch, true);
    secondArray = oTable.columns([2]).search(loadSearch, true);
    

    then perform something like this:

    finalArray = $.uniqueSort(firstArray.concat(secondArray));
    finalArray.draw();
    

    A potential benefit of this method is being able to preferentially decide which column(s) you want to be given priority in the search results. In my case, I'd like to be able to have column 0 searched first and its results displayed at the top prior to going to column 2.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    In future, as I say, I want to allow grouping of search columns and that will allow what you have above to just automatically work.

    There is currently only the option of using a custom search plug-in to do it.

    Allan

  • ewmantheiewmanthei Posts: 14Questions: 1Answers: 2

    For what it's worth, I resolved this by switching to server side processing and manipulating the data within php. I'm still running from a flat json file so there's no unnecessary db calls, but php allows me to restructure the data object however I want more easily and then send it back.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Cunning - thanks for posting back, and good to hear you have it working the way you need.

    Allan

This discussion has been closed.