Amit
Amit

Reputation: 196

How to get third row value in jqgrid calculated based on other two column value

I have there column in my jqgrid two vales i am getting from json from third column i have to calculate value from other two and show how can i do this

  jQuery("#vehicleResultGrid").jqGrid({

        data : jsonText,
        datatype : 'local',
        rowNum : 20000,
        width : '100%',
        height : 'auto',
        colNames : [ 'FIN', 'VIN','balnce' ],
        colModel : [{
            name : 'value1',
            sortable:false,
            width : 190,
            classes: "col1"
        },{
            name : 'value2',
            sortable:false,
            width : 190
        },{

            name : 'blance',
            formatter: CalculatedFormatFunction

        }]

    });

     function CalculatedFormatFunction(cellval, opts, rowObject, action) {

           return rowObject[0]*rowObject[1];
    }

I have try with this code.

Upvotes: 0

Views: 3046

Answers (1)

Oleg
Oleg

Reputation: 221997

If you need implement filling of the third blance column on the client side you have two main implementation ways:

  1. usage of custom formatter
  2. usage of beforeProcessing callback.

The second way is better because you can use some predefined formatter for calculated column. For example you can still use formatter: "interger" for the column blance

In case of usage datatype: 'local' the problem of filling third column blance is really trivial. You has already input data (variable jsonText in your original code) as array of items. For example you has input data as

var myOrgData = [
        {value1: 2, value2: 3},
        {value1: 5, value2: 7}
    ];

So you can just add blance property to all items in the input array:

var l = myOrgData.length, i, item;
for (i = 0; i < l; i++) {
    item = myOrgData[i];
    item.blance = item.value1 * item.value2;
    // or if the values could be strings then
    // item.blance = parseInt(item.value1, 10) * parseInt(item.value2, 10);
}

In the way you solve the problem very easy and can use any formatter for the blance column. For example you can defined blance column like

{name: "blance", formatter: "integer", sorttype: "integer"}

If you would use custom formatter instead

{name: "blance", sorttype: "integer",
    formatter: function (cellValue, option, rowObject) {
        return parseInt(rowObject.value1, 10) * parseInt(rowObject.value2, 10);
    }}

You could use unchanged input data, but the advantages of predefined formatter you can't use or you would have to call the original formatters manually which make the code more complex and less readable.

If you have datatype: "json" or datatype: "xml" than the usage of beforeProcessing callback is very close to the simple modification of input data described above. The callback beforeProcessing receive the data returned from the server as object and the callback can modify it. One can add additional property in the same way.

Upvotes: 1

Related Questions