如何在jqGrid中映射xml?

时间:2011-06-21 14:09:36

标签: jquery jqgrid

我正在使用jqGrid,我有一个给定的xml我希望映射到jqGrid

<list>
<com.abc.db.ConfigInfo>
<cfgId>83</cfgId>
<cfgName>test</cfgName>
<cfgDesc>test</cfgDesc>
<cfgType>test</cfgType>
<fileName>csmclientbenz.xml</fileName>
<absolutePath>../webapps/csm/files//1-105101/csmclientbenz.xml</absolutePath>
<emailAddress>abhishek@abc.com</emailAddress>
<projectId>1-105101</projectId>
<hostname>benz</hostname>
<createDate>2011-06-15 15:29:55.0 IST</createDate>
<updateDate>2011-06-15 15:29:55.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
</com.abc.db.ConfigInfo>
<com.abc.db.ConfigInfo>
<cfgId>102</cfgId>
<cfgName>cfgname1</cfgName>
<cfgDesc>test</cfgDesc>
<cfgType>test</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//1-105101/csmclientestilo.xml</absolutePath>
<emailAddress>abhishek@abc.com</emailAddress>
<projectId>1-105101</projectId>
<hostname>estilo</hostname>
<createDate>2011-06-20 18:26:03.0 IST</createDate>
<updateDate>2011-06-20 18:26:03.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
</com.abc.db.ConfigInfo>
</list>

我使用过类似的东西,但是请帮助我从这里开始,如何将xml XMLHttpRequest分配给jqGrid

var xml=client.responseText;
         $('#configDiv').empty();
            $('<div width="100%">')
            .attr('id','configDetailsGrid')
            .html('<table id="list1" width="100%"></table>'+
                    '<div id="gridpager"></div>'+
                '</div>')       
            .appendTo('#configDiv');    

            jQuery("#list1").jqGrid({
                datatype: "clientSide",
                height: 250,
                colNames:['cfgId','cfgName', 'cfgDesc', 'cfgType','absolutePath', 'updateDate', 'fileName'],
                colModel:[
                    {name:'cfgId',index:'cfgId', width:90, align:"right"},
                    {name:'cfgName',index:'cfgName', width:90, align:"right"},
                    {name:'cfgDesc',index:'cfgDesc', width:90, align:"right"},
                    {name:'cfgType',index:'cfgType', width:90, align:"right"},
                    {name:'absolutePath',index:'absolutePath', width:90, align:"right"},
                    {name:'updateDate',index:'updateDate', width:90, align:"right"},
                    {name:'fileName',index:'fileName', width:90, align:"right"},
                ],
                pagination:true,
                pager : '#gridpager',
                rowNum:10,
                scrollOffset:0,
                height: 'auto',
                autowidth:true,
                viewrecords: true,
                gridview: true,
 xmlReader: { 
                    root : "list",
                    row: "com.abc.db.ConfigInfo",
                    repeatitems: false,
                    id: "ASIN"
                    },
                edit:false,
                add:false,
                del:false

            });

稍后我还需要隐藏cfgidfilename。谢谢,请帮忙

1 个答案:

答案 0 :(得分:1)

你有特殊的性格'。' (点)在XML节点的名称中,所以你必须像这样转义字符:

row: "com\\.abc\\.db\\.ConfigInfo"

此外,您的数据具有节点而非属性,因此您无需在jqGrid读取数据之前转换数据。因此,以下代码将直接使用XML数据:

jQuery('#list').jqGrid({
  url: 'Ricky.xml',
  datatype: 'xml',
  colNames:['cfgId','cfgName', 'cfgDesc', 'cfgType','absolutePath', 'updateDate', 'fileName'],
  colModel:[
      {name:'cfgId',index:'cfgId', width:90, align:"right"},
      {name:'cfgName',index:'cfgName', width:90, align:"right"},
      {name:'cfgDesc',index:'cfgDesc', width:90, align:"right"},
      {name:'cfgType',index:'cfgType', width:90, align:"right"},
      {name:'absolutePath',index:'absolutePath', width:90, align:"right"},
      {name:'updateDate',index:'updateDate', width:90, align:"right"},
      {name:'fileName',index:'fileName', width:90, align:"right"},
  ],
  pager : '#pager',
  rowNum:10,
  scrollOffset:0,
  height: 'auto',
  autowidth:true,
  viewrecords: true,
  gridview: true,
  xmlReader: {
      root : "list",
      row: "com\\.abc\\.db\\.ConfigInfo",
      repeatitems: false
  }
});

查看工作demo