简体   繁体   English

jQuery Datatable Sort Date列的一种简单方法

[英]JQuery Datatable Sort Date column in a simple way

My date column is not sorting properly, it looks like sorting a normal number and not a proper date column. 我的日期列排序不正确,看起来像对正常数字而不是正确的日期列进行排序。

I use the javascript below for all the datatables I have in my application, so it is generic for all, that's why I don't set the columns information, I leave it to be selected automatic by the datatable. 我在应用程序中使用的所有数据表都使用下面的javascript,因此它对所有数据表都是通用的,这就是为什么我不设置列信息,而是让数据表自动选择它。 In this way is it possible to make my date column to be sort correctly? 这样可以使我的日期列正确排序吗? Or do I have to set something such as column format? 还是我必须设置诸如列格式之类的内容?

 table.dataTable({
                stateSave: true,
                "bLengthChange": false,
                "bFilter": searchable,
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0]
                }],
                "order": [
                    [1, 'asc']
                ],
                "bSort": true,
                "lengthMenu": [
                    [10, 20, 50, 100, 150, -1],
                    [10, 20, 50, 100, 150, "All"]
                ],
                "iDisplayLength": 10,
                "pageLength": 10
            });

Below is my view, I use MVC 下面是我的看法,我使用MVC

  <table class="table table-bordered" id="datatableList">

        <thead class="heading">
            <tr>
                <th class="table-checkbox"><input type="checkbox" class="group-checkable" /></th>
                <th>ID</th>
                <th>Date</th>
                <th>Account Name</th>
            </tr>
        </thead>

        <tbody>

            @foreach (var item in Model.AllLabourCharges.OrderByDescending(x => x.Date))
            {
                <tr>
                    <td><input type="checkbox" class="checkboxes" value="@item.Id" /></td>
                    <td>@Html.ActionLink(item.Id.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0'), "Form", new { item.Id })</td>
                    <td>@item.Date.Date.ToString(GeneralHelper.DateFormat)</td>
                    <td>@Html.EmptyIfNull(x => item.Account.AccountName)</td>
                </tr>
            }

        </tbody>
    </table>

Below my references: 在我的参考资料下方:

<script src="~/Content/assets/global/plugins/select2/select2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="~/Content/assets/global/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="~/Content/assets/global/plugins/datatables/extensions/ColReorder/js/dataTables.colReorder.min.js"></script>
<script type="text/javascript" src="~/Content/assets/global/plugins/datatables/extensions/Scroller/js/dataTables.scroller.min.js"></script>
<script type="text/javascript" src="~/Content/assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js"></script>

您需要首先定义所有列。

You data should be exact format like date format should be as like javascript date, so can to one more things for date. 您的数据应为准确的格式,如日期格式应为javascript date,因此可以为日期添加更多内容。

you can add a hidden date value with your display date value. 您可以在显示日期值中添加隐藏的日期值。

i faced same problem with date field and resolved as above solution. 我遇到与日期字段相同的问题,并解决上述解决方案。

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

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