I have to create a grid from an xml. The problem is that the coloumns have to be create dynamically from the xml (because depending on the request, the xml result can be different). for example given this xml:
<population>1 341 990 000</population>
<population>237 556 363</population>
the result should be:
|country |population |year|
|China |1 341 990 000|2011|
|Indonesia |237 556 363 |2011|
there is any way to do it directly from the component? in case of No how can I manage it?
Thanks in advance for your help
There is no built-in initialization from the xml, but it possible to do the next
a) load data by dhx.ajax
b) now you have access to the server response ( both as plain text and as json object ) and can get some of that info and use for grid’s configuration
c) second part of data can be used in parse command to load the data
Something like next ( beware of typos )
var resp = dhx.DataDriver.json.toObject(text);
first of all thanks for your answer. But I have some problem in implementig what you said. I’m using XML for data representation, so I defined the grid component as:
when the component is initialized I call the method you said in your answer:
var resp = dhx.DataDriver.xml.toObject(text,xml);
inspecting the resp variable with firebug (I see that this variable is a Document object) I do not see the representation of the xml document (the link http:\192… work perfectly and return an xml as showed in the first post), where can I find a specification for the Document object?
The other function:
the docs specification for the component grid says that “header” shows/hides grid header but I should create the fields for the component so the first parameter of the $$(‘ui_grid’).define should be “field”. Seeing that the field name has the same name of the xml tags, I need to get them from the resp variable (an array of all the tags in the Xml document). How can I do that?
Thanks again for your help
Sorry for inconvenience, I have provided the sample with json which was misleading .
toObject for xml data type returns an XML Document object
This is common XML object, not a dhtmlx component|wrapper. You can use W3C DOM api to access its nodes and attributes
var driver = dhx.DataDriver.xml;
var xml = driver.toObject(text, xml);
var top_node = driver.xpath("//nodename");
var json_object = diver.tagToObject(top_node);
//here you will have json object with all properties of xml
//you can use xpath with //topnode/fieldnode - to select the necessary branch in xml
//after converting to json - you can feed result to the define or parse commands