1


I'm facing an issue. I'm building an EXTJ 6.2 modern app with Sencha architect 4.1. I'm using the grid component in my panel with a server loaded store. I'd like to color rows according to the data I have.

In classic, this is doable with

viewConfig: {
  forceFit: true,  
  getRowClass: function(record, rowIndex, p, store) { 
     //some if statement here  
}

I tried this in modern but it doesn't work. Does anyone know of another way or a hack that I could do color the rows? Or at best at least change the one-color background.

I'd really like to avoid using the list component if possible.

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
Aghi
  • 119
  • 3
  • 13

3 Answers3

2

In modern you can use itemConfig to configure Ext.grid.Row.

Add the code bellow to a Sencha Fiddle:

 Ext.application({
name : 'Fiddle',

launch : function() {
    var store = Ext.create('Ext.data.Store', {
fields: ['name', 'email', 'phone'],
data: [
    { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224", color: "blue"  },
    { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234", color: "green" },
    { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244", color: "yellow" },
    { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254", color: "red" }
]
});

Ext.create('Ext.grid.Grid', {
title: 'Simpsons',

variableHeights: true,

store: store,

itemConfig: {
    listeners: {
        painted: function (row) {
            var record = row.getRecord();
            var color = record.get('color');

            row.setStyle('background: '+color)

            //if (color == 'red')
                //row.setStyle('background: red');
        }
    }
},

columns: [
    { 
        text: 'Name',  
        dataIndex: 'name', 
        minWidth: 200, 
        //flex: 1,
        //cellWrap: true,
        cell: {
            bodyStyle: {
                whiteSpace: 'normal'
            }
        }
    },
    { 
        text: 'Email', 
        dataIndex: 'email', 
        flex: 2, 
        minWidth: 250 

    },
    { 
        text: 'Phone', 
        dataIndex: 'phone', 
        flex: 1,  
        minWidth: 120
    },
    {
        text: 'Color',
        dataIndex: 'color',
        flex: 1
    }
],

//height: 200,
//layout: 'fit',
fullscreen: true
});
}
});

the itemConfig part is what will do the trick.

After @Gwynge's comment i've created another example setting the color to each cell using the renderer config:

Ext.application({
name : 'Fiddle',

launch : function() {
    var store = Ext.create('Ext.data.Store', {
fields: ['name', 'email', 'phone'],
data: [
    { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224", color: "blue"  },
    { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234", color: "green" },
    { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244", color: "yellow" },
    { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254", color: "red" }
]
});

Ext.create('Ext.grid.Grid', {
title: 'Simpsons',

variableHeights: true,

store: store,

columns: [
    { 
        text: 'Name',  
        dataIndex: 'name', 
        minWidth: 200, 
        //flex: 1,
        //cellWrap: true,
        cell: {
            bodyStyle: {
                whiteSpace: 'normal'
            }
        },
        renderer: function(value, record, dataIndex, cell) {
           cell.setStyle('background: '+record.get('color')+';')     
           return value;
        }
    },
    { 
        text: 'Email', 
        dataIndex: 'email', 
        flex: 2, 
        minWidth: 250,
        renderer: function(value, record, dataIndex, cell) {
           cell.setStyle('background: '+record.get('color')+';')     
           return value;
        }
    },
    { 
        text: 'Phone', 
        dataIndex: 'phone', 
        flex: 1,  
        minWidth: 120,
        renderer: function(value, record, dataIndex, cell) {
           cell.setStyle('background: '+record.get('color')+';')     
           return value;
        }
    },
    {
        text: 'Color',
        dataIndex: 'color',
        flex: 1,
        renderer: function(value, record, dataIndex, cell) {
           cell.setStyle('background: '+record.get('color')+';')     
           return value;
        }
    }
],

//height: 200,
//layout: 'fit',
fullscreen: true
});
}
});

I hope this will help.

Zoti
  • 822
  • 11
  • 31
  • 2
    This doesn't reliably work. If you change the data (so for example setting a new store) it does not call the painted event for existing rows - if your new store had 6 rows then it would only call painted for the additional 2 rows - the existing 4 are left the same even though the data has changed – Gwynge Apr 24 '19 at 15:46
  • I haven't tested it with the scenarios you mentioned. can you provide a fiddle? – Zoti Apr 25 '19 at 07:36
  • @Gwynge i've created another example using `renderer`. Can you test it? Cheers – Zoti Apr 25 '19 at 07:49
  • That does work - if you need to set colour by column then it's useful - if you want to set on a row a basis then see this thread here where a simple solution has been provided (but I've still got a question about it where the colours are changed) https://www.sencha.com/forum/showthread.php?471702-Modern-grid-change-row-colours&p=1324574#post1324574 – Gwynge Apr 25 '19 at 12:33
  • since the color is being setted according to the record, the color is being passed through the entire row. – Zoti Apr 26 '19 at 12:26
0

To color a row, the following code couldn't work in my project

itemConfig: {
    listeners: {
        painted: function(row) {
            var record = row.getRecord();
        }
    }
}

row.getRecord doesn't work (getRecord() is not recognized as function)

I succeed to color a row from a cell

columns: [{
    text: 'Name',
    dataIndex: 'name',
    width: 150,
    sortable: true,
    renderer: function(v, record, dataIndex, cell) {
        var row = cell.up();
        row.setStyle('background: ' + record.get('color') + ';');
        return v;
    }
}]
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
DEV6773
  • 11
  • 1
  • 3
0

I found that neither of the solutions suggested in the accepted answer worked well for me. The solution using a painted event handler only works on first load. If the data is updated then the styling doesn't change so the rows are still coloured as per the original data. The renderer solution is unwieldy if you have a large number of columns or want to have multiple renderers.

For anyone else in the same boat, here's my solution:

itemConfig: {
    viewModel: true,
    bind: {
        cls: '{record.IsEnabled === false ? "disabled" : ""}'
    }
}
Stephen
  • 23
  • 5