Implementing a custom filter with OR columns and optional column selection.

Implementing a custom filter with OR columns and optional column selection.

David_ODavid_O Posts: 3Questions: 1Answers: 0

I have a table that I want users to be able to filter using 2 criteria:

  • They can select rows based on a fixed set of values. Cells in the rows can contain none, one or many of that fixed set.
  • They can also reduce the set of matches by selecting which of a fixed set of columns contain a match.

I've got a simplified test case:

https://live.datatables.net/togaxisi/2/edit?html,js,output

In reality, the searched columns will be hidden and the table will use responsive to wrap additional data, but I don't think either of those affects this.

I think the best way to do this is to:
* use checkboxes on the fixed set of values (Apples ... Dates) to build a regex that can be matched against each target column to give a boolean array saying if each cell in the columns matches.
* use a second set of checkboxes as column selectors and use those to create a matching array that says if a column is selected.
* Combine those using & to get a row selection flag.

I've had a stab at it in the example, but I can't get the filter to apply. I'm pretty sure the filter function isn't running at all. I do very little programming with JS, so have probably done something fundamentally wrong, but I hope the logic is clear.

I also know I haven't handled the special cases where no values or columns are selected.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,368Questions: 26Answers: 5,138
    Answer ✓

    New in Datatables 2 is search.fixed(). This provides a lot of capability plus it replaces the search plugin. I updated an old example to use search.fixed() as an OR search across two columns.
    https://live.datatables.net/vexekozu/1/edit

    Basically the search.fixed() function is initialized then in the checkbox change event handler draw() is called which will update the Datatable display including executing the search.fixed() function.

    Kevin

  • David_ODavid_O Posts: 3Questions: 1Answers: 0

    Many thanks, Kevin. That's exactly the API that I needed.

    I've gone back to my example and worked the logic into that API so that it works as the question originally requested, in case anyone else has a similar multiple column filter to implement.

    https://live.datatables.net/togaxisi/10/edit

  • allanallan Posts: 65,421Questions: 1Answers: 10,864 Site admin

    That's slick - like it! Nice implementation :)

  • David_ODavid_O Posts: 3Questions: 1Answers: 0

    Left some console logging in the modified example by mistake. They're just for show. I did, of course, code the logic first time without making any dumb mistakes that needed row by row console logging to iron out the errors...

Sign In or Register to comment.