Dojo datagrid - Retrieve value for field of each column.
I'm currently using a dojo datagrid and I want to be开发者_Python百科 able to loop through a list containing all the value for each field of the grid. For example say my grid has these columns:
<th field="name" width="200px">Name</th>
<th field="description" width="200px">Description</th>
<th field="type" width="200px">Type</th>
How do I get a list of all the field values? With this example, the list should be [name, description, type]. Thanks for any help!
Let's try this.
First, you need to know the ID of the dijit. If it's autogenerated (because you are declaring your DataGrid in HTML), you may need to use dojo.query
to find the DOM node, and use dijit.byNode(node)
to locate the dijit. The example below assumes you know the ID.
var fields = [];
var getFields = function(object) {
// recurse to handle stacked rows
if(dojo.isArray(object)) {
dojo.forEach(object, function(o) {
getFields(o);
});
} else if(object.field) {
fields.push(object.field);
} else if(object.cells) {
getFields(object.cells);
}
};
var structure = dijit.byId("dojox_grid_DataGrid_0").structure;
getFields(structure);
console.log(fields);
This will recursively process the structure of the Grid, looking for any object with a field
property.
Update I had to add in a check for object.cells
for declarative grids.
精彩评论