Color legend for grid panel in ExtJS4

2019-03-02 01:54发布

问题:

I have a requirement that each cell in the grid can take any color out of available 8 colors based on some criteria. After that I need to show a color legend of those 8 colors and their tag names below the grid panel. Can somebody suggest me if ExtJS4 has any that kind of feature for grids or any idea on how can I approach this problem.

/** Example of CSS */

.r1 .x-grid-cell-inner {
background-color: #f0f6ff;
color: #09d6ff;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r2 .x-grid-cell-inner {
background-color: rgba(255, 183, 189, 0.22);
color: #900;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r3 .x-grid-cell-inner {
background-color: #e2ffe2;
color: #090;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r4 .x-grid-cell-inner {
background-color: rgba(255, 233, 228, 0.12);
color: #99890e;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r5 .x-grid-cell-inner {
background-color: rgba(186, 242, 250, 0.10);
color: #1a4f99;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r6 .x-grid-cell-inner {
background-color: rgba(255, 242, 239, 0.23);
color: #ff7f00;
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r7 .x-grid-cell-inner {
background-color: rgba(228, 224, 255, 0.7);
color: rgba(29, 7, 255, 0.60);
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.r8 .x-grid-cell-inner {
background-color: rgba(255, 233, 228, 0.32);
color: rgba(255, 22, 12, 0.65);
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}


.n1 .x-grid-cell-inner {
background-color: rgb(255, 255, 255);
color: rgb(255, 127, 0);
border-left: 1px dotted rgba(2, 3, 3, 0.27);
border-right: 1px dotted rgba(2, 3, 3, 0.27);
}

回答1:

I've had to do something similar.

I applied a "color" class to the cell based on the value using the column's renderer config.

In my case the value determined the color of the cell.

Here is the renderer:

// ... other column configs
renderer: function(value, meta, record) {
    switch(value) {

        case 1 : 
            meta.tdCls = 'orange-cell';
            break;

        case 2 : 
            meta.tdCls = 'light-orange-cell';
            break;

        case 3 : 
            meta.tdCls = 'green-cell';
            break;   
    }

    // I only had a color in my cell without any kind of value
    // so didn't include the following return statement. I added it here
    // just so you would know that you can have it also.
    return value;
}

The CSS classes looked like this:

.orange-cell {
    background-color: #ffbb22 !important;
}
.light-orange-cell {
    background-color: #33cc55 !important;
}
.green-cell {
    background-color: #ffeecc !important;
}

This is working fine with ExtJS 4.1.0



回答2:

Add something like that to your grid definition:

viewConfig: {
  getRowClass: function(record, rowIndex, rowParams, store){
    return record.get("valid") ? "row-valid" : "row-error";
  }
}

See http://docs.sencha.com/ext-js/4-0/#!/api/Ext.view.Table-method-getRowClass for more details.

Note: That would work for individual rows. Do you really need cell-by-cell coloring?