2015-02-09 144 views
0

我嘗試對自定義日期格式列和所有我發現的dataTables插件進行排序,但無效。DataTables自定義日期格式排序

予加載這樣的數據:

HTML:

<table id="task-list" class="display" cellspacing="0" width="100%" style="display: none;"> 
    <thead> 
     <tr> 
      <th><?php echo $this->translate('th-title'); ?></th> 
      <th><?php echo $this->translate('th-description'); ?></th> 
      <th><?php echo $this->translate('th-author'); ?></th> 
      <th><?php echo $this->translate('th-date'); ?></th> 
      <th><?php echo $this->translate('th-status'); ?></th> 
      <th><?php echo $this->translate('th-responsible'); ?></th> 
      <th><?php echo $this->translate('th-options'); ?></th> 
     </tr> 
    </thead> 

    <tfoot> 
     <tr> 
      <th><?php echo $this->translate('th-title'); ?></th> 
      <th><?php echo $this->translate('th-description'); ?></th> 
      <th><?php echo $this->translate('th-author'); ?></th> 
      <th><?php echo $this->translate('th-date'); ?></th> 
      <th><?php echo $this->translate('th-status'); ?></th> 
      <th><?php echo $this->translate('th-responsible'); ?></th> 
      <th><?php echo $this->translate('th-options'); ?></th> 
     </tr> 
    </tfoot> 
</table> 

和JS部分:

$('table#task-list').on('xhr.dt', function (e, settings, json) { 
    $(document).trigger('task.filter.applied',[trigger, target]); 
    $(this).show(); 
}).on('error.dt', function (e, settings, techNote, message) { 
    alert('error occured'); 
}).dataTable({ 
    "destroy": true, 
    "ajax": { 
     url: $(trigger).prop('href'), 
     type: 'POST', 
     data: { filterUser: _this.filterUser }, 
    }, 
    "columns": [ 
     { "data": "title" }, 
     { "data": "description" }, 
     { "data": "author" }, 
     { "data": "date_created" }, 
     { "data": "status" }, 
     { "data": "responsible" }, 
     { "data": "options" } 
    ], 
    "columnDefs": [ 
     { className: "options", "targets": [-1] }, 
    ],        
    "order":[[3,'desc']], 
    "iDisplayLength": 50, 
    "fnInitComplete": function() { 
     $('i.fa[data-dt-action]').tooltip(); 
     PLUGIN.applyButtonAction({ target: 'i.fa[data-dt-action="edit"]', fn: 'Task.edit' }); 
     PLUGIN.applyButtonAction({ target: 'i.fa[data-dt-action="assign"]', fn: 'Task.assign' }); 
     PLUGIN.applyButtonAction({ target: 'i.fa[data-dt-action="status"]', fn: 'Task.status' }); 
     PLUGIN.applyButtonAction({ target: 'a[data-dt-action="view"]', fn: 'Task.view' }); 
    }, 
    language: Dash.dataTables.language     
}); 

在列4(指數3)日期格式爲dd.mm.yyyy, hh:mm 任何想法如何正確地排序?

謝謝!

回答

0

您將需要使用自定義列定義來區分顯示值和其他用途。

'columns': [{ 
    ...snip other columns... 
    [ 
     'title': 'Some Column Title', 
     'type': 'date', 
     'data': function(row, type) { 
      if (type === 'display') { 
       return row["YourColumn"]; //or row[index] if you like 
      } else { 
       return //some parseDate(row["YourColumn"]) function here; 
      } 
     } 
    ], 
    ...snip other columns... 
}], 

如果我有「一些parseDate功能」,你需要的是分析該日期格式轉換成一個javascript Date對象的函數。

+0

謝謝,它的作品就像一個魅力:D – 2015-02-10 08:56:31