Q:

JsGrid sum two (database) fields and show result in grid

I have a jsGrid which is build by using a JSON result from a database query.
The database returns the fields Amount1 and Amount2 (for example).
I want to sum those to fields to create a new field which will be shown in the grid. Something like:
   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?

A:

How can I Achieve that?

Inside of itemTemplate function there is two parameter value and  item.

itemTemplate is a function to create cell content. It should return markup as string, DomNode or jQueryElement. The function signature is function(value, item), where value is a value of column property of data item, and item is a row data item.

You can get your sum value using item.Amount1+item.Amount2
DEMO

$("#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>

