Hidden title sorting problem
Hidden title sorting problem
markjo
Posts: 66Questions: 0Answers: 0
I use hidden title sorting.
In my script code, when i only declare "aoColumns", sorting is ok.
Hidden title sorting works.
But when i change options of datatable my code becomes broken.
In the below code i commented out the problematic part.
When i uncomment that part my script becomes broken.
How can i fix it?
Thank you
[code]
jQuery.fn.dataTableExt.oSort['title-numeric-asc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['title-numeric-desc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
$('#myid').dataTable(
{
"aoColumns": [
null,
null,
null,
{ "sType": "title-numeric" },
null,
null,
null,
{ "sType": "title-numeric" },
null,
null,
null
],
// "bPaginate": true,
// "sPaginationType": "full_numbers",
// "bFilter": false,
// "sDom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
// "bStateSave": true,
// "fnDrawCallback": updateTableFuncts(),
}
);
runSomethings();
} );
function runSomethings() {
alert("somethings changed "); }
}
[/code]
In my script code, when i only declare "aoColumns", sorting is ok.
Hidden title sorting works.
But when i change options of datatable my code becomes broken.
In the below code i commented out the problematic part.
When i uncomment that part my script becomes broken.
How can i fix it?
Thank you
[code]
jQuery.fn.dataTableExt.oSort['title-numeric-asc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['title-numeric-desc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
$('#myid').dataTable(
{
"aoColumns": [
null,
null,
null,
{ "sType": "title-numeric" },
null,
null,
null,
{ "sType": "title-numeric" },
null,
null,
null
],
// "bPaginate": true,
// "sPaginationType": "full_numbers",
// "bFilter": false,
// "sDom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
// "bStateSave": true,
// "fnDrawCallback": updateTableFuncts(),
}
);
runSomethings();
} );
function runSomethings() {
alert("somethings changed "); }
}
[/code]
This discussion has been closed.
Replies
Allan
Thank you very much.
What is the error? Can you please suggest the correct code.
I am trying to use title-numeric to sort some data as per the title field using your plugin. I have added { "sType": "title-numeric" } in the header and the corresponding code from http://datatables.net/plug-ins/sorting#how_to_type
I am having the following error "a.match(/title="*(-?[0-9\.]+)/) is null"
My code is similar to http://www.datatables.net/forums/discussion/4595/how-to-use-hidden-sorting-plug-in/p1
Anyways, attached is my code:
[code]
jQuery.fn.dataTableExt.oSort['title-numeric-asc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9]+)/)[1];
var y = b.match(/title="*(-?[0-9]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['title-numeric-desc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9]+)/)[1];
var y = b.match(/title="*(-?[0-9]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
oTable = $('#example').dataTable({
"aoColumns": [
null,
null,
{ "sType": "title-numeric" },
{ "sType": "title-numeric" },
null,
null
],
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaSorting": [ ]
});
});
[/code]
Following are some demo values from my table:
I would really appreciate your help.
Thank You.
Allan
The debugger seems helpful. I looked into the rows of the debugger Datatable and realised that the row seems to have no values, when infact there is data in the html file (not sure why this should happen).
Following is the actual row in the html file
[code]
AAAGACA,MIR-511
[/code]
Following is what you see in the row 1 of the debugger datatable
[code]
["", "", "", "", "", "AAAGACA,MIR-511"]
[/code]
P.S. (once I figure out how) I would eventually use title-numeric to sort the columns 1,2 and 5 also.
Thank You for your help.
If you look at the Firebug / Inspector console, do you see an error being generated? I think that the sorting plug-in will need to be modified to cope with that.
Allan
>
I just realised you were using the title in the TD tag. The sorting functions do not have access to the TD's attributes by default - they string process only the data _inside_ the cell.
What you need to do is use a custom data gatherer for the sort:
http://datatables.net/development/sorting#data_source
http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html
Or put the numeric data inside the cell.
A limitation in the current implementation - sacrificing flexibility for speed...
Allan
[quote]
put the numeric data inside the cell.
[/quote]
This definitely would not work due to html page becoming ugly. Numbers are only for sorting (numbers not to be shown in the cell)
[quote]
use a custom data gatherer for the sort:
[/quote]
This might work. However, with my very limited knowledge of javascript I am not sure about how it can be done for my case. Can you please tell me what changes I need to make?
Your help is greatly appreciated. I can create a new thread if you want to answer this question completely separate from the above ones.
Thanks.