Bug in sorting (with fix)

Bug in sorting (with fix)

systematicalsystematical Posts: 23Questions: 0Answers: 0
edited March 2014 in DataTables 1.10
I've found a bug with DataTables 1.10.0-beta.3.dev the following datatables setup:

[code]
var oTable = $('#report-table').dataTable({
"processing": true,
"paging": true,
"ordering": true,
"serverSide": true,
"sAjaxSource": "/hidden/hidden.json",
"ajax": "/hidden/hidden.json",
"sDom": 'frtip',
"iDisplayLength": 300,
"bAutoWidth": false,
"aoColumns": [
{mData:"Data.barcode"},
{mData:"Data.ref"},
{mData:"Data.line_number"},
{mData:"Data.company_name"},
{mData:"Data.name"},
{mData:"Data.buy_price"},
{mData:"Data.status"},
{mData:"Data.total_repairs",bSortable:false},
{mData:"Data.created"}
]
});
[/code]

What occurs is that when a column is sorted that has null as a value the following error is encountered:

[code]
Uncaught TypeError: Cannot read property 'replace' of null jquery.dataTables-1.10.0-beta.3.js?_44617362=:13760
[/code]

Example of XHR JSON response:
[code]
{
"sEcho": 1,
"iTotalRecords": 15517,
"iTotalDisplayRecords": 1039,
"aaData": [
{
"Data": {
"id": "229",
"barcode": "6153850",
"line_number": "3",
"ref": "3091200"
"created": "2013-09-25 14:57:12"
"company_name": "HIDDEN" ,
"barcode": "6153850",
"buy_price": "65.00",
"sell_price": "105.00",
"unit_number": "",
"customer_tag": "",
"production_week": "311",
"status": null,
"total_repairs": null
}
}
]
}
[/code]

Here is the change I made to the code:

[code]
"html-pre": function ( a ) {
return a.replace ?
a.replace( /<.*?>/g, "" ).toLowerCase() :
a+'';
},
[/code]

Changed to:

[code]
"html-pre": function ( a ) {
if(a != null){
return a.replace ?
a.replace( /<.*?>/g, "" ).toLowerCase() :
a+''
}
else{
return a;
}
},
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    To be honest, I don't think this is actually a bug. its not how I would expect the issue to crop up perhaps, but the issue is the null and DataTables isn't being told how to handle it. For that you want to use sDefaultContent / defaultContent . Simply set it to be an empty string.

    Having said that, the html sorting type probably should cope with null values - I'll look into that.

    Allan
  • systematicalsystematical Posts: 23Questions: 0Answers: 0
    It is odd to return null like that from the server, but this is they way CakePHP will return values if their is no related record on a LEFT JOIN. I'll use sDefaultContent rather than modifying dataTables core. Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    No - null is fine. It is 100% valid data - you just need to tell DataTables what to do with it. null isn't just assumed to be an empty space (this isn't Oracle software!).

    I made the change to DataTables earlier on today: https://github.com/DataTables/DataTablesSrc/commit/46d1f3bbf . Forgot to post back here.

    However, I would still recommend using sDefaultContent.

    Allan
This discussion has been closed.