You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kendo Grid排序修改:解决IdleTime列排序问题,改用UpdatedTime排序

解决Kendo Grid中IdleTime列排序异常,改用UpdatedTime字段排序

我来帮你搞定这个排序问题!你遇到的情况是IdleTime列因为是字符串格式,排序逻辑不符合预期,想要让它点击排序时实际按UpdatedTime字段来排序对吧?下面给你两种简单可行的方案,结合你的现有代码来调整:

首先先把你现有的Grid代码贴出来方便参考:

@(Html.Kendo().Grid(Model).Name("StudentModel")
    .Columns(column => {
        column.Bound(p => p.ID).Width(30).EditorTemplateName("#=GetID(this)#");
        column.Bound(p => p.IdleTime).Width(30);
        column.Bound(p => p.UpdatedTime).Width(50);
        // 其他列配置...
    })
    // 其他Grid配置...
)

方案一:直接给IdleTime列指定排序字段

这是最直接的方式,只需要在IdleTime列的配置里加上Sortable的字段指定,告诉Grid点击这个列排序时,用UpdatedTime作为排序依据:

column.Bound(p => p.IdleTime).Width(30)
    .Sortable(sort => sort.Field(p => p.UpdatedTime));

方案二:在数据源层面配置排序规则

如果你的Grid有多个列需要自定义排序逻辑,可以在DataSource里统一配置,指定当排序IdleTime列时,实际使用UpdatedTime字段:

.DataSource(dataSource => dataSource
    .Ajax() // 这里根据你的数据源类型调整,比如Server模式就改成.Server()
    .Sort(sort => sort.Add("IdleTime").Field("UpdatedTime"))
    // 这里继续你的其他数据源配置,比如Read方法、Model定义等
)

小提示

  • 确保你的UpdatedTime字段在后端是标准的DateTime类型,这样排序出来的时间顺序才是准确的。
  • 如果不需要在界面上显示UpdatedTime列,可以把它设置为隐藏,不影响排序逻辑:
    column.Bound(p => p.UpdatedTime).Width(50).Hidden();
    

内容的提问来源于stack exchange,提问作者Oniel Telies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:30:33