Handling custom sorting in a dynamically generated table (variable column count)
Handling custom sorting in a dynamically generated table (variable column count)
a.quackenbos
Posts: 4Questions: 0Answers: 0
I'm not exactly a javascript master, so I figure this is the best place to ask about this. I'm generating a table with PHP that has a variable amount of data in it - the column count could change based on the parameters set by the user, and the types of data in the columns is also variable - some string, some floats, some regular numbers. None of the columns holding currency or percents are sorting right, obviously due to the $ and % symbols.
Considering that I have no real way of telling which columns will hold what (without a very brute-force-ish algorithm, which is what I'm trying to avoid), is there a way to handle this cleanly? Perhaps a way to dynamically detect types and assign a different sorting function to said columns?
Considering that I have no real way of telling which columns will hold what (without a very brute-force-ish algorithm, which is what I'm trying to avoid), is there a way to handle this cleanly? Perhaps a way to dynamically detect types and assign a different sorting function to said columns?
This discussion has been closed.
Replies
There most certainly is a way to do this - it's DataTables type detection plug-in :-) Have a look at this page: http://datatables.net/plug-ins#type - there is even a plug-in which should do the trick for you perfectly! Use this in combination with the currency sorting plug-in and that's that!
Demo: http://datatables.net/examples/example_sorting_plugin.html
Regards,
Allan
If they are being sorted as strings, then DataTables must be picking up some non-numeric character somewhere in that column - white space for example can cause this issue. If you make sure that there is only numeric information in the column, there should be no problem with it sorting correctly :-)
Regards,
Allan