繁体   English   中英

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

在具有“某些parseDate函数”的地方,您需要一个将该日期格式解析为javascript Date对象的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM