0

I am using jqGrid, I have a given xml which i want to be mapped to 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>

I used something like this, but please help me move from here, how shall i assign xml which comes from XMLHttpRequest to 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

            });

Later I also need to hide cfgid and filename. Thanks please help

1 Answers1

1

You have special character '.' (point) in the name of XML node, so you have to escape the character like so:

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

Moreover you data has nodes and not attributes, so you don't need convert the data before it can be read by jqGrid. So the following code will use the XML data directly:

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
  }
});

See the working demo.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg: my xml is my `xml` variable so i should read it using `data: xml, datatype: "clientSide",` –  Jun 22 '11 at 04:36
  • @Oleg: Also I need two hidden columns `cfgid` and `filename` shall i just put hidden in colModel? so that on row click i'd like to retrieve all the values of a particular row, even the hidden ones –  Jun 22 '11 at 04:38
  • 1
    @Ricky: I wrote you before about different methods to hide the columns. You can use `hidden:true` property for `filename` column and remove `cfgid` column from the grid. To use `cfgid` as a rowid you should add `id: "cfgId"` inside of `xmlReader`. See [the modified demo](http://www.ok-soft-gmbh.com/jqGrid/Ricky1.htm). – Oleg Jun 22 '11 at 04:52
  • @Oleg: Sorry for my repeated questions :) I am getting a response as xml which is getting stored in a `xml` variable, how can i use this data? I used `data: xml, datatype: "clientSide", ` but does not take affect, it just shows me headers –  Jun 22 '11 at 04:55
  • 1
    @Ricky: One more remark. If you get the contain of XML data in the `xml` variable only to forward it to jqGrid you don't need to do this and allow jqGrid to read the data itself. If you **really** construct the data in `xml` variable of need it for another purpose you can use `datatype: 'xmlstring'` and `datastr : xml`. The ``datatype: 'xmlstring'`` supports not only the data in the string form (`xml` variable), but also the `XMLDocument` also. – Oleg Jun 22 '11 at 05:00
  • 1
    @Oleg: Thanks it works now, The reason why i am assigning my response to xml is because from my login page i send session token which is used by the current page's onLoad to send sessiontoken to server side and recieve `xml` as response. so i had to save the response before. Your answers helped me a lot to understand jqGrid. Thanks :) –  Jun 22 '11 at 05:08