Sorting date in datatable

后端 未结 5 1601
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 17:50

I\'m trying to sort dates in my datatable like DD/MM/YYYY (day, month, year) . I was following https://datatables.net/plug-ins/sorting/ .

but all the

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 18:25

    You can do your own comparator in order to keep the control of how is ordering the dates.

     jQuery.extend(jQuery.fn.dataTableExt.oSort, {
                "ddMmYyyy-pre": function (a) {
                    a = a.split('/');
                    if (a.length < 2) return 0;
                    return Date.parse(a[0] + '/' + a[1] + '/' + a[2]);
                },
                "ddMmYyyy-asc": function (a, b) {
                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                },
                "ddMmYyyy-desc": function (a, b) {
                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                }
            });
    

    As you can see in the above comparator you can choose how to parse the date depending on your data.

    And in the columns definition:

      "columnDefs": [
                {
                    targets: [4], type: "ddMmYyyy"
                }]
    

提交回复
热议问题