Aurelia 转换值和数据表

Aurelia converted values and dataTables

我在 Aurelia 组件中使用 DataTables jQuery 插件。使用列排序,它可以很好地排除带有日期的列。
在此专栏中,我使用 value-convertet 将 isoString 值转换为 DD.MM.YYYY 日期格式。 Value covreters 的使用会导致错误的日期列排序,但如果我不使用 value-converter,一切正常。不幸的是,我没有找到它无法正常工作的任何原因。

错误的过滤示例:我在 18.05.2017

之前看到日期值类似于 27.05.2010 的行

数据表初始化:

$('#searchResultsTable').dataTable({
    destroy: true,
    searching: false,
    paging: false,
    orderMulti: false,
    order: [[ 2, "desc" ]],
    dateFormat: 'DD.MM.YYYY'
});

日期值转换器(使用moment库):

import * as moment from 'moment';

export class DateFormatValueConverter {
    toView(value: Date, format: string): string {
        if (value) {
            return moment(value).format(format);
        }

        return null;
    }

    fromView(value: string, format: string): Date {
        var isValid = moment(value, format, true).isValid();
        if (value && isValid) {
            return moment(value, format).toDate();
        }

        return null;
    }
}

更新: 使用值转换器订购

Orderd 没有 ValueConverter(按照 2017 年值的顺序排序)

数据的排序机制 table 工作正常 - 恐怕您的理解有误。

当按降序排序时,任何以 27. 开头的都将排在最前面,因为它们是“最大的”。在所有以 27 开头的日期中,它将按月份排序,最大的在前,然后是年份。

排序机制没有意识到您正在排序日期,因此我们需要查看自定义排序插件;

https://www.datatables.net/plug-ins/sorting/

特别是 Date-De 插件 - 因为它符合您的日期格式;

https://www.datatables.net/plug-ins/sorting/date-de

取自上页的示例;

$('#example').dataTable( {
    columnDefs: [
        { type: 'de_datetime', targets: 0 },
        { type: 'de_date', targets: 1 }
    ]
});