简体   繁体   English

Jquery 数据表 - 日期排序不适用于月份(与日期相关的月份)

[英]Jquery Datatable - Date sorting not working with months (Months with respect to dates)

Datatable sorting with all things excluding with Dates.数据表排序,除日期外的所有内容。 Only sort with date(days) without considering their months.仅按日期(天)排序而不考虑月份。 I have dates in (DD-MM-YYYY) formats which was coming dynamically from database.我有 (DD-MM-YYYY) 格式的日期,它是从数据库动态获取的。 But some of the dates were coming between another month also.但有些日期也在另一个月之间。

I have used Jquery (jquery-3.3.1.js) and Datatable (datatables_1.10.19.js)我用过 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');
            }
        }
    });

});

Output : (After Sorting)输出:(排序后)

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

Try using moment.js plugin尝试使用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');
            }
        }
    });

});

More info can be found here更多信息可以在这里找到

As far as I know, DataTable (JS) sorts independent of your query.据我所知,DataTable (JS) 的排序与您的查询无关。 I share my solution using JS.我使用 JS 分享我的解决方案。 In my case the first column, the number 0 is Date type and It's that I want to order by default.在我的情况下,第一列的数字 0 是日期类型,这是我默认要订购的。

JavaScript JavaScript

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

View (HTML) Illustrative example.查看 (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>

Source: https://datatables.net来源: https : //datatables.net

Regards问候

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

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