custom sorting: Uncaught TypeError: Cannot read property 'oSort' of undefined

custom sorting: Uncaught TypeError: Cannot read property 'oSort' of undefined

jcollumjcollum Posts: 3Questions: 0Answers: 0
edited March 2012 in General
I've added custom sorting to a table that has a list of items, prices, hyperlinks and percentages. It's actually working fine but I'm getting a js error on page load: Uncaught TypeError: Cannot read property 'oSort' of undefined.

Here's my script tags:
[code]



[/code]

And my init code (coffee):
[code]
table_name = '#the_table'
$(table_name).dataTable(
"aoColumns": [
{ "sType": "string" },
{ "sType": "formatted-num" },
{ "sType": "percent" },
{ "sType": "formatted-num" },
{ "sType": "html" },
],
"bAutoWidth": true,
"aaSorting": [[3,'desc']],
"bJQueryUI": true,

"fnDrawCallback": () ->
window.resize_center(50)
)
[/code]

So I'm pretty sure that I'm setting that up right, according to the docs. But in my `dataTables.sorting.js` file, I get an error on the first line of this code:

[code]
jQuery.fn.dataTableExt.oSort['formatted-num-asc'] = function(a,b) {
/* Remove any formatting */
var x = a.match(/\d/) ? a.replace( /[^\d\-\.]/g, "" ) : 0;
var y = b.match(/\d/) ? b.replace( /[^\d\-\.]/g, "" ) : 0;

/* Parse and return */
return parseFloat(x) - parseFloat(y);
};
[/code]

The error is: Uncaught TypeError: Cannot read property 'oSort' of undefined. This is a Rails app, the coffee and js live side by side with no issues.

Suggestions for getting rid of this error? The page is working fine, but I think leaving js errors in your page looks unprofessional.

Replies

  • jcollumjcollum Posts: 3Questions: 0Answers: 0
    Solved this like so:

    if ($.fn.dataTableExt !== undefined) { ..stuff... }

    For some reason this script is loading twice even though it's only defined once. Might be an issue with how I have Rails set up.
  • jcollumjcollum Posts: 3Questions: 0Answers: 0
    This issue was related to the Rails asset pipeline. The solution: if there's anything that needs to happen at a specific time in the page load, you should put that code in the doc ready function instead of relying on the order of scripts loading. Sorta specific to Rails.
  • allanallan Posts: 64,360Questions: 1Answers: 10,627 Site admin
    Thanks for updating us with what was going on and the solution! :-)

    Allan
This discussion has been closed.