如何将另一个节点内的xml节点的属性映射到jqgrid中的列

时间:2013-02-26 13:02:41

标签: jqgrid xml-parsing

下面是我要映射到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应该如何将以下内容映射到列中?

  1. 生产标签下的销售订单
  2. “请求的投放详细信息”标记下的日期
  3. “请求的投放详细信息”标记下的数量

1 个答案:

答案 0 :(得分:1)

您可以为列xmlmapSalesOrderDate指定Quantity属性,并将xmlmap定义为函数。该函数将主要元素(我不确定<ProtoRequestInfo><ProtoRequest>)作为参数。您可以在xmlmap内获取所需的属性,并从函数xmlmap返回。

您可以在the answer中找到使用XML属性的示例(请参阅the demo)。

更新The demo显示了如何以您发布的格式读取XML数据。结果如下图所示

enter image description here

我在演示中使用了以下代码:

$("#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的实现。