extjs change grid cell background based on value

2020-02-05 09:02发布

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?

6条回答
Melony?
2楼-- · 2020-02-05 09:28

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.

查看更多
倾城 Initia
3楼-- · 2020-02-05 09:32

refer to these example

Ext.onReady(function(){
    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'change'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "change":100  },
            { 'name': 'Bart', "email":"bart@simpsons.com", "change":-20  },
            { 'name': 'Homer', "email":"home@simpsons.com",  "change":23   },
            { 'name': 'Marge', "email":"marge@simpsons.com", "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;
}
查看更多
Juvenile、少年°
4楼-- · 2020-02-05 09:35
renderer: function (value, metaData) {
    if (parseInt(value) > 0) {
        metaData.tdStyle = 'background-color:#ffaaaa';
    }
    return value
}
查看更多
放荡不羁爱自由
5楼-- · 2020-02-05 09:47

Try

.x-grid-cell.category-matching {
    background-color:green;
}
.x-grid-cell.category-not-matching {
    background-color:red;
}
查看更多
【Aperson】
6楼-- · 2020-02-05 09:49

you can set style in class like this:

js:

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

           }]

css:

.column-ltr{
   direction :rtl;
}
查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-05 09:50

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

查看更多
登录 后发表回答