I am trying to create a Dojo dojox.grid.DataGrid programmatically. It is getting created but does not show the output, instead it shows an error with the message: “Sorry, an error occurred”
Here is the code:
var info=[
{
"Propery":"key1","Value":"value1"},
{
"Property":"key2", "Value":"value2"},
{
"Property":"key3","Value":"value3"}
];
var layout = [
{
name: "Property",
field: 'Property',
width: "180px"},
{
name: "Value",
field: 'Value',
width: "180px"}
];
var myStore = new dojo.data.ItemFileReadStore({data:{items:info}});
var grid = new dojox.grid.DataGrid({
store: myStore,
structure: layout
},"myDiv");
grid.startup();
I don’t know where it is going wrong, please help me out.
Dude, this is like the third time I answer a question of yours. It strongly reminds me of the two before, but i’ll have a try anyway.
Your example can’t work, because in your
layout-variable, you refer to afieldnamed ‘property’ which doesn’t exist indata.items(info).You also did’t get the concept of the
json–arrayright. It schould represent like a bunch of object with a very similar structure. Assume you want to store some people, then you would have somekeyslike firstname, lastname, age, gender etc. Each person would have different values on this. Thejson–arraywould look like that:Note, that the names of the
propertiesare similar, just the values differ.What you try to do obviously is, to store one single
objectas anarray, and make each property anobject. Think about it:[]meansarray,{}meansobjectin JavaScript.In fact, your
info-variable should look like this:and if you would like to store some more object it would look like this:
…
objects stored in anarray. Simple as that.Now to your
layout var:It should discribe the
structureof yourDataGridin relation to the data given by itsstore. So it descripes the columns as a whole, not the single cells.You can therefore only refer to the property-names (key1, key2, key3, or firstname, lastname, age, gender) but not to the values, as the may be different sometimes. You only know the structure of your objects, not its actual content.
Therefore, it should look like that: