0

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.

Amit
  • 196
  • 4
  • 16
  • You should add more full code of jqGrid which you use. It's unclear *where* and *how* you calculate value for the "third" row (or column???). What is your question? *Where* you try to get the value? Is the code fragment which you posted from some callback (for example from `loadComplete`)? Sorry, but your current question is absolutely unclear. – Oleg Apr 01 '13 at 11:56
  • 1
    If I think I understand the question you can see my answer at http://stackoverflow.com/questions/15682574/calculate-average-of-two-sums-from-two-collumn-and-show-it-in-next-collumn-in-jq/15684766#15684766 – Mark Apr 01 '13 at 12:01
  • @Mark i have seen your question but there is problem of rowObject accessing i have updated code i am getting undefine value for third column but if i use return 1; in column it show me that value. – Amit Apr 01 '13 at 13:46
  • 1
    @Amit In your CalculatedFormatFunction make sure that both rowObject[0], [1] are values that can be multiplies (both are able to be translated to numbers in Javascript) – Mark Apr 01 '13 at 14:14
  • @Amit: If you use `datatype : 'local'` then the value of `data` should be array of items with *named* properties. For example, `[{value1:2,value2:10},{value1:"5",value2:"20"}, ...]`. So the usage of `rowObject[0]*rowObject[1]` inside of `CalculatedFormatFunction` would be wrong. You should use `parseInt(rowObject.value1,10)*parseInt(rowObject.value2,10)` instead. – Oleg Apr 01 '13 at 15:26

1 Answers1

1

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798