extension ordering not working in specific scenario - would appreciate insight
extension ordering not working in specific scenario - would appreciate insight
We recently began using datatables in a scenario where the users can edit the text direclty in the cells using input, select tags.
We promptly ran into the issue of sorting cells containing data inside of HTML tags, and used the dataTable.ext.order functions to add methods for sorting these columns.
It worked perfectly initially, on a new page, however, it does not work for any textfields that contain text. It works fine for any non-text, input type="text" fields.
If you go to our fiddle, http://jsfiddle.net/m9o5myyd/ you can see a sample grid with textboxes and/or select items in the table. You will be able to sort the SortOrder and Status columns (a textbox containing numbers and a select tag containing drop down values), but the first column, containing a textbox containing text, will not sort.
The debugger for the "real" page, is here: https://debug.datatables.net/ewezuj
Can anyone provide any insight into what might be happening? Thank you in advance!
Replies
Hi,
Thanks for posting the test case!
This is sort of(*) a bug in DataTables. What is happening is that the type detection in DataTables is being run on:
It detects that as an
html-num
data type, which is done by stripping the HTML and therefore leaving an empty string which is valid for that data format. It does that for all rows, and hence that columns is detected as a valuehtml-num
type.That is a problem because that data type expects numeric data, but your data is actually string based there. Since numeric data is expected, it converts the data to a number, which in this case results in
NaN
. All rows have that value for that column and thus no sorting will occur...The "fix" is to set the
columns.type
to bestring
- or better yet, use the natural sorting plug-in and use that - updated example, as that will correctly sort the numeric data.(*) - I say "sort of" a bug, because it is working as it was designed - there is a bug in the design in that the type detection does not operate on the data returned by the live sorting plug-ins. It should! That is something I need to fix, but it isn't going to be an easy change, so it might be the next major release before I can do that... Thanks for bringing it up though!
Allan
Thats fantastic. Works perfectly. Thank you very much!