user1042514
user1042514

Reputation: 39

jQueryGrid for XML representation of List<Map>

I have XML that looks like this:

<executionResults>
    <results>
        <items>
        <item>
            <entries>
                <entry>
                    <key>name</key>
                    <value>user1</value>
                </entry>
                <entry>
                    <key>id</key>
                    <value>id1</value>
                </entry>
            </entries>
        </item>
        <item>
            <entries>
                <entry>
                    <key>name</key>
                    <value>user2</value>
                </entry>
                <entry>
                    <key>id</key>
                    <value>id2</value>
                </entry>
            </entries>
        </item>
    </items>
    </results>
</executionResults>

And I want to put it into a jQuery grid. I've tried read this: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data

And tried this:

function readIntoGrid(xmlData)
{
    $('#dataGrid').jqGrid({
    datastr: xmlData,
    datatype: 'xmlstring',
    colNames:['Key', 'Value'],
    colModel:[
      {name:'key', index:'entry->key', width:120},
      {name:'value', index:'entry->value', width:120, hidden:false}
    ],
    xmlReader:
    {
        root: 'items',
        row: 'item->entries',
        repeatitems: false
    },
    height:'100%',
    sortable:false,
    caption:'List of Maps test'
  });

}

But nothing shows up in the grid. How does the javascript need to be modified to show the data in the grid?

Upvotes: 0

Views: 826

Answers (2)

Suma
Suma

Reputation: 11

I assume the xml given above is slightly different, meaning the "key"s should be same for all "item"s(They will be columns. so column should repeat for each "item"). Something like the following:

The following jquery works for the xml given above.

function readIntoGrid(xmlData)
{
    $('#dataGrid1').jqGrid({
    datastr: xmlData,
    datatype: 'xmlstring',
    colNames:['Second', 'First'],
    colModel:[
        {name:'value',   width: 150 },  
        {name:'value', width: 150 },
    ],
    xmlReader:
    {
        root: 'items',
        row: 'item',
        repeatitems: true,
        cell:"value"
    },
    height:'100%',
    pager: '#pager3',
    rowNum: 10,
    sortable:false,
    caption:'Search results',
    viewrecords: true,
    page: 1
    });
}

Look at the repeateitems and cell values in xmlreader.

Upvotes: 1

Oleg
Oleg

Reputation: 221997

There are syntax error in the row property of xmlReader. Instead of row: 'item->entries' you should use row: 'item>entries'. You can (but not muss) also modify root: 'items' to root: 'executionResults>results>items'.

The syntax of index properties which you use (index:'entry->key' and index:'entry->value') seems also wrong.

See the demo.

Upvotes: 2

Related Questions