MVCjqGrid.添加日期列

本文关键字:日期 添加 MVCjqGrid | 更新日期: 2023-09-27 18:12:53

我有一个网格:

 @(Html.Grid("grid")
      .SetJsonReader(new MvcJqGrid.DataReaders.JsonReader { Id = "Id", RepeatItems = false })
      .SetRequestType(RequestType.Post)
      .AddColumn(new Column("Date").SetWidth(300).SetLabel("Date").)
      .SetUrl(Url.Action("Action", "Controller"))
      .SetRowNum(10)
      .SetHeight(500)
      .SetRowList(new[] { 10, 15, 20, 50 })
      .SetViewRecords(true)
      .SetGridView(true)
      .SetPager("pager")
    )

和此网格的数据:

[HttpPost]
public JsonResult Action(GridSettings gridSettings)
{
    var data = new List<DateTime>();
    data.AddRange(Enumerable.Range(0, 1000).Select(s => new
    {
        Date = DateTime.Now.AddHours(s * 4),
    }));
    int totalRecords = data.Count;
    if (!string.IsNullOrWhiteSpace(gridSettings.SortColumn))
    {
        data = (gridSettings.SortOrder == "asc" ?
            data.AsQueryable().OrderByPropertyName(gridSettings.SortColumn) :
            data.AsQueryable().OrderByPropertyNameDescending(gridSettings.SortColumn)).ToArray();
    }
    data = data.Skip((gridSettings.PageIndex - 1) * gridSettings.PageSize).Take(gridSettings.PageSize).ToArray();
    var jsonData = new
    {
        total = totalRecords / gridSettings.PageSize + 1,
        page = gridSettings.PageIndex,
        records = totalRecords,
        rows = data
    };
    return Json(jsonData);
}

那么问题是我应该如何使用日期?如果我离开他们作为日期时间,他们将显示没有格式,但与Date = DateTime.Now.AddHours(s * 4).ToString(),将不排序正确?

MVCjqGrid.添加日期列

你可以格式化你排序后的数据(我重构了你的控制器动作一点点,让它工作):

[HttpPost]
        public JsonResult GetData(GridSettings gridSettings)
        {
            var data = Enumerable.Range(0, 1000).Select(s => DateTime.Now.AddHours(s * 4)).ToList();
            var totalRecords = data.Count();
            if (!string.IsNullOrWhiteSpace(gridSettings.SortColumn))
            {
                data = (gridSettings.SortOrder == "asc" ?
                    data.OrderBy(x=>x) :
                    data.OrderByDescending(x=>x)).ToList();
            }
            data = data.Skip((gridSettings.PageIndex - 1) * gridSettings.PageSize).Take(gridSettings.PageSize).ToList();
            var id = (gridSettings.PageIndex - 1) * gridSettings.PageSize;
            var jsonData = new
            {
                total = totalRecords / gridSettings.PageSize + 1,
                page = gridSettings.PageIndex,
                records = totalRecords,
                rows = data.Select(d=>new
                    {
                        id = ++id,
                        Date = d.ToShortDateString()
                    })
            };
            return Json(jsonData);
        }