Reputation: 257
So I am using the Datatables
plugin (http://datatables.net/) to sort data within my tables.
The data I have stored includes numbers (22.34), currency ($223,400) and formatted numbers (233,623). There is a section on the website for sorting plugins (http://datatables.net/plug-ins/sorting).
I have spent the last 2 hours attempting to get this to work but keep getting errors no matter what I try.
Here is my code:
Included is the following script:
<script src="assets/js/dataTables.plugins.js"></script>
Which contains the following:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},
"formatted-num-asc": function ( a, b ) {
return a - b;
},
"formatted-num-desc": function ( a, b ) {
return b - a;
}
} );
Then the main code:
<script>
$(document).ready(function() {
var oTable = $('#sample_1').dataTable( {
"sDom": "<'row-fluid'<'span4'l><'span4 tbl_time_frame'><'span4'f>r>t<'row-fluid'<'span4'i><'span4'><'span4'p>>",
"sPaginationType": "bootstrap",
"aoColumns": [
{ "sType": "numeric" },
null,
{ "sType": "formatted-num"},
{ "sType": "numeric"},
null,
null,
null,
null,
null
],
"oLanguage": {
"sLengthMenu": "_MENU_ records per page",
"oPaginate": {
"sPrevious": "Prev",
"sNext": "Next"
}
},
"fnInitComplete": jQuery('.tooltips').tooltip()
});
});
I have the following error on page load:
Uncaught TypeError: Cannot read property 'oSort' of undefined
Then when I click on the third row I get the following errors alternately:
Uncaught TypeError: Property 'formatted-num-asc' of object #<Object> is not a function jquery.dataTables.js:4038
Uncaught TypeError: Property 'formatted-num-desc' of object #<Object> is not a function
Can anyone help?
Cheers
Upvotes: 2
Views: 5045
Reputation: 23
I just ran across this old post when trying to solve the same problem when using the naturalSort.js with dataTables.js. What worked for me was moving the naturalSort.js after the dataTables.js. Simple change, but fixed the problem.
Upvotes: 0
Reputation: 257
The problem was to do with the order in which the files were included. I changed this and it solved the issue.
Upvotes: 1