kendo-grid - 带有自定义数据源的 kendo mvc 网格,该数据源具有空 id 行
问题描述
我想用我在更改数据源方面没有发言权的声明来打开这个问题,数据源是数据库中的一个函数,也在其他地方使用。
我有一个剑道网格,我正在从这样的视图模型加载:
var data = db.Database.SqlQuery<OdometerLogsViewModel>($@"
SELECT ID, OrderNum, Carrier, Driver, Truck, StartTime, EndTime, StartOdometer, EndOdometer, Mileage, Type, Locked
FROM dbo.fnMileageLog(isnull('{StartDate}', getdate()), coalesce('{EndDate}', '{StartDate}', getdate()), {filterCarrierID}, {filterDriverID}, {filterTruckID}) ml
WHERE ('{filterType}' LIKE 'Both' OR ml.Type LIKE '{filterType}%')
AND ({id} = 0 or {id} = ml.ID)
ORDER BY Truck, StartTime DESC
这很好地加载到网格中。但是,我注意到当我的替代行(代表卡车的“死头”因此没有订单号)存在(未过滤掉)时,编辑一行需要很长时间。经过检查,事实证明,每次我为所有空 id 条目点击保存时,网格都试图“创建”一条新记录。我测试过的一页上有 43 个。我在网格数据源 requestStart 事件中验证了这一点。显然,在这种情况下,您无法阻止默认值。我如何防止网格这样做?我不反对 hacky 方法。
解决方案
I got in touch with Telerik and they told me that Telerik internally does an IsNew() check on each model instance (each row) of the dataset. Since my alternating rows are not editable and are only there to identify empty miles driven, the solution was to simply modify the sql query to populate the null IDs with a fake value. We chose -1.
var data = db.Database.SqlQuery<OdometerLogsViewModel>($@"
SELECT CASE WHEN ID IS NULL THEN -1 ELSE ID END ID , OrderNum, Carrier, Driver, Truck, StartTime, EndTime, StartOdometer, EndOdometer, Mileage, Type, Locked
FROM dbo.fnMileageLog(isnull('{StartDate}', getdate()), coalesce('{EndDate}', '{StartDate}', getdate()), {filterCarrierID}, {filterDriverID}, {filterTruckID}) ml
WHERE ('{filterType}' LIKE 'Both' OR ml.Type LIKE '{filterType}%')
AND ({id} = 0 or {id} = ml.ID)
ORDER BY Truck, StartTime DESC
推荐阅读
- angular - 使用模态实用程序类避免循环依赖?
- r - flexdashboard 的离子名称
- regex - 在powershell中用正则表达式替换子字符串
- r - 如何将解析函数中的文本与其他(未解析的)文本结合起来进行绘图注释?
- angular - 试图找出一种干净的方法来使用 observables 对每个数组项执行操作
- 3d - 为 PostGIS DB 中的 2D 渲染优化 3D 数据
- laravel - 在 laravel api 中返回带有 json 的响应会删除集合分页链接
- sql - 如何在 Google Spanner 中查看数据库或表的索引?
- r - R 中的 Swimmerplot,堆叠条之间有“空白”空间(ggplot)
- formula - 如何查看和重新编辑 LibreOffice 或 OpenOffice Writer 文本文档表格中的公式?