【问题标题】:Jquery Datatable - Date sorting not working with months (Months with respect to dates)Jquery Datatable - 日期排序不适用于月份(相对于日期的月份)
【发布时间】:2019-11-12 19:42:39
【问题描述】:

除日期外的所有事物的数据表排序。仅按日期(天)排序,而不考虑其月份。 我有来自数据库的(DD-MM-YYYY)格式的日期。但有些日期也在另一个月之间。

我用过 Jquery (jquery-3.3.1.js) 和 Datatable (datatables_1.10.19.js)

$(document).ready(function (){
    var rows_selected = [];
    var bookid_value = [];
    var table = $('#example').DataTable({
        "language": {
            "search": ' ',
            "searchPlaceholder": "Search",
        },
        lengthChange: false,
        "scrollY":        "1000px",
        "scrollCollapse": true,
        "paging":         false,
        'columnDefs': [{
            'targets': 1,
            'searchable': true,
            'orderable': false,
            'width': '1%',
            'bSort': true,
            "type": 'date'
        }],
        'order': [[1, 'asc']],

        'rowCallback': function(row, data, dataIndex){
             var rowId = data[0];
            if($.inArray(rowId, rows_selected) !== -1){

                $(row).find('input[type="checkbox"]').prop('checked', true);
                $(row).addClass('selected');
            }
        }
    });

});

输出:(排序后)

31-08-2019
31-07-2019
31-08-2019
25-07-2019
31-08-2019
08-07-2019
31-08-2019
04-07-2019
31-08-2019
10-07-2019
10-07-2019
13-07-2019
15-07-2019
31-08-2019
31-08-2019

【问题讨论】:

  • 数据表无法识别您的日期格式。你可以用谷歌搜索数据表排序日期,你会意识到:这些日期是作为字符串而不是日期来管理的。所以这种排序是正确的(对于字符串)。

标签: javascript jquery arrays sorting datatables


【解决方案1】:

尝试使用moment.js插件

$(document).ready(function (){
    var rows_selected = [];
    var bookid_value = [];

    $.fn.dataTable.moment('DD-MM-YYYY');

    var table = $('#example').DataTable({
        "language": {
            "search": ' ',
            "searchPlaceholder": "Search",
        },
        lengthChange: false,
        "scrollY":        "1000px",
        "scrollCollapse": true,
        "paging":         false,
        'columnDefs': [{
            'targets': 1,
            'searchable': true,
            'orderable': false,
            'width': '1%',
            'bSort': true,
            "type": 'date'
        }],
        'order': [[1, 'asc']],

        'rowCallback': function(row, data, dataIndex){
             var rowId = data[0];
            if($.inArray(rowId, rows_selected) !== -1){

                $(row).find('input[type="checkbox"]').prop('checked', true);
                $(row).addClass('selected');
            }
        }
    });

});

更多信息可以找到here

【讨论】:

    【解决方案2】:

    据我所知,DataTable (JS) 的排序独立于您的查询。我使用 JS 分享我的解决方案。在我的情况下,第一列,数字 0 是日期类型,默认情况下我要订购。

    JavaScript

    $('#datatable').DataTable({
    order: [[0, 'desc']],
    responsive: true
    });
    

    查看 (HTML) 说明性示例。

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/>
    <script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.18/r-2.2.2/datatables.min.js"></script>
    <table id="datatable" class="table table-striped table-bordered" style="width:100%">
    <thead>
        <tr>
        <th>Date</th>
        <th>Name</th>
        <th>Option</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td>datevalue</td>
        <td>namevalue</td>
        <td>optionvalue</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
        <th>Date</th>
        <th>Name</th>
        <th>Option</th>
        </tr>
    </tfoot>
    </table>
    

    来源:https://datatables.net

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多