Bug: Sort number column, and sType
Bug: Sort number column, and sType
cu8164kp
Posts: 6Questions: 0Answers: 0
I'm sorting my columns and my numeric column gets sorted by first digit like so
1
12
130
2
21
22
3
4
5
So i thought to try the aoColumns "sType" and set the column to be numeric. This worked for the default sort, but with that option set when I click the sort arrow it doesn't do anything. Any column I specify with "aoColumns" breaks sorting for that specific column. It didn't matter if it was Date, or numeric if I set the aoColumn sorting breaks for that specified column.
Thanks for any help
I have same problem with 1.4.3 and 1.5beta7.
Thanks for any help.
My example settings:
$(document).ready(function() { $('#table1').dataTable({
"aaSorting": [[ 0, "desc" ]],
"bPaginate": true,
"bFilter": true,
"iDisplayLength": 50,
"aoColumns": [
{ "sType": 'numeric' },
null,
{ "sType": 'date' },
{ "sType": 'numeric' },
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
]
});} );
1
12
130
2
21
22
3
4
5
So i thought to try the aoColumns "sType" and set the column to be numeric. This worked for the default sort, but with that option set when I click the sort arrow it doesn't do anything. Any column I specify with "aoColumns" breaks sorting for that specific column. It didn't matter if it was Date, or numeric if I set the aoColumn sorting breaks for that specified column.
Thanks for any help
I have same problem with 1.4.3 and 1.5beta7.
Thanks for any help.
My example settings:
$(document).ready(function() { $('#table1').dataTable({
"aaSorting": [[ 0, "desc" ]],
"bPaginate": true,
"bFilter": true,
"iDisplayLength": 50,
"aoColumns": [
{ "sType": 'numeric' },
null,
{ "sType": 'date' },
{ "sType": 'numeric' },
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
]
});} );
This discussion has been closed.
Replies
If DataTables is not detecting the numeric type, then it must be because you have non-numeric data somewhere in that column. This might be white-space, or some letters. As you will be able to see from my zero-config example numeric data is correctly detected and sorted: http://datatables.net/examples/example_zero_config.html (fourth column).
Regards,
Allan
{{ object.ticket.id }}
Switching to the following fixed the issue:
{{ object.ticket.id }}
If I remove the tags it sorts correctly, but that takes away from some of the functionality. If I can keep my it allows me to link to the document. Is there any other way I can set a link without messing up the sort type of a column?
Thanks,
Okay got it - thanks for the information! Yes, the HTML would cause DataTables to treat the data as a string, hence the odd sorting, so what you can do is you make use of the "Numbers with HTML" sorting plug-in: http://datatables.net/plug-ins#sorting_numbers_html
Simply include this code (just after DataTables) and then set the sType for your column to "num-html" and it would work as you would expect!
Hope this helps!
Allan
How did u get the ur functionality working, even i.m facing the same problem if anchor tabs are present.
//sets up numeric sorting of links
jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
};
what do you mean by
" include this code (just after DataTables) and then set the sType for your column to "num-html" "
how to do that?
Allan
In my data table, i'm showing a column that should do a numeric sort in descending order. But what i'm getting is sth like this. Its showing in descending order, but when no of digits changes its doing like this.
521003
521002
1000004
1000003
1000002
1000001
I also have some blank values in this column. Can anyone suggest what can i do so that plugin does numeric search properly.
in the plugin i also added: aoColumns":[{"sType": "num-html"}] as it's the first column to be sorted.
By doin this css and pagination breaks for the plugin.
When i remove the aoColumns and add the below functions, it shows the result correctly but the sorting function on column header is screwd for that column.
"numeric-comma-asc": function(a,b) {alert('numeric-comma-asc');
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
"numeric-comma-desc": function(a,b) {alert('numeric-comma-desc');
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
,
Kindly provide your valuable inputs.
Thanks
PVerma.
Allan
Sorting is being done at the server - not the client. So there must be something going wrong in the server-side script. I'd suggest looking into that.
I should also say that since the sorting is being done at the server, sType has no effect - that is a client-side only parameter.
Allan
the regular expression in both of the sorting functions is only replacing one occurrence of the comma, change every replace ("a" and "b") to something like this
[code]
a.replace( /,/g, "" );
[/code]
the g at the end is for a global replacement, I had the same issue as you and this fixed mine.
Hope this helps
This returns the following error: Uncaught TypeError: Property 'num-html-asc' of object # is not a function.
I know a previous post mentions that 'num-html-asc' and doesn't work but does this mean that reordering the column by clicking on the header is not possible with this plugin?
Many thanks
http://datatables.net/plug-ins/type-detection
and the sort plugin:
http://datatables.net/plug-ins/sorting