Search code examples
extjsextjs3

Ext JS EditorGridPanel - How to split cell to show multiple rows


In Ext.grid.EditorGridPanel table how to split a cell to have two or three rows?

Like the below image

enter image description here


Solution

  • I managed to do a kind of rowspan instead of row splitting. IMO it's easier and it looks the same as grid on attached image. Example code:

    var grid = new Ext.grid.EditorGridPanel({
        [...],
    
        // hook up events
        initComponent: function () {
            Ext.grid.GridPanel.prototype.initComponent.call(this);
    
            this.getView().on('refresh', this.updateRowSpan, this);
            this.getView().on('rowupdated', this.updateRowSpan, this);
        },
    
        onLayout : function(vw, vh) {
            this.updateRowSpan();
        },
    
        // set span on rows
        updateRowSpan: function() {
            var columns = this.getColumnModel().config,
                view = this.getView(),
                store = this.getStore(),
                rowCount = store.getCount(),
    
                column = columns[0], // put propert column index here
                dataIndex = column.dataIndex,
    
                spanCell = null,
                spanCount = null;
                spanValue = null;
    
            for (var row = 0; row < rowCount; ++row) {
                var cell = view.getCell(row, 0),
                    record = store.getAt(row),
                    value = record.get(dataIndex);
    
                if (spanValue != value) {
                    if (spanCell !== null) {
                        this.setSpan(Ext.get(spanCell), spanCount);
                    }
    
                    spanCell = cell;
                    spanCount = 1;
                    spanValue = value;
                } else {
                    spanCount++;
                }
            }
    
            if (spanCell !== null) {
                this.setSpan(Ext.get(spanCell), spanCount);
            }
        },
    
        // set actual span on row
        setSpan: function(cell, count) {
            var view = this.getView(),
                innerCell = Ext.get(cell.down('*')),
                height = cell.getHeight(),
                width = cell.getWidth();
    
            cell.setStyle('position', 'relative');
            if (count == 1) {
                innerCell.setStyle('position', '');
                innerCell.setStyle('height', '');
                innerCell.setStyle('height', '');
            } else {
                innerCell.setStyle('position', 'absolute');
                innerCell.setStyle('height', (height * count - cell.getPadding('tb') - innerCell.getPadding('tb')) + 'px');
                innerCell.setStyle('width', (width - cell.getPadding('lr') - innerCell.getPadding('lr')) + 'px');
            }
        }
    });
    

    This code changes style of .x-grid3-cell-inner by applying position: absolute and big enough size to cover rows below. Notice that you must also apply some opaque background to make it work. Working sample: http://jsfiddle.net/RpxZ5/8/

    I first wrote code for Ext JS 4, if you interested, here is working sample: http://jsfiddle.net/wQSQM/3/