Search code examples
javascriptextjsdatagridextjs4extjs-grid

ExtJs grid similar columns created more simply


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!


Solution

  • 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);
        }
    });