Select inputs (dropdowns) as filters - additional questions
Select inputs (dropdowns) as filters - additional questions
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
The data appears to be sorted as a string in this code:
This thread provides a suggestion to sort the column data as numeric.
Use
column().type()to get the type. If thenumthen use a statement that uses the sort function as described in the thread above. Otherwise use the statement you already have.Kevin
I tried the solution Allan mentioned and it didn't work. First the values passed into the
sort()function foraandbare strings not numeric. Secondreturn a<b;is not correct. Thesort()docs state a numeric value is expected. Usereturn 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