DataTables自定义日期格式排序

DataTables custom date format sorting

我尝试对自定义日期格式列进行排序,但我发现的所有 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 知道如何正确排序吗?

谢谢!

您将需要使用自定义列定义来区分显示值和其他用途的值。

'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...
}],

我有 "some parseDate function" 你需要一个函数来将日期格式解析为 javascript Date 对象。