Sorting Plugins - Uncaught TypeError: Cannot read property '1' of null

Sorting Plugins - Uncaught TypeError: Cannot read property '1' of null

FourmatFourmat Posts: 2Questions: 0Answers: 0
edited November 2013 in General
Hello,

I have an issue with some of the sorting plugins such as the alt-string, and title-string. I have tried to use these plugins on several columns, and I keep getting the following error in the console: Uncaught TypeError: Cannot read property '1' of null.

My script is :
[code]
$(document).ready(function() {
$('#datatableproductListing').dataTable({

"sDom": '<"top"iflp<"clear">>rt<"bottom"ip<"clear">>',
//"bJQueryUI": true,
"iDisplayLength": 25,
"aaSorting": [ [5,'asc']],
//"aaSorting": [[ 2, "asc" ], [3,'asc'], [4,'asc'], [5,'asc']],
"aLengthMenu": [[10, 25, 50, 100,300,1000,-1], [10, 25, 50,100,300,1000, "All"]],
//"bJQueryUI": true,
"sPaginationType": "full_numbers",
//"bPaginate": false,
//"bFilter": false,
//"bInfo": false
"aoColumnDefs": [
{ "sWidth": "1px", "aTargets": [ 0 ] },
{ "sWidth": "1px", "aTargets": [ 1 ] },
{ "sWidth": "1px", "aTargets": [ 2 ] },
{ "bSearchable": false, "bVisible": false, "aTargets": [ 0 ] }
],
"aoColumns": [
{ "bSortable": false },
{ "bSortable": false },
null,
{ "sType": 'alt-string' },
{ "sWidth": '60%' },
{ "sType": 'currency' },
{ "bSortable": false }
]


})

} );

[/code]

and the plugin codes are:

[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"title-string-pre": function ( a ) {
return a.match(/title="(.*?)"/)[1].toLowerCase();
},

"title-string-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"title-string-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"alt-string-pre": function ( a ) {
return a.match(/alt="(.*?)"/)[1].toLowerCase();
},

"alt-string-asc": function( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"alt-string-desc": function(a,b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

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));
};
[/code]

contents of the cell in the column I'd like to sort on is:
[code]

[/code]

Sometimes the cells are blank and contain only:
[code][/code]

the error references lines similar to this:
[code]
return a.match(/alt="(.*?)"/)[1].toLowerCase();
[/code]

If there is a blank cell, does that bring up the 'null' error? If so How can I prevent this from happening?

An example of this issue is here:
http://www.emisupply.com/catalog2/specials.php
username: demo
pw: demo1
(this is a temporary login available for a short period while we dissect this issue)


Thanks for any help, Matt

Replies

  • FourmatFourmat Posts: 2Questions: 0Answers: 0
    edited November 2013
    Well, I found a solution through some trial and error. Basically I changed the sorting plugin to set the contents of the alt attribute to a character that would force the sorting to the end, such as '{' and it works fine. For example:

    [code]
    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "alt-string-pre": function ( a ) {
    if (a.match(/alt="(.*?)"/) == null){
    //sets a to a character that forces the sort to the end.
    a = '{';
    return a;
    }else{
    return a.match(/alt="(.*?)"/)[1].toLowerCase();
    }
    },

    "alt-string-asc": function( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "alt-string-desc": function(a,b) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
    } );
    [/code]

    Now when the script runs into a cell that is empty, it will assign it the '{' character and sort accordingly.

    I came up with this solution after finding this thread: http://datatables.net/forums/discussion/4025/sorting-to-ignore-empty-cells/p1
This discussion has been closed.