extjs change grid cell background based on value
Asked Answered
M

6

11

I applied a renderer to my grid-column, but the background color is not changing:

renderer: function(value, meta) {
    if (parseInt(value) > 0) {
        meta.tdCls = 'category-matching'; return value;
    }
    else { 
        meta.tdCls = 'category-not-matching'; return value;
    }
}

css:

.x-grid-cell .category-matching {
    background-color:green;
}
.x-grid-cell .category-not-matching {
    background-color:red;
}

I also tried

.grid-cell-inner

and

background-color:red; !important

but no effect.

Any idea?

Merozoite answered 11/11, 2013 at 10:48 Comment(0)
T
38

Try this...

renderer : function(value, meta) {
    if(parseInt(value) > 0) {
        meta.style = "background-color:green;";
    } else {
        meta.style = "background-color:red;";
    }
    return value;
}

It works for me.

Tomfool answered 11/11, 2013 at 11:26 Comment(1)
Works for me too except you need to add 'return value' or you'll end up with an empty column.Atiptoe
S
2

Inspired by Select Smile... this worked for me:

    var myRender = function (value, metaData, record, rowIndex, colIndex, store, view) {
        if (parseInt(value) < 0) {
            metaData.attr = 'style="background-color:#ffaaaa !important;"';
        }
        return value
    };

and the field

{id: 'dta', dataIndex: 'days_to_arrival', renderer: myRender}

that's it.

ps. done under ExtJS v2.2.1

Sandell answered 14/1, 2014 at 9:28 Comment(0)
W
1

refer to these example

Ext.onReady(function(){
    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'change'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"[email protected]",  "change":100  },
            { 'name': 'Bart', "email":"[email protected]", "change":-20  },
            { 'name': 'Homer', "email":"[email protected]",  "change":23   },
            { 'name': 'Marge', "email":"[email protected]", "change":-11   }
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });

    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            { header: 'Name',  dataIndex: 'name' },
            { header: 'Email', dataIndex: 'email', flex: 1 },
            { header: 'Change', dataIndex: 'change', tdCls: 'x-change-cell' }
        ],
        height: 200,
        width: 400,
        viewConfig: {
            getRowClass: function(record, index) {
                var c = record.get('change');
                if (c < 0) {
                    return 'price-fall';
                } else if (c > 0) {
                    return 'price-rise';
                }
            }
        },
        renderTo: Ext.getBody()
    });
});

CSS:

.price-fall .x-change-cell {
    background-color: #FFB0C4;
    color:red;
}
.price-rise .x-change-cell {
    background-color: #B0FFC5;
    color:green;
}
Workhouse answered 11/11, 2013 at 11:41 Comment(1)
the values are from the store, so i dont change anything. But for further use cases, it will be useful!Merozoite
E
1
renderer: function (value, metaData) {
    if (parseInt(value) > 0) {
        metaData.tdStyle = 'background-color:#ffaaaa';
    }
    return value
}
Estrange answered 26/1, 2018 at 17:7 Comment(1)
Even if your code solves the OP issue, it is recommended to add some of the descriptive text to your code snippet.Denning
S
0

Try

.x-grid-cell.category-matching {
    background-color:green;
}
.x-grid-cell.category-not-matching {
    background-color:red;
}
Selfesteem answered 11/11, 2013 at 11:8 Comment(1)
Also, it would be helpful if you could post the rendered HTML of the grid.Selfesteem
P
0

you can set style in class like this:

js:

columns: {
        items: [
           {
                ...
                innerCls: 'column-ltr',

           }]

css:

.column-ltr{
   direction :rtl;
}
Playboy answered 29/12, 2019 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.