2

I have a grid, which has lots of columns which are almost identical, they have the same names, but with a different number on the end. They use the same renderer functions, but with a different parameter value, and so on...

These column definitions are relatively long (5-10 lines). Is there a way to generate them threw a loop or something? It would make my code nicer and a lot more compact.

Thanks in advance!

Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Zuller
  • 177
  • 2
  • 11

1 Answers1

2

Yes, you can. You can also generate your column configuration on the server.

Here can you see how you can do it:

Ext.define('mynamespace.Grid', {
    extend: 'Ext.grid.Panel'

    // ... your grid configuration 

    initComponent: function() {

       var cm = [];

       Ext.each(columnsArray, function(rec) {
           var col = {
               text: rec.name,
               dataIndex: rec.dataIndex
               // ... renderer and so on
           };

           cm.push(col);
       }, this);

       this.columns = {
           items: cm
       };

       this.callParent(arguments);
    }
});
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
  • Thanks a lot, worked perfectly. Did a minor edit for those who it is not obvious, that the this.callParent(arguments); is needed at the end. – Zuller Apr 28 '13 at 11:27