DataTables自定义日期格式排序

时间:2015-02-09 16:03:51

标签: javascript 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 知道如何正确排序吗?

谢谢!

1 个答案:

答案 0 :(得分: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对象的函数。