JQuery Datatable 日期列未正确排序

Posted

技术标签:

【中文标题】JQuery Datatable 日期列未正确排序【英文标题】:JQuery Datatable Date Column Not Sorting Correctly 【发布时间】:2014-08-30 09:11:08 【问题描述】:

我正在开发一个 Asp.Net MVC 5 站点,它使用 JQuery Datatables http://www.datatables.net/ 向用户显示表格数据。我的代码运行良好,即显示、过滤、分页等。除了我的两个日期列(RegisteredDate 和 LastLoginDate)之外,我还对大多数列进行了排序。

剃刀视图

<script>
        $(document).ready(function () 
            $('#dataTables-example').dataTable(  
                "bServerSide": true,  
                "sAjaxSource": "/Doctor/GetAjaxData",
                "bProcessing": true,
                "bJQueryUI": true,
                "aoColumns": [
                           "sName": "DoctorID", "visible": false ,
                          
                              "mData": null,
                              "mRender": function (data, type, full) 
                                  return "<a href='/Admin/Doctor/DoctorDetails/" + full[0] + "'>" + full[1] + "</a>";

                              
                          ,
                          null, //Email
                           "sName": "Doctor_GMC", "bSortable": false , //GMC
                          null, //RegisteredDate
                          null, //LastLoginDate
                          
                              "mData": null,
                              "mRender": function (data, type, full) 
                                  return "<button class='btn btn-danger btn-xs' id='btnDelete' data-toggle='modal' data-target='#myModal' data-id='"+ full[0] +"'>Delete</button>";

                              
                          

                ]
            );  
        );  
</script>

MVC 控制器

        public JsonResult GetAjaxData(JQueryDataTableParamModel param)
        
            IEnumerable<doctor> allDoctors;

            allDoctors = _doctorService.GetAllDoctors();

            var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
            Func<doctor, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Doctor_FName :
                                                        sortColumnIndex == 2 ? c.Doctor_Email :
                                                        sortColumnIndex == 4 ? c.RegisteredDate.Value.ToString("dd/MM/yyyy") :
                                                        c.LastLoginDate.Value.ToString("dd/MM/yyyy"));

            IEnumerable<doctor> filteredDoctors;
            if (!string.IsNullOrEmpty(param.sSearch))
            

                    filteredDoctors = _doctorService.GetAllDoctors()
                            .Where(d => d.Doctor_FName.ToUpper().Contains(param.sSearch.ToUpper())
                                        ||
                                        d.Doctor_LName.ToUpper().Contains(param.sSearch.ToUpper())
                                        ||
                            d.Doctor_Email.ToUpper().Contains(param.sSearch.ToUpper()));

            
            else
            
                filteredDoctors = allDoctors;
            

            var displayedDoctors = filteredDoctors;

            var sortDirection = Request["sSortDir_0"]; // asc or desc
            if (sortDirection == "asc")
                displayedDoctors = displayedDoctors.OrderBy(orderingFunction);
            else
                displayedDoctors = displayedDoctors.OrderByDescending(orderingFunction);

            displayedDoctors = displayedDoctors
                        .Skip(param.iDisplayStart)
                        .Take(param.iDisplayLength);

            var aaData = displayedDoctors.Select(d => new string[]  Convert.ToString(d.DoctorID), d.NameFull, d.Doctor_Email, d.Doctor_GMC, d.RegisteredDate.Value.ToString("dd/MM/yyyy"), d.LastLoginDate.Value.ToString("dd/MM/yyyy") ).ToArray();

            return Json(new
            
                sEcho = param.sEcho,
                aaData = aaData,
                iTotalRecords = allDoctors.Count(),
                iTotalDisplayRecords = filteredDoctors.Count()
            , JsonRequestBehavior.AllowGet);

          

RegisteredDate 和 LastLoginDate 都应该是英国日期,即以 dd/mm/yyyy 显示。它们保持这种格式,但是,排序似乎不是基于日期排序,它似乎只是将日期视为普通字符串。

在我的控制器操作中,您将能够看到我已尝试将日期保留为 uk 格式日期,以尝试排序正常工作,例如,但不幸的是它仍然无法正确排序。

sortColumnIndex == 4 ? c.RegisteredDate.Value.ToString("dd/MM/yyyy") : c.LastLoginDate.Value.ToString("dd/MM/yyyy"));

var aaData = displayDoctors.Select(d => new string[] Convert.ToString(d.DoctorID),d.NameFull,d.Doctor_Email, d.Doctor_GMC, d.RegisteredDate.Value.ToString("dd/MM/yyyy"), d.LastLoginDate.Value.ToString("dd/MM/yyyy") ).ToArray();

有人对我在哪里出错或如何解决有任何建议吗?

非常感谢任何帮助。

谢谢。

【问题讨论】:

【参考方案1】:

如果您将日期保留为 DateTime 类型,它会起作用,所以这样做:

if(sortColumnIndex == 1 || sortColumnIndex == 2)

    Func<doctor, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Doctor_FName : c.Doctor_Email);

if(sortColumnIndex == 4)

    Func<doctor, DateTime> orderingFunction = c => c.LastLoginDate);

因为 orderingFunction 可以有不同的数据类型,您可能必须在每个条件下进行排序,但可以在 1 行上完成,例如:

if(sortColumnIndex == 4)

    Func<doctor, DateTime> orderingFunction = c => c.LastLoginDate);
    filteredDoctors = Request["sSortDir_0"] == "asc" ? filteredDoctors.OrderBy(orderingFunction) : filteredDoctors.OrderByDescending(orderingFunction);

【讨论】:

【参考方案2】:

您可以使用对象而不是字符串数据类型:

Func orderingFunction

【讨论】:

以上是关于JQuery Datatable 日期列未正确排序的主要内容,如果未能解决你的问题,请参考以下文章

Jquery Datatable - 日期排序不适用于月份(相对于日期的月份)

jQuery DataTable 日期顺序

JQuery DataTable 插件宽度问题

使用Rails中的DataTable对格式化的日期列进行排序

数据表按日期正确排序

Jquery datatable Date Column Sorting not sorting based on recent Dates