javascript - JsGrid 将两个(数据库)字段相加并在网格中显示结果

标签 javascript json ajax jsgrid

我有一个 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 内部函数有两个参数valueitem

  • 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/

相关文章:

javascript - 单击按钮时使用 JQuery 显示/隐藏某些 Div

javascript - 更改 javascript 输出区域设置

javascript - 由于范围问题,ng-click 不会在 ng-repeat 内触发

javascript - 将 jquery ajax (json) 附加到表

php - 是否可以在没有 MySQL 的情况下制作功能齐全的购物车

javascript - 文本框在 javascript 中接受 1900 - 2050 的日期

javascript - 页面刷新 jsp 后选择框选项保持选中状态

javascript - 使用 json 制作动态 HTML 表格

python - 如何将 json 加载到 pandas 数据框中?

JavaScript/AJAX 打印异常