Kendo grid sort by nullable property

2019-09-07 05:25发布

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条回答
唯我独甜
2楼-- · 2019-09-07 06:03

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"))
查看更多
登录 后发表回答