【发布时间】:2014-11-08 09:09:24
【问题描述】:
我有一个订单线网格,有一个总计列,使用模型定义中的总计函数计算(数量 x 价格):
schema: {
model: {
id: "id",
fields: {
id: { type: "string" },
orderId: { type: "number" },
lineNo: { type: "number" },
itemNo: { type: "string" },
...
price: { type: "number" },
qty: { type: "number" },
comment: { type: "string" },
},
total: function () {
return (this.get("qty") * this.get("price")).toFixed(2);
}
}
}
注意:我在模型定义中使用总函数而不是常规剑道模板“#=Qty * price#”的原因是我需要自定义编辑表单的总函数(它有一个总字段需要数量或价格变化时动态更新)。
列定义在这里:
{
field: "total",
title: "Total",
width: 60,
template: "#= total() #",
}
这里是一个笨蛋:http://plnkr.co/edit/YHiupWy49mvk4kZAqJTA?p=preview
我需要在网格之外有一个始终反映网格总数(= Total 列的总数)的字段,我在 plunker 的网格下方放置了一个占位符,Grand Total ;这是总数应该显示的地方。
我怎样才能做到这一点,最好是用 AngularJS 的方式?
编辑 2014 年 9 月 15 日,根据接受的答案,这是我采用的解决方案:
dataBound: function(e) {
var gridData = e.sender.dataSource.data();
$timeout(function() {
$scope.ticket.subTotal = 0;
for (var i = 0; i < gridData.length; i++) {
$scope.ticket.subTotal += gridData[i].qty*gridData[i].price;
}
});
}
请注意,我使用 $timeout 是因为添加行是以 Angular 感知的方式完成的,而常规编辑则不是。通过使用 $timeout,$apply 将始终以安全的方式自动应用。
HTML:
<b>Grand Total: </b> <input type="text" ng-model="ticket.subTotal" />
【问题讨论】:
标签: angularjs kendo-ui kendo-grid