Using SearchBuilder to filter on checkboxes

Using SearchBuilder to filter on checkboxes

ajamesajames Posts: 5Questions: 2Answers: 0

Link to test case:

http://live.datatables.net/kehodetu/1/edit

Description of problem:

In the above demo the first column of the table is a prepopulated list of check boxes. I would like to use SearchBuilder to filter based on whether a row is checked or not.

I use orthogonal data processing to convert a checked box to the filter strings "selected" and "not selected". I then try to configure SearchBuilder to also use this same orthogonal data.

Problem: When I try to build a filter for the check boxes, I get no data displayed. There are no console errors.

Steps to reproduce, using the above test case:

1) Add condition.

2) Data = "Selected"

3) Condition = "Equals"

4) Value = "selected".

Expected outcome: one matching record found (Garrett Winters).

Actual outcome: no matching records found.

(Just to add: I realize that even if this worked, it would not be a useful solution the way it is currently written, because changes to check boxes made by users would not be visible to DataTables. But I am learning about SearchBuilder - so, one step at a time).

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yep, as you say, that wouldn't work as live DOM elements are not something that SearchBuilder considers. This example here may be of interest, as it demonstrates orthogonal data.

    Colin

  • ajamesajames Posts: 5Questions: 2Answers: 0

    In my example and in the "steps to reproduce", there are no live DOM events. The checkbox is pre-populated. The user does not click any checkboxes.

  • ajamesajames Posts: 5Questions: 2Answers: 0

    I have a work-around which is to change the source data for my first column to "true" and "false" from its current form (which is a string representing the checkbox HTML). I am not seeing why my original approach does not work, though.

    It would be great to understand that, if possible.

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948
    Answer ✓

    Your first test case is close. The problem is that Datatables automatic type detection for the checkbox column is setting it to html-num instead of string. You can set the type by changing your render function like this:

          render: function ( data, type, row, meta ) {
            if (type === 'filter' || type === 'type' ) { // || type === 'sort'
    

    http://live.datatables.net/kehodetu/4/edit

    Or you can use columns.type to set the type, for example: type: 'string',

    Kevin

  • ajamesajames Posts: 5Questions: 2Answers: 0

    Not only did you solve my problem, but now I have a use-case for the previously mysterious type === 'type'. Thank you so much!

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    previously mysterious type === 'type'

    Yes, that way Datatables will type the column based on the filter orthogonal data. Took me awhile to understand that feature :smile:

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Nice, that works well!

    Colin

This discussion has been closed.