下面是我要映射到jqGrid中的列的XML:
<ProtoRequestInfo NPO = "102922">
<ProtoRequest
No = "84P6-11-00002"
Requestor = "Daniel Frank(E677648)"
CustomerName = "TLV BMW"
CustomerOrder = ""
MWO = "4601302"
PartNumber = "813818-0003"
ProductType = "CHRA"
CreationDate = "12-May-2011"
ABCClasification = "B - Durability testing / Production supplier-soft tooling"
ProtoStatus = "Closed"
UsageType = "Assembly Request"
BOMAvailabilityDate = "13-May-2011"
BOMCommitedDate = ""
Technology = "VNT Step3 REA"
Plant = "84P6-Thaon Les Vosges"
EstimatedBudget_USD = "0.00">
<Production
No = "102219281"
Status = "Closed"
SalesOrder = ""
<RequestedDeliveryDetails Date = "01-Aug-2011" Quantity = "48"/>
<AgreedDetails Date = "29-Sep-2011" Quantity = "48"/>
<EstimatedDetails Date = "24-Aug-2011" Quantity = "47.0"/>
<EstimatedDetails Date = "20-Sep-2011" Quantity = "1.0"/>
<Info>No Qty Shipped</Info>
</Production>
</ProtoRequest>
</ProtoRequestInfo>
colmodel应该如何将以下内容映射到列中?
答案 0 :(得分:1)
您可以为列xmlmap
,SalesOrder
和Date
指定Quantity
属性,并将xmlmap
定义为函数。该函数将主要元素(我不确定<ProtoRequestInfo>
或<ProtoRequest>
)作为参数。您可以在xmlmap
内获取所需的属性,并从函数xmlmap
返回。
您可以在the answer中找到使用XML属性的示例(请参阅the demo)。
更新:The demo显示了如何以您发布的格式读取XML数据。结果如下图所示
我在演示中使用了以下代码:
$("#list").jqGrid({
datatype: "xml",
url: "ReadAttrFromXml.xml",
gridview: true,
autoencode: true,
height: "auto",
rowNum: 10000, // no local paging
colModel: [
{name: "No", xmlmap: function (obj) {
return $(obj).attr("No");
}},
{name: "Requestor", width: 130, xmlmap: function (obj) {
return $(obj).attr("Requestor");
}},
{name: "CustomerName", width: 120, xmlmap: function (obj) {
return $(obj).attr("CustomerName");
}},
{name: "SalesOrder", xmlmap: function (obj) {
return $(obj).find(">Production").attr("SalesOrder");
}},
{name: "Date", formatter: "date", formatoptions: {srcformat: "d-M-Y"}, align: "center",
sorttype: "date",
xmlmap: function (obj) {
return $(obj).find(">Production>RequestedDeliveryDetails").attr("Date");
}},
{name: "Quantity", formatter: "integer", sorttype: "integer", align: "right",
xmlmap: function (obj) {
return $(obj).find(">Production>RequestedDeliveryDetails").attr("Quantity");
}}
],
cmTemplate: {width: 100},
xmlReader: {
root: "ProtoRequestInfo",
row: "ProtoRequest",
repeatitems: false,
id: "[No]"
}
});
最重要的是xmlmap
函数和xmlReader
的实现。