dataTables and sorting by date in IE8

2019-07-20 17:00发布

Having some trouble having dataTables sort date fields (which have {"sType":"date"}. The null sorting type (which just sorts the data as if it's a plain string) works fine in IE8.

Works in IE 9 & 10, as well as latest versions of Chrome, Safari, Firefox. This app is not used on mobile clients, so I didn't test it there.

Is there something special about IE8 that I need to be looking into? There are no errors on the JavaScript console in IE8, so I'm a little stumped as to where to look. Checked the dataTables docs and search around SO, but have yet to find anything IE8-specific.

UPDATE:

Here's the JavaScript I'm using to initialize my datatable.

//sprv results table
$("#sprv_report_table").livequery(function(){$(this).dataTable({
  "aoColumns": [{"sType":"date"},null,null,null,null,{"sType":"date"}],
  "iDisplayLength": 10, 
  "sPaginationType": "full_numbers"
});});1

Since the datatables code does get applied to the table, and I can sort other columns, I don't have any reason to believe that this event isn't getting fired - it seems rather obvious that it is being fired by the browser.

1条回答
Bombasti
2楼-- · 2019-07-20 17:43

Try something like this:

$(this).dataTable({
  "aoColumns": [{
    mData: function(data, type) {
       var realDate = Date.parse(data); //data will be a string
       switch(type) {
         case 'display':
            return realDate.toString('MM/dd/yyyy'); //note that this line needs to be implemented by you (however you format dates)
         default:
            return realDate;
       }
    }
   },null,null,null,null,{"sType":"date"}],
  "iDisplayLength": 10, 
  "sPaginationType": "full_numbers"
});

See reference on mData usage here: http://datatables.net/ref

integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).

string - read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source.

null - the sDefaultContent option will be used for the cell (null by default, so you will need to specify the default content you want - typically an empty string). This can be useful on generated columns such as edit / delete action columns.

function - the function given will be executed whenever DataTables needs to set or get the data for a cell in the column. The function takes three parameters: {array|object} The data source for the row {string} The type call data requested - this will be 'set' when setting data or 'filter', 'display', 'type', 'sort' or undefined when gathering data. Note that when undefined is given for the type DataTables expects to get the raw data for the object back

{*} Data to set when the second parameter is 'set'.

The return value from the function is not required when 'set' is the type of call, but otherwise the return is what will be used for the data requested.

查看更多
登录 后发表回答