[Solved] Sorting a table column
[Solved] Sorting a table column
I have a size column in the table which shows the size of the file with unit (e.g. 1KB, 10MB, 1GB etc.). Since unit part is string whole size value is input to table as the string so when the sorting is done it do String sorting.
For example for it has sorted form of like this: 5KB 500MB 6KB which is not correct. So I am wondering what will be the solution to the problem? Is there any parameter setting that can be done to solve this issue?
Thanks in advance.
For example for it has sorted form of like this: 5KB 500MB 6KB which is not correct. So I am wondering what will be the solution to the problem? Is there any parameter setting that can be done to solve this issue?
Thanks in advance.
This discussion has been closed.
Replies
1. Write a sorting plug-in which will convert GB (etc) to absolute units
2. Use the hidden title numeric sorting plug-in to sort: http://datatables.net/plug-ins/sorting#hidden_title
Allan
I solved by adding following sorting-plugins and data type
Sorting Plugins
[code]
jQuery.fn.dataTableExt.oSort['file-size-asc'] = function(a,b) {
var x = a.substring(0,a.length - 2);
var y = b.substring(0,b.length - 2);
var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1));
var y_unit = (b.substring(b.length - 2, b.length) == "MB" ? 1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1));
x = parseInt( x * x_unit );
y = parseInt( y * y_unit );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['file-size-desc'] = function(a,b) {
var x = a.substring(0,a.length - 2);
var y = b.substring(0,b.length - 2);
var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1));
var y_unit = (b.substring(b.length - 2, b.length) == "MB" ? 1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1));
x = parseInt( x * x_unit);
y = parseInt( y * y_unit);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
New data type
[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
var sValidChars = "0123456789";
var Char;
/* Check the numeric part */
for ( i=0 ; i<(sData.length - 3) ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
}
/* Check for size unit KB, MB or GB */
if ( sData.endsWith("KB") || sData.endsWith("MB") || sData.endsWith("GB") )
{
return 'size';
}
return null;
}
);
[/code]
Regards,
Allan
But I have one problem with display if I add this plugin as follow. My table doesn't have sorting or any other feature it just became plain table.
[code]
$(document).ready(function() {
$('#documents').dataTable({
"sDom": 'T<"clear">lfrtip',
"aoColumns": [
null,
null,
{ "sType": "file-size" },
null,
null
]
});
});
[/code]
What I am doing wrong?
1. There is a javascript error somewhere (open the Firebug console - it will say)
2. The #documents selector isn't right - which doesn't seem very likely :-)
Allan
Thanks
[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
var sValidChars = "0123456789";
var Char;
/* Check the numeric part */
for ( i=0 ; i<(sData.length - 3) ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
}
/* Check for size unit KB, MB or GB */
if ( sData.substring(sData.length - 2, sData.length) == "KB"
|| sData.substring(sData.length - 2, sData.length) == "MB"
|| sData.substring(sData.length - 2, sData.length) == "GB" )
{
return 'size';
}
return null;
}
);
[/code]
Only change is use of substring() function.
Now everything working fine. I think we can post on the plug-ins page.
Thanks.