我有如下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
部分。所以基本上我想访问同一行中的其他单元格值。我怎样才能做到这一点。
我在下面使用了代码片段,但它们的结果是 undefined
rowObject[0] // undefined
rowObject.priceOri //undefined
谁能告诉我实现此目标的步骤。
更新:我有Tony Tomov的JqGrid版本4.4.0。由于我正在开发一个已开发的应用程序,因此无法更改或更新该库的版本。因此,我必须使用相同的JqGrid版本4.4.0进行操作。
似乎Oleg'srowObject instanceof Element ? $(rowObject).attr("priceOri") : rowObject.priceOri
正在满足此要求。
更新2 :(因为rowObject.rewards
在以下情况下将无法工作)
新的XML具有以下更改的格式,
<item priceOri =“ 100”折扣=“ 10”奖励=“ 20”> </ item>
所以新的格式化程序会像
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
}
请注意,我没有rewards
在jQgrid表中显示该值。所以我怎么能做到这一点。
奥列格的答案: 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
在自定义格式化程序内部处理数据时存在一个重要问题。的格式与rowObject
输入数据的项目相同。因此,必须使用rowObject[0]
还是rowObject.priceOri
处理JSON数据取决于是否repeatitems: false
使用过。以同样的方式,自定义格式器的代码应该是另一种处理JSON数据的代码,该数据使用下一个节点或属性。一个必须使用$(rowObject).find(">priceOri")
或$(rowObject).attr("priceOri")
(最后一个对应于您的数据格式)。
如果另外使用,则事件(为了理解)必须是更复杂的事件loadonce: true
。仅在处理从服务器加载的数据时,才必须使用上述表达式。接下来的处理(在本地排序,分页或过滤之后)必须使用rowObject.priceOri
符号。
在Tony更改了jqGrid的许可协议(请参阅帖子),将产品商业化(请参阅价格)并将jqGrid重命名为“ Guriddo jqGrid JS”之后不久,我开发了jqGrid的免费jqGrid fork 。自从免费jqGrid开发大约一年以来,我实现了许多功能(从Wiki中描述了最多的功能,每个发布的版本都有自述)。
免费的jqGrid保留了rowObject
与以前相同的参数格式,以保持与先前版本的兼容性,但是参数option
已扩展。它包含rowData
具有已解析数据的属性。要访问priceOri
免费jqGrid中的自定义格式器,可以使用options.rowData.priceOri
。因此您可以使用
function discountFmatter (cellvalue, options, rowObject) {
return parseFloat(options.rowData.priceOri) -
parseFloat(options.rowData.discount);
}
格式化程序的代码在使用时的loadonce: true
工作方式相同,即使您将返回数据的格式从XML更改为JSON(例如,以提高代码的性能),它也将保持工作状态。
该演示使用来自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"
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句