Login Register

Getting grid to work with dojo.data

I have a feeling I'm doing something very silly, but this is simply not working:

onLoad handler:

var view1 = {
		cells: [[
		{name: "Delete?", editor: dojox.grid.editors.bool},
		{name: "Name", field: "name"},
		{name: "Size", field: "size"},
		{name: "Uploaded", field: "dateAndTime"}
		]]
	};
	var grid = dijit.byId("fileGrid");
	grid.setStructure([view1]);
	grid.refresh();

The headers show up properly, and I get one row which is all ?s. But I am retrieving the data properly, according to my server logs:

{"items":[{"dateAndTime":"2007/12/14 00:34:13","data":{},"size":400,"name":"file0"},
{"dateAndTime":"2007/12/14 01:34:13","data":{},"size":1000,"name":"file1"},
{"dateAndTime":"2007/12/14 02:34:13","data":{},"size":3000,"name":"file2"},
{"dateAndTime":"2007/12/14 03:34:13","data":{},"size":5000,"name":"file3"},
{"dateAndTime":"2007/12/14 04:34:13","data":{},"size":400,"name":"file4"}],
"label":"name","identifier":"name"}

So what am I missing here? Where's my data? ;_;

Try a defined width/height

Try a defined width/height for the Grid (or its parentNode) in px/em, rather than %.

ItemFileReadStore doesn't like inner objects

It's the "data" that breaks your data. Try removing the "data" property from your data or change it to a string. It seems like ItemFileReadStore can't handle objects inside row items. There was another similar case recently: Dojo Grid+CakePHP.

I filed a bug for this: dojo.data.ItemFileReadStore breaks when items contain objects. Plus, there's another bug filed about a similar issue: ItemFileReadStore don't accept empty arrays as attribute values

That worked!

Thanks very much for your help! 8-)

Inner objects are items.

That is not a bug in ItemFile*Store. The design of ItemFile*Store is that inner objects inside an item are also treated as items. This was an intentional decision on the design of that particular store so that you could construct a 'Tree' view easily with ItemFile*Store without having to do everything via _reference formats and the like. It's also documented in the ItemFile*Store documentation. Second example, shows the construction of an Item hierarchy via inner objects.

http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...

As an aside...

There is a way to do child objects so that they're not converted to a generic 'item'. Use the typeMap parameter and define a syntax for your custom objects to be de-serialized. That way you can then deserialize that object as just raw JS object type and not an item, in such cases. There is an example of using the build in typeMap code for Date in the documentation.

http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...

For example, if you wanted to do a raw object that doesn't get converted to item format, you could do something like (pseudocode'ish):

dojo.declare("foo.ObjectWrapper", Object, {constructor: function(params){dojo.mixin(this, params};});
typeMap:{'foo.ObjectWrapper': {
   type: foo.ObjectWrapper,
   deserialize: function(value){
      return new foo.ObjectWrapper(value);
   }
}
var dataStore = new dojo.data.ItemFileReadStore({url: "some/url", typeMap: typeMap});

And then in your data set do something like:
items: [
   { name:'Africa', type:'continent', breaker: {_type: 'foo.ObjectWrapper' _value:{}} }
]

And You should get the behavior you expect, I believe. I haven't tried it, but I believe it should do what you wanted here.

Still about inner objects...

Alright, I was under the false impression that "children"-property was intended to have some (_reference-like) special meaning. How about this issue with the grid not rendering? The behavior with these inner objects is somewhat unexpected. Shall I file the same bug for Grid (model) then?

Grid content doesn't render when store items contain objects

Opened a new issue about the original problem: Grid content doesn't render when store items contain objects.

The problem appears to

The problem appears to happen at step 4 in the ItemFileReadStore. I realize this is not the solution, but by adding the indicated continue statement, the ItemFileReadStore seems to properly complete the processing when a row contains an object. Otherwise, the process appears to exit and not complete the processing of the row. I'm not sure what the ramifications are, however, of the added line for other types of items. Is there a better solution?

// Step 4: Some data files specify an optional 'identifier', which is
// the name of an attribute that holds the identity of each item.
// If this data file specified an identifier attribute, then build a
// hash table of items keyed by the identity of the items.
var arrayOfValues;

var identifier = dataObject.identifier;
if(identifier){
this._itemsByIdentity = {};
this._features['dojo.data.api.Identity'] = identifier;
for(i = 0; i < this._arrayOfAllItems.length; ++i){
item = this._arrayOfAllItems[i];
arrayOfValues = item[identifier];

if(!arrayOfValues) continue; // ***** added by bortner

var identity = arrayOfValues[0];
if(!this._itemsByIdentity[identity]){
this._itemsByIdentity[identity] = item;
}else{
if(this._jsonFileUrl){
throw new Error("dojo.data.ItemFileReadStore: The json data as specified by: [" + this._jsonFileUrl + "] is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]");
}else if(this._jsonData){
throw new Error("dojo.data.ItemFileReadStore: The json data provided by the creation arguments is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]");
}
}
}

That seems to be unconnected

That seems to be unconnected to this problem... it looks like you're checking for an object's identifier. This might help if one item doesn't have an identifier, but if you happen to be including an object that *does* have an identifier, the problem with grid would still occur.

I know "seems" to work is a poor response

Perhaps it is only a symptom. However, when I add the line, the Grid will render with an object in the row. If I remove the line, nothing from the rows will render. It may have simply been a coincidence. What I saw was that

1. when the extra line was "not" included
2. AND and an object was present in the rows
3. Then the _arrayOfAllItems and _arrayOfTopLevelItems contained data AND the grid failed to render.

1. When the line "was" included
2. AND an object was present in the rows
3. Then the _arrayOfAllItems and _arrayOfTopLevelItems were both empty AND the grid DID render.

I "believe" (it's been over a month) that without the extra line/continue that a null caused the step 4 to cease.

In any case, it may be a mute point. If you use the "dojox.data.QueryReadStore" and just point the URL attribute to your file.json it works fine with or without objects contained in the rows.

Then, for the Grid you can simply use a formatter to extract the data from the inner objects.

(with div tags removed)
dojoType="dojox.data.QueryReadStore" jsId="jsonStore" url="products.json"

dojoType="dojox.grid.data.DojoData" jsId="model"
rowsPerPage="20" store="jsonStore" query="{ SerialNumber: '*' }" clientSort="true"

id="grid" dojoType="chkp.ProductGrid" model="model" structure="layout"

I'm removing my extra line from the ItemFileReadStore to keep the code standard and use the QueryReadStore instead.

I hope that helps anyone else who came up against this problem.