Clay Banks
Clay Banks

Reputation: 4581

Get all records from a selected row - Extjs

So I have the controller here to return records of a clicked row:

...
this.control({
             'mylist': {
                cellclick: function(view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                        var id = record.get('ID');
                        var name = record.get('NAME');
                        var desc = record.get('DESC');
                        var view = record.get('VIEW');
...

How can I instead iterate through mylist dynamically to get all the fields and assign a variable to each field(as the number of columns may increase in the future)?

Cheers!

Upvotes: 0

Views: 1009

Answers (1)

drembert
drembert

Reputation: 1246

If you are looking to loop through only the fields of the record that are mapped to defined columns you could do the following:

cellclick: function(view, td, cellIndex, record, tr, rowIndex, e, eOpts) {

  var i,
      columns = view.panel.columns, 
      currentRecordKey,
      newRecord = {};

   for(i=0; i < columns.length; i++){

        currentRecordKey = columns[i].dataIndex;
        newRecord[currentRecordKey] = record.get(currentRecordKey)
   }

   //do something with new record
}

While this solution does not create a var for ever key it does create a map containing all values of the current record which serves a similar purpose. For example, you could simply use newRecord.id to get the id field for the record in your example and it would serve the same purpose as a var declaration because the statement would fail if that field (key in the map) didn't actually exist. Keep in mind that this is not much different than simply using the actual record itself minus some of the extra fields from the record object.

Upvotes: 1

Related Questions