PDA

View Full Version : Dynamic XML to grid, store fine but no Grid?



petzlux
9 Sep 2009, 8:38 AM
I have a asp page which is returning XML records. I have no control over the format of the XML. Also, the fields per record returned will be variable. So I needed to write some parser to create for each returned XML the appropriate XMLReader and ColumnModel.

My code loads the XML fine into the store, as can be seen in Firebug, but the grid wont display any rows (even though the grid is linked fine to the store records). Whats going on here?

here is the XML:


<?xml version="1.0" encoding="UTF-8"?>
<data>
<item>
<cell name="ID" value="1287" />
<cell name="TYPE" value="residential" />
<cell name="NAME" value="rue des Romains" />
<cell name="Type (I)" value="Line" />
<cell name="Branches (I)" value="1" />
<cell name="Coordinates (I)" value="6" />
<cell name="X (I)" value="5.8725239" />
<cell name="Y (I)" value="49.5535016" />
<cell name="Longitude (I)" value="5.8725" />
<cell name="Latitude (I)" value="49.5535" />
<cell name="Length (I)" value="1.78076130538226E-03" />
<cell name="Area (I)" value="0" />
<cell name="Bearing (I)" value="54.4214533135259" />
<cell name="Selection Mask (I)" value="1" />
<cell name="Selection (I)" value="1" />
<cell name="Geom (I)" value="&lt;geom, line, branches: 1, coords: 6&gt;" />
<cell name="Version (I)" value="0" />
</item>
<item>
<cell name="ID" value="1337" />
<cell name="TYPE" value="residential" />
<cell name="NAME" value="rue des Romains" />
<cell name="ONEWAY" value="yes" />
<cell name="Type (I)" value="Line" />
<cell name="Branches (I)" value="1" />
<cell name="Coordinates (I)" value="7" />
<cell name="X (I)" value="5.87399655" />
<cell name="Y (I)" value="49.55483" />
<cell name="Longitude (I)" value="5.8740" />
<cell name="Latitude (I)" value="49.5548" />
<cell name="Length (I)" value="2.4594064539588E-03" />
<cell name="Area (I)" value="0" />
<cell name="Bearing (I)" value="205.602208011944" />
<cell name="Selection Mask (I)" value="1" />
<cell name="Selection (I)" value="1" />
<cell name="Geom (I)" value="&lt;geom, line, branches: 1, coords: 7&gt;" />
<cell name="Version (I)" value="0" />
</item>
</data>

And here is my ExtJS code:


Ext.onReady(function(){

store = new Ext.data.Store({
id: 'dataStore'
});

var colModel = new Ext.grid.ColumnModel({
columns: { header: "ID", width: 60, sortable: true},
defaults: {
sortable: true,
resizable: true,
width: 75
}
});

grid = new Ext.grid.GridPanel({
colModel: colModel,
store: store,
frame: true,
stripeRows: true,
height: 200,
renderTo: 'table'
});
Ext.Ajax.request({
url: 'test.xml',
success: function(response){
var xml = response.responseXML;
var dq = Ext.DomQuery;
var recordDef = new Array();
var columnDef = new Array();
var record = dq.select('item:first cell', xml);

for (var i = 0; i < record.length; i++) {
recordDef[i] = new Object();

recordDef[i].name = record[i].attributes[0].value;
recordDef[i].mapping = "cell[@name=\'" + record[i].attributes[0].value + "\']@value";

columnDef[i] = new Object();
columnDef[i].header = record[i].attributes[0].value;
columnDef[i].dataIndex = record[i].attributes[0].value;
columnDef[i].sortable = true;

var intrinsic = new RegExp("(I)");
if (intrinsic.test(record[i].attributes[0].value)) {
columnDef[i].hidden = true;
}
}

var colModel = new Ext.grid.ColumnModel({
columns: columnDef,
defaults: {
sortable: true,
resizable: true,
width: 75
}
});

grid.colModel.columns = columnDef;

var XMLreader = new Ext.data.XmlReader({
record: 'item',
id: "cell[@name='ID']@value"
}, recordDef);

store.reader = XMLreader;
store.loadData(xml);
console.dir(store);
console.dir(grid);
},
failure: function(response){
Ext.Msg.Alert('Somethings gone wrong');
}
});

});