Kendo grid sort by nullable property

我的未来我决定 提交于 2019-12-11 20:47:02

问题


I am using a html helper for Kendo grid to display some data. The model used by the grid has a DateTime nullable property and I want to display the items in grid sorted descending by myDateTime property, with the null values first.

I've managed to display them ordered descending, but the null values are displayed last. Any ideas how could I display the null values first and then the descending sorted values?

@(Html.Kendo().Grid<MyVm>()
          .Name("myGrid")
          .Columns(columns =>
          {
              columns.Bound(m => m.myDateProperty);
          })
          .Sortable()
          .DataSource(dataSource => dataSource
              .Ajax()
              .Sort(s => s.Add("myDateProperty").Descending())
              .Model(model => model.Id(a => a.Id))
              .ServerOperation(false)
                  .Read("myMethod", "myController"))

回答1:


I don't think you can do it any other way that this workaround:

ViewModel:

public class GridViewModel{
    public DateTime? myDateProperty { get; set; }
    public DateTime myDateProperty_filter { get {return myDateProperty.HasValue ? myDateProperty.Value : DateTime.MaxValue} ; }
}

Controller:

JsonResult myMethod([DataSourceRequest] DataSourceRequest request){
    //get your data and convert it to GridViewModel
    List<GridViewModel> models = getModel();
    return Json(models.ToDataSourceResult(request));
}

View:

@(Html.Kendo().Grid<GridViewModel>()
      .Name("myGrid")
      .Columns(columns =>
      {
          columns.Bound(m => m.myDateProperty_filter).ClientTemplate("#= myDateProperty #");
      })
      .Sortable()
      .DataSource(dataSource => dataSource
          .Ajax()
          .Sort(s => s.Add("myDateProperty_filter").Descending())
          .Model(model => model.Id(a => a.Id))
          .ServerOperation(false)
              .Read("myMethod", "myController"))


来源:https://stackoverflow.com/questions/26564029/kendo-grid-sort-by-nullable-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!