PDA

View Full Version : How to define XML Reader Recordtypes/Fields at runtime?



saadkhan
6 Mar 2010, 8:26 PM
Hello,
I need to configure my grid at runtime. For that, I need to build up my XMLReader at runtime so that I could dynamically add columns in grid as well. I dont know how to add fields/record tpyes in xmlreader of my store. Please guide me how to do it?

ZeusTheTrueGod
6 Mar 2010, 11:59 PM
"DO MY JOB, PLEASE". I can offer you 25 usd per hour consultancy :).

Try to do something, write simple code , show your progress to us. May be someone will help you for free. But they will not work on you for free

saadkhan
7 Mar 2010, 3:26 AM
Trying to configure store fields from outside Ext.onready:


function RecieveStatusXML(str) {
var statusXml = StringToXml(strrr);

var statusStore = Ext.StoreMgr.get('statusStore');
var childs = statusXml.documentElement.childNodes.item(0).childNodes;
var recData = [];
for(var x=0; x < childs.length; x++)
{
//recData.push(new Ext.data.Field({name: "'" + childs.item(x).tagName + "State'", mapping: "'" + childs.item(x).tagName + " > @State'" }));
//recData.push(new Ext.data.Field({name: "'" + childs.item(x).tagName + "Description'", mapping: "'" + childs.item(x).tagName + " > @Description'" }));
var TopicRecord = Ext.data.Record.create(
[
{name: "'" + childs.item(x).tagName + "Description'", mapping: "'" + childs.item(x).tagName + " > @Description'" },
{name: "'" + childs.item(x).tagName + "State'", mapping: "'" + childs.item(x).tagName + " > @State'" }
]
);
var myNewRecord = new TopicRecord(
{
title: childs.item(x).tagName,
state: childs.item(x).attributes[0].nodeValue,
description: childs.item(x).attributes[1].nodeValue
}
);
recData.push(myNewRecord);
}
statusStore.add(recData);
//statusStore.fields = recData;
//statusStore.load();
}


Grid:


var statusGrid = new Ext.grid.GridPanel({
id: 'statusGrid',
title: 'Status',
store: statusStore,
hideHeaders: true,
trackMouseOver: false,
autoWidth: false,
columns:
[
{id: '_dateAndTime', header: "DateAndTime", dataIndex: 'DateAndTime', renderer: statusRenderer}
],
viewConfig:
{
forceFit: true
},
autoExpandColumn: '_dateAndTime'
});

function statusRenderer(value, p, r)
{
var arr = [];
var statusStore = Ext.StoreMgr.get('statusStore');
statusStore.each(function(record)
{
arr.push(record.data.title, '<br />', record.data.state, '<br />', record.data.description, '<br /><br />');
});
return arr.join('');
}


My Store:


var statusStore = new Ext.data.Store({
storeId: 'statusStore',
proxy: new Ext.data.MemoryProxy(''),
reader: new Ext.data.XmlReader(
{
record: 'Status'
},
[
{name: 'DateAndTime', mapping: '@DateAndTime'}
]
)
});


And my XML:


<Statuses><Status DateAndTime='date'> <Door State='1' Description='desc' /><Acc State='2' Description='sadasd sad r' /><St State='0' Description='lpc' /> </Status></Statuses>


Now will you try to 'help' me???? :)

fay
7 Mar 2010, 3:45 AM
This might help get you started: http://www.extjs.com/forum/showthread.php?t=4548

Animal
7 Mar 2010, 4:23 AM
Think a bit more about what you are doing.

You want to create ONE record definition.

Don't you?

So, you only need to interrogate ONE "Status" element.

So there's a start.



var dataRec = Ext.DomQuery.selectNode(myXmlDocument, "Status");


What you ware wanting to create by iterating through that is an Array of Field definition objects. Containing named and mapping. Maybe type if you know it.

Then you want to use that Array to create a record definition. Use DomQuery.

You must learn to analyze problems clearly.

steffenk
7 Mar 2010, 4:28 AM
I use the override from condor and it works perfect

http://www.extjs.com/forum/showthread.php?t=53009

Animal
7 Mar 2010, 4:54 AM
I think the OP is wanting to create a Record definition by interrogating the first data item.

But you're right, he'll probably want to create a column model fro the same information too.

saadkhan
7 Mar 2010, 5:53 AM
Well, the main problem is that I dont know the elements inside <Status> tag. So I thought of adding record type at run time into my xml reader. And as far as my columns are concerned, so I just need a single column and use a XTemplate in order to show all the unknown elements (inside <Status>) data....
So, maybe I am wrong to iterate through my XML each time I get it....So what you suggest now? Guide me, I will try my best and show it to you!!!!!

Animal
7 Mar 2010, 6:23 AM
Iterate through the child items of the Status element.

saadkhan
7 Mar 2010, 6:31 AM
I have already done that.....what should I do after iterating all through those child elements? How can I add record fields in my store`s xmlreader?

Animal
7 Mar 2010, 12:27 PM
Be a javascript programmer!

Iterate through the child elements, examining them, and push field definition objects onto an Array.

When you're done, create a record definition from that Array.

Come on! think about it.

saadkhan
12 Mar 2010, 7:27 AM
Hello Animal,
Now I have done what you told me to do (i hope so)....But data showing is redundant. I mean if <Statuses> tag would have 10 elements inside it, then data of each of all those 10 elements are shown 10 times. Here is the code:



function RecieveStatusXML(str) {
var statusXml = StringToXml(strrr);

var statusStore = Ext.StoreMgr.get('statusStore');
var childs = statusXml.documentElement.childNodes.item(0).childNodes;
var recData = [];
for(var x=0; x < childs.length; x++)
{
var TopicRecord = Ext.data.Record.create(
[
{name: "'" + childs.item(x).tagName + "Description'", mapping: "'" + childs.item(x).tagName + " > @Description'" },
{name: "'" + childs.item(x).tagName + "State'", mapping: "'" + childs.item(x).tagName + " > @State'" }
]
);
var myNewRecord = new TopicRecord(
{
title: childs.item(x).tagName,
state: childs.item(x).attributes[0].nodeValue,
description: childs.item(x).attributes[1].nodeValue
}
);
recData.push(myNewRecord);
}
statusStore.add(recData);
}


What am I missing over here?

saadkhan
12 Mar 2010, 7:34 AM
Bingooooooooooooooooo........it was my mistake. I was iterating through all the elements using each method to render them.
Now its working. Thanks for your push :)