javascript jqGrid setCell 计算值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9655426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:22:11  来源:igfitidea点击:

jqGrid setCell calculated value

phpjavascriptjqgrid

提问by haiderc

I need to develop a grid using jqGrid and PHP what has 4 columns: Product, Quantity, Priceand Amount. The product name is retrieved from the database. If the user edits the Quantity and Price columns, the amountcell should be changed automatically by multiplying the Quantityand Pricecolumns. I've tried as follows:

我需要使用 jqGrid 和 PHP 开发一个有 4 列的网格:ProductQuantityPriceAmount。从数据库中检索产品名称。如果用户编辑 Quantity 和 Price 列,则应通过乘以QuantityPrice列来自动更改金额单元格。我试过如下:

var grid = $("#reportTable");

........

afterSaveCell: function (rowid, name, val, iRow, iCol) {
  grid.jqGrid("setCell", rowid, "amount", val, "");
  calculateTotal();
}

Please note that the calculateTotal()can calculate well the grand summary of the Quantitycolumn without any error, and can show on the footer of the grid perfectly.

请注意,calculateTotal()可以很好地计算Quantity列的总汇总,没有任何错误,并且可以完美地显示在网格的页脚上。

采纳答案by Oleg

I suppose that you use cell editing. In the case the afterSaveCellcallback is really good place to update the calculated column amountbased on the current values from the columns quantityand price. The corresponding code could be about the following

我想你使用单元格编辑。在这种情况下,afterSaveCell回调确实是amount根据列quantity和 中的当前值更新计算列的好地方price。对应的代码大概如下

afterSaveCell: function (rowid, cellname, value) {
    var quantity, price, $this;
    if (cellname === 'quantity' || cellname === 'price') {
        $this = $(this);
        quantity = parseFloat($this.jqGrid("getCell", rowid, 'quantity'));
        price = parseFloat($this.jqGrid("getCell", rowid, 'price'));
        $this.jqGrid("setCell", rowid, 'amount', quantity * price);
    }
}

The demodo almost the same, but calculate 'total' as the sum of 'amount' and 'tax'. For the test you should modify the value from 'amount' or 'tax' column and verify that 'total' will be recalculated.

该演示几乎相同,但将“总计”计算为“金额”和“税收”的总和。对于测试,您应该修改“amount”或“tax”列中的值,并验证将重新计算“total”。