JQuery数据表日期列未正确排序

时间:2014-07-09 11:07:39

标签: jquery jquery-datatables

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

Razor查看

<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显示。它们保持这种格式,但是,排序似乎不是根据日期排序,它似乎只是将日期视为普通字符串。

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

  

sortColumnIndex == 4? c.RegisteredDate.Value。 ToString(“dd / MM / yyyy”):   。c.LastLoginDate.Value的的ToString( “DD / MM / YYYY”));

     

var aaData = displayedDoctors.Select(d =&gt; 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();

有没有人对我在哪里出错或者如何解决这个问题有任何建议?

非常感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:2)

如果您将日期保留为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可以有不同的datataypes,你可能不得不在每个条件下进行排序,但它可以在1行完成,例如:

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

答案 1 :(得分:0)

您可以使用object代替字符串数据类型:

Func <医生,对象> orderingFunction