Table sorting numbers with commas
Table sorting numbers with commas
Hi,
Table sorting doesnt work
Below is an example of how the numbers sort in one of the columns. How do I get the table to sort the data properly? I am using WordPress's wp-table reloaded plugin which uses DataTable coding.
1
1,861
119,825
126,631
140
163
2049
2254
etc
Is there DataTables coding that exists that I can extend to the WordPress wp-table reloaded plugin?
Thanks,
-BrianP
Table sorting doesnt work
Below is an example of how the numbers sort in one of the columns. How do I get the table to sort the data properly? I am using WordPress's wp-table reloaded plugin which uses DataTable coding.
1
1,861
119,825
126,631
140
163
2049
2254
etc
Is there DataTables coding that exists that I can extend to the WordPress wp-table reloaded plugin?
Thanks,
-BrianP
This discussion has been closed.
Replies
http://datatables.net/plug-ins/sorting
and removed the /* Remove the currency sign */ sections. Using this slightly modified plugin now sorts this type of data just fine.
I'm not using the wp-table plugin so I can't comment on that. I am using ThemeRoller and TableTools though.
Where do I copy this code [as per your instructions above]?
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
02. /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
03. var x = a == "-" ? 0 : a.replace( /,/g, "" );
04. var y = b == "-" ? 0 : b.replace( /,/g, "" );
05.
10. /* Parse and return */
11. x = parseFloat( x );
12. y = parseFloat( y );
13. return x - y;
14.};
15.
16.jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
17. /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
18. var x = a == "-" ? 0 : a.replace( /,/g, "" );
19. var y = b == "-" ? 0 : b.replace( /,/g, "" );
20.
24.
25. /* Parse and return */
26. x = parseFloat( x );
27. y = parseFloat( y );
28. return y - x;
29.};
The way to make use of sorting plug-ins such as the currency plug-in is described on the sorting plug-in page: http://datatables.net/plug-ins/sorting . Basically you just include the sorting plug-in code after your script tag for jQuery and DataTables, but before you actually make the $().dataTable() call.
Here is an example of how to do it with the numeric comma plug-in: http://datatables.net/examples/plug-ins/sorting_sType.html
Regards,
Allan
Looks like the solution is to modify an existing plugin for now, as the first commenter suggested.