PDA

View Full Version : XML rows within rows



deitch
23 Aug 2007, 8:57 AM
Does anyone know how you can handle rows within rows in XMLReader? Let us assume we have 3 records, and each record can have an arbitrary number of elements in it. This is perfectly legit XML, but I cannot get it to work with XMLReader. Use the analogy of cards in a phonebook. There are 3 cards, and each card can have one or more phone numbers on it.



<cards>
<card>
<name>John</name>
<phone location="home">555-1212</phone>
<phone location="work">555-1313</phone>
</card>
<card>
<name>Jack</name>
<phone location="home">555-2222</phone>
<phone location="work">555-2323</phone>
<phone location="myname">555-3456</phone>
</card>
<card>
<name>Jill</name>
<phone location="home">555-7777</phone>
</card>
</cards>


Visually, this would work with a grid of one column on the left, where the person selects a card. When a card is selected, rowselect in the RowSelectionModel fires to let us know which card is selected, and we can update a separate grid on the right. I am at a loss as to how to retrieve the data for the <phone> entries, though.



// this will not work because <phone> is not of any known type
var Card = Ext.data.Record.create([
{name: 'title'},
{name: 'phone'}
]);

datastore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'cardfile.xml'}),
reader: xmlReader = new Ext.data.XmlReader({
record: 'card'
}, Card)
});

var sm = new Ext.grid.RowSelectionModel();
sm.on('rowselect',
function(model,index, card){
updateCardDetail(card);
});

// and now I am stuck - how to get the <phone> entries from the card?


Thanks.
Avi

deitch
23 Aug 2007, 8:58 AM
Sorry, the record should show {name: 'name'}. It does in the actual code.

Animal
23 Aug 2007, 11:36 PM
Your Record definition needs a mapping property to explain from which element within the <card> element to draw the data. By default, it uses the field name. 'phone' is ambiguous, it returns 3 elements.

http://extjs.com/deploy/ext/docs/output/Ext.data.Record.html#create

You will need to use the correct DomQuery (see docs) path to specify where to get the data from.