我有一个 jsGrid,它是使用数据库查询的 JSON 结果构建的。 数据库返回字段 Amount1 和 Amount2(例如)。
我想将这些字段求和以创建一个将显示在网格中的新字段。像这样的东西:
fields: [{
name: "Amount1",
type: "text",
title: "bla",
width: 45
},
{
name: "Amount2",
type: "text",
title: "blabla",
width: 60
},
{
name: "Sum",
type: "text"
},
{
itemTemplate: function(Sum) {
return Amount1.value + Amount2.value {
someformatting
};
},
我怎样才能实现这一目标?
最佳答案
How can I Achieve that?
itemTemplate
内部函数有两个参数value
和item
。
- itemTemplate 是创建单元格内容的函数。它应该以字符串、DomNode 或 jQueryElement 的形式返回标记。函数签名为function(value, item),其中value是数据项的列属性值,item是行数据项目。
您可以使用item.Amount1+item.Amount2
获取总值(value)
演示
$("#jsGrid").jsGrid({
width: "100%",
data: [{
Amount1: 10,
Amount2: 12
}, {
Amount1: 156,
Amount2: 125
}, {
Amount1: 101,
Amount2: 122
}],
fields: [{
width: 80,
align:'center',
name: "Amount1",
type: "number"
}, {
width: 80,
align:'center',
name: "Amount2",
type: "number",
}, {
width: 80,
align:'center',
headerTemplate: function() {
return "<th class='jsgrid-header-cell'>Sum</th>";
},
itemTemplate: function(value, item) {
return item.Amount1+item.Amount2;
}
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
<div id="jsGrid"></div>
关于javascript - JsGrid 将两个(数据库)字段相加并在网格中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50505422/