I have modified the example available at http://dojotoolkit.org/reference-guide/1.9/dojox/grid/LazyTreeGrid.html to make it work with QueryReadStore which fetches data from a backend service in JSON format.
My sample service returns data in the following format:
{"identifier":"id","label":"name","items":[{"name":"Africa","id":1,"type":"CONTINENT","population":null,"area":null,"hasChildren":true},{"name":"Asia","id":8,"type":"CONTINENT","population":null,"area":null,"hasChildren":true}]}
Note that unlike the ItemFileWriteStore example, the children element are not populated inline.
Now the Web page code :
// Dependencies
dojo.require("dojox.grid.LazyTreeGrid");
dojo.require("dojox.grid.LazyTreeGridStoreModel");
dojo.require("dojox.data.QueryReadStore");
.
.
.
// Use QueryReadStore instead of ItemFileWriteStore.
// The URL points to a service that returns root nodes of the tree in JSON format.
// When a node is expanded, the same URL gets called with an extra parameter
// called parentId=<id> where id is identifier of the clicked row. The service
// should return children of the specified id.
var store = new dojox.data.QueryReadStore({url: "./admin/getRootNodes.json",
requestMethod: "get" });
// hasChildren field in the JSON response should be boolean (true - has children, false otherwise)
var model = new dojox.grid.LazyTreeGridStoreModel({
store: store,
serverStore: true,
childrenAttrs: ['hasChildren']
});
/* set up layout */
var layout = [
{name: 'Name', field: 'name', width: '30%'},
{name: 'Type', field: 'type', width: '30%'},
{name: 'Area', field: 'area', width: '20%'},
{name: 'Population', field: 'population', width: '20%'}
];
/* create a new grid: */
var grid = new dojox.grid.LazyTreeGrid({
id: 'grid',
treeModel: model,
structure: layout,
rowSelector: '20px',
}, document.createElement('div'));
/* append the new grid to the div */
dojo.byId("gridDiv").appendChild(grid.domNode);
/* Call startup() to render the grid */
grid.startup();