【问题标题】:How to update column on other column change in inline edit如何在内联编辑中更新其他列更改的列
【发布时间】:2016-08-24 18:25:58
【问题描述】:

免费的 jqgrid 包含 3 列:价格、数量和总和。

使用html5数字输入类型。

如果在内联编辑中更改了 Sum 列,则应使用公式计算 Price 列值

价格 = 总和 / 数量;

我尝试使用下面的 jqgrid 模板来实现这一点,但它不会更改价格列,因为 input#Priceinput#Quantity 未定义。

元素 ID 是从行 ID 创建的,并且在每一行中都不同。 Jqgrid 不传递行 id 值来更改事件。

在免费 jqgrid 中实现此类计算的最佳实践是什么?

var sumColumnTemplate = {
    "editoptions": {
        "type": "number", 
        "dataEvents":[{"type":"change","fn":function(e) {
            $('input#Price').val(parseFloat(this.value) / parseFloat(    
               $('input#Quantity').val()  ));
        }
        }
};

Chrome 检查元素显示为内联编辑模式下的行创建了以下标记:

<tr role="row" id="1383" tabindex="-1" class="ui-widget-content jqgrow ui-row-ltr" editable="1" aria-selected="false">
    ... lot of td s skipped

   <td role="gridcell" style="text-align:right;" class="" title="4" aria-describedby="grid_Quantity"><input type="number" class="grid-decimal-edit editable" id="1383_Quantity" name="Quantity" cm="[object Object]" icol="5" role="textbox" style="width: 100%; box-sizing: border-box;"></td>

   <td role="gridcell" style="text-align:right;" class="" 
   aria-describedby="grid_Price"><input type="number" title="" maxlength="15" class="grid-decimal-edit editable" id="1383_Price" name="Price" cm="[object Object]" icol="6" role="textbox" style="width: 100%; box-sizing: border-box;"></td>

   <td role="gridcell" style="text-align:right;" title="" aria-describedby="grid_Sum"><input type="number" title="" maxlength="15" class="grid-decimal-edit editable" id="1383_Sum" name="Sum" cm="[object Object]" icol="7" role="textbox" style="width: 100%; box-sizing: border-box;"></td>
   ....
</tr>

table 有很多行,id 中的 1383 可能是每一行都不同的行号。

在内联编辑模式下从当前行获取数字值的最佳方法是什么?

使用了 Bootstrap 3、jquery、jquery-ui、ASP.NET MVC 4、Razor 视图、.NET 4.6。

更新

列定义:

{"label":"Quantity","name":"Quantity","index":"Quantity","searchoptions":{"sopt":["eq","ne","lt","le","gt","ge"],"maxlength":12,"size":12},"template":"number","formatoptions":{"thousandsSeparator":"","decimalPlaces":4,"defaultValue":""},"align":"right","editoptions":{"type":"number","step":"any","min":-999999,"max":9999999,"title":"","maxlength":12,"dataEvents":[{"type":"change","fn":function(e) {dataChanged(e.target)}
},{"type":"focus","fn":function(e) {if(typeof e.target.ischanged=='undefined') {e.target.ischanged=false}}
},{"type":"blur","fn":function(e) {}
}],"readonly":null,"class":"grid-decimal-edit","disabled":null},"editable":function(options){return getEditable(options,false);}
,"width":68,"classes":"","hidden":false},

{"label":"OstuPrice","name":"Price","index":"Price","searchoptions":{"sopt":["eq","ne","lt","le","gt","ge"],"maxlength":15,"size":15},"template":"number","formatoptions":{"thousandsSeparator":"","decimalPlaces":5,"defaultValue":""},"align":"right","editoptions":{"type":"number","step":"any","min":-99999999,"max":999999999,"title":"","maxlength":15,"dataEvents":[{"type":"change","fn":function(e) {dataChanged(e.target)}
},{"type":"focus","fn":function(e) {if(typeof e.target.ischanged=='undefined') {e.target.ischanged=false}}
},{"type":"blur","fn":function(e) {}
}],"readonly":null,"class":"grid-decimal-edit","disabled":null},"editable":function(options){return getEditable(options,false);}
,"width":75,"classes":"","hidden":false,"tere":"1234"},

{"template":sumColumnTemplate
,"label":"Sum","name":"Sum","width":80,"index":"Sum","hidden":false},

【问题讨论】:

  • 您能否发布列PriceQuantity 的定义以及可能使用sumColumnTemplate 的其他列?我还看到了cmicol 属性,它们不应该存在于免费jqGrid 的当前版本(4.13.4)中。您使用哪个版本?
  • 我正在使用 4.13.3-pre 。我使用$('input[Name="Price"]')$('input[Name="Quantity"]') 选择器解决了问题。这是最佳实践吗?将来需要实现使用不同列的其他计算。
  • @Oleg 。变量 cm 和 icol 在代码中使用。 jqgrid 如何将这些变量转换为列属性?
  • @Oleg 我更新了问题并添加了列定义。有问题的 sumColumnTemplate 定义已简化。实际上它包含与价格和数量列 colmodel 发布的大部分相同的属性
  • 4.13.3-pre 表示 4.13.2 之后的一些未知的初步版本。我建议您更新到当前版本 4.13.4。您对sumColumnTemplate 的使用有疑问。 **我要求您包括 sumColumnTemplate´ is used. Could you do this?** The usage of $('input[Name="Price"]')` 不好的列的定义。最好先通过 id 获取&lt;tr&gt; 元素(例如使用getGridRowById 方法),然后仅在行内使用jQuery.find('input[Name="Price"]') 搜索。

标签: javascript jquery jqgrid free-jqgrid parsefloat


【解决方案1】:

问题存在是因为您使用template 属性和对象作为值。从 jqGrid 4.7(参见 my old pull request)开始,可以定义“标准”模板并将其用作字符串。

需要使用下面的代码来定义

$.jgrid = $.jgrid || {};
$.jgrid.cmTemplate = $.jgrid.cmTemplate || {};
$.jgrid.cmTemplate.mySum = {
    editoptions: {
        type: "number", 
        dataEvents: [{
            type: "change",
            fn: function(e) {
                // some code
            }
        }]
    }
};

现在可以使用template: "mySum":

{"template":"mySum","label":"Sum","name":"Sum","width":80}

而不是template: sumColumnTemplate:

{"template":sumColumnTemplate,"label":"Sum","name":"Sum","width":80,
"index":"Sum","hidden":false}

【讨论】:

  • 为什么 template:"mySum"template:sumColumnTemplate 更好?它们通过 mySum 产生完全相同的结果,还需要对 cmTemplate 进行额外分配。
  • @Andrus: template:"mySum" 包含 string 值,可以通过 JSON 进行序列化/反序列化。因此,您可以通过 JSON 从服务器加载此类colModel。另一方面,template:sumColumnTemplate 在对象内部包含 函数,不能在 JSON 数据内部使用。
  • 是否应该在更改事件中确定行 ID 以进行计算?如果是,找到它的最佳做法是什么? $(this).closest('tr') ?
  • 如何在定义 colmodel 后应用模板以覆盖现有设置并将新的更改事件处理程序添加到可能在模板属性引用的基本模板和 colmodel 中定义的现有更改事件处理程序?
  • @Andrus:我不确定它是否完全正确。您需要更改哪一列?您需要更改一列还是多列?我认为您不需要使用新的sumColumnTemplate 变量。另一方面,您可能可以使用 setColPropsumColumnTemplate 作为值来在某些列/列上应用新设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 2020-02-19
  • 2013-02-03
相关资源
最近更新 更多