【发布时间】:2014-05-08 02:02:29
【问题描述】:
我需要在 Kendo UI Grid 页脚模板中显示自定义计算。内置聚合不适用于业务需求。我找到了一种使用下面所示的代码 sn-ps 在页脚模板中显示函数结果的方法。这段代码的关键点是:
- 已定义自定义计算函数(名为 calc)。
- footerTemplate 中存在对自定义计算函数的引用(即 footerTemplate: "Total: #=window.calc()#" )。
function calc() {
// assume this to be dynamically determined
var field = "Value";
// assume this to be dynamically determined
var dataSource = window.ds;
// some custom calc logic
var newValue = 0;
$.each(dataSource.data(), function(index, model) {
newValue += model.get(field);
});
return newValue;
}
$(document).ready(function() {
window.ds = new kendo.data.DataSource({
data: [
{Name:"One", Value:1},
{Name:"Two", Value:1},
{Name:"Three", Value:1},
{Name:"Four", Value:1},
{Name:"Five", Value:1}
],
pageSize: 10,
schema: {
id: "Name",
fields: {
Name: { type: "string"},
Value: { type: "number"}
}
}
});
$("#grid").kendoGrid({
dataSource: window.ds,
scrollable: false,
pageable: true,
editable: true,
columns: [
{ field: "Name", title: "Name" },
{ field: "Value", title: "Value",
footerTemplate: "Total: #=window.calc()#" }
]
});
});
我要解决的问题是,当用户更改列中的一个值时,计算值需要即时更新。这似乎不是在 Kendo UI Grid API 中实现这一点的直接方法。
到目前为止,我所做的是向 Grid 添加一个保存处理程序,该处理程序调用 Grid 的刷新方法。
$("#grid").kendoGrid({
dataSource: window.ds,
scrollable: false,
pageable: true,
editable: true,
columns: [
{ field: "Name", title: "Name" },
{ field: "Value", title: "Value",
footerTemplate: "Total: #=window.calc()#" }
],
save: function (e) {
e.sender.refresh();
}
});
这可行(页脚值在用户更改列中的值后立即更新),但它有一个缺点。更改值然后单击列中的另一个单元格时,新单击的单元格不会立即进入编辑模式(此功能显然因调用 refresh 方法而短路)。然后,用户必须再次单击单元格才能使其进入编辑模式(糟糕!)。
在http://trykendoui.telerik.com/oVEV/4 有一个此代码的工作示例。
到目前为止,我的解决方案并不理想。有人知道这个问题的更优雅的解决方案吗?
【问题讨论】:
标签: kendo-ui telerik kendo-grid