如何获取基于其他两列值计算的jqgrid中的第三行值 [英] How to get third row value in jqgrid calculated based on other two column value

查看:155
本文介绍了如何获取基于其他两列值计算的jqgrid中的第三行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jqgrid中有两个值,我从第三列的json中获取了两个值,我必须从其他两个值中计算出值,并说明如何做到这一点

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];
    }

我尝试使用此代码.

推荐答案

如果需要在客户端实现第三列blance的填充,则有两种主要的实现方式:

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

  1. 自定义格式化程序的使用
  2. beforeProcessing回调的用法.
  1. usage of custom formatter
  2. usage of beforeProcessing callback.

第二种方法更好,因为您可以将一些预定义的格式化程序用于计算列.例如,您仍然可以将formatter: "interger"用于列blance

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

在使用datatype: 'local'的情况下,填充第三列blance的问题确实微不足道.您已经输入数据(原始代码中的变量jsonText)作为项目数组.例如,您输入的数据为

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}
    ];

因此,您只需将blance属性添加到输入数组中的所有项目:

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);
}

以非常简单的方式解决问题,并且可以将任何格式化程序用于blance列.例如,您可以定义blance列,如

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.

如果您具有datatype: "json"datatype: "xml",则beforeProcessing回调的用法与上述输入数据的简单修改非常接近.回调beforeProcessing接收从服务器返回的数据作为对象,并且回调可以对其进行修改.可以用相同的方式添加其他属性.

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.

这篇关于如何获取基于其他两列值计算的jqgrid中的第三行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆