Select inputs (dropdowns) as filters - additional questions

Select inputs (dropdowns) as filters - additional questions

HugoHaHugoHa Posts: 6Questions: 2Answers: 0
edited April 1 in Free community support

Hello,

I implemented filtering of columns using select inputs (dropdowns) as shown at https://datatables.net/examples/api/multi_filter_select.html.

There are two things I want to change from the default behavior:

1) The raw data for the column looks like this:

1,2,2,100,4,2,100,300,100,4,1,2,1

The select input (dropdown) generated looks like this:

1
100
2
300
4

How can I sort the entries in this select input (dropdown) by numeric value? The list should look like this:

1
2
4
100
300

2) I want to add 2 additional entries in the select inputs (dropdowns) like this:

less than 100
100 or more

1
2
4
100
300

"< 100" should filter for all entries with the values 1, 2 and 4; ">= 100" should filter for all entries with the values 100 and 300. How can this behaviour be achieved?

Thank you for a helpful hint!
HugoHa

Link to test case: https://datatables.net/examples/api/multi_filter_select.html
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • kthorngrenkthorngren Posts: 22,438Questions: 26Answers: 5,161

    The data appears to be sorted as a string in this code:

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' );
                } );
    

    This thread provides a suggestion to sort the column data as numeric.

    Use column().type() to get the type. If the num then use a statement that uses the sort function as described in the thread above. Otherwise use the statement you already have.

    Kevin

  • kthorngrenkthorngren Posts: 22,438Questions: 26Answers: 5,161

    I tried the solution Allan mentioned and it didn't work. First the values passed into the sort() function for a and b are strings not numeric. Second return a<b; is not correct. The sort() docs state a numeric value is expected. Use return Number(a) - Number(b); instead to get the negative, positive or 0 value expected. For example:
    https://live.datatables.net/ciqoguri/2/edit

    Note that I updated the first four HTML rows with the values of 1, 10, 100 and 2 in the Age column to show the sort() function works correctly.

    Kevin

Sign In or Register to comment.