treebear
treebear

Reputation: 325

Dojo datagrid - Retrieve value for field of each column.

I'm currently using a dojo datagrid and I want to be 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!

Upvotes: 1

Views: 2552

Answers (1)

OverZealous
OverZealous

Reputation: 39560

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.

Upvotes: 2

Related Questions