我有一个如下所示的 XML 数据源,<root> <item priceOri = "100" discount = "10"></item> <item priceOri = "200" discount = "110"></item> . . .</root>我正在使用 JqGrid 将这些数据填充到表中。代码如下所示。datatype : 'xml',colModel: [ ... {name:'priceOri', index:'priceOri', width:60,xmlmap : "[priceOri]", align:"center"}, {name:'discount', index:'discount', width:60,xmlmap : "[discount]", align:"center"}, {name:'price', index:'price', width:60,xmlmap : "[price]", align:"center", editable: true, formatter:discountFmatter}, ... ]xmlReader: { root: "root", row: "item", repeatitems: false},格式化程序如下所示。function discountFmatter (cellvalue, options, rowObject){ var price; // do calculation based on other cell values in the same column //price = priceOri - discount; var new_format_value = price; return new_format_value}在代码中,我需要访问 item 标记中的其他值来计算 price 部分。所以基本上我想访问同一行中的其他单元格值。我怎样才能做到这一点。我使用了下面的代码片段,但结果是 undefinedrowObject[0] // undefinedrowObject.priceOri //undefined谁能告诉我实现这一目标的步骤。 更新: 我有 Tony Tomov 的 JqGrid 4.4.0 版本。由于我正在开发一个已经开发的应用程序,因此我无法更改或更新该库版本。所以我必须使用相同的 JqGrid 版本 4.4.0。似乎 Oleg 的 rowObject instanceof Element ? $(rowObject).attr("priceOri") : rowObject.priceOri 正在满足此要求。 更新 2:(因为 rowObject.rewards 在以下情况下不起作用)新的 XML 具有以下更改格式, 所以新的格式化程序会像,function discountFmatter (cellvalue, options, rowObject){ var price; // do calculation based on other cell values in the same column //price = priceOri - discount - rewards; var new_format_value = price; return new_format_value}请注意,我没有在 jQgrid 表中显示值 rewards。那么我怎样才能做到这一点。奥列格的回答:In case of usage old jqGrid you will have to add new column rewards. You can use hidden: true property in the column. Free jqGrid allow you to use additionalProperties 最佳答案 在 custom formatter 内部处理数据存在一个重要问题。 rowObject 的格式与输入数据项相同。因此,必须使用 rowObject[0] 或 rowObject.priceOri 来处理 JSON 数据,这取决于是否使用了 repeatitems: false。同样,自定义格式化程序的代码应该是另一个处理 JSON 数据的代码,它使用下一个节点或属性。一个必须使用 $(rowObject).find(">priceOri") 或 $(rowObject).attr("priceOri") (最后一个对应于您的数据格式)。如果另外使用 loadonce: true ,则事件更复杂(为了理解)必须是代码。只有在处理从服务器加载的数据时才必须使用上述表达式。下一个处理(在本地排序、分页或过滤之后)必须使用 rowObject.priceOri 表示法。在 Tony 更改了 jqGrid 的许可协议(protocol)(参见 free jqGrid ),使产品商业化(参见 the post )并将 jqGrid 重命名为“Guriddo jqGrid JS”之后,我开发了 jqGrid 的 the prices 分支。自从大约一年的免费 jqGrid 开发以来,我实现了许多功能(其中大部分功能在 the wiki 和每个已发布版本的自述文件中都有描述)。Free jqGrid 保持与以前版本相同的 rowObject 参数格式以保持与以前版本的兼容性,但扩展了参数 option。它包含 rowData 属性和 解析的 数据。要在免费的 jqGrid 中访问自定义格式化程序中的 priceOri ,可以使用 options.rowData.priceOri 。因此你可以使用function discountFmatter (cellvalue, options, rowObject) { return parseFloat(options.rowData.priceOri) - parseFloat(options.rowData.discount);}格式化程序的代码在使用 loadonce: true 的情况下以相同的方式工作,即使您将返回数据的格式从 XML 更改为 JSON(例如为了提高代码的性能),它也会保持工作。 The demo 使用来自 GitHub 的免费 jqGrid 的最新代码并显示 它使用以下代码$(function () { "use strict"; function discountFmatter (cellvalue, options, rowObject) { return parseFloat(options.rowData.priceOri) - parseFloat(options.rowData.discount); } $("#grid").jqGrid({ url: "prime.xml", datatype: "xml", colModel: [ { name: "priceOri", xmlmap: "[priceOri]" }, { name: "discount", xmlmap: "[discount]" }, { name: "price", xmlmap: "[price]", editable: true, formatter: discountFmatter } ], cmTemplate: { width: 60, align: "center" }, iconSet: "fontAwesome", xmlReader: { root: "root", row: "item", repeatitems: false }, loadonce: true, viewrecords: true, rownumbers: true, caption: "Stack Overflow Example" });});关于jquery - 如何从 JqGrid 中单元格的自定义格式化程序访问其他行数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34268073/
10-15 01:57