【问题标题】:ExtJs 3 : Change cell background color of a gridExtJs 3:更改网格的单元格背景颜色
【发布时间】:2014-01-08 09:12:24
【问题描述】:

我有一个 extJs(3.0) 网格面板,它是使用 json 从 postgresql 数据库加载的。这是我的网格面板代码段:

var drop_pick_grid = new Ext.grid.GridPanel({
store : dropPickGridStore,
cm : new Ext.grid.ColumnModel([ selectModel, {
    sortable : true,
    header : "Drop/Pick Loc",
    dataIndex : 'locationName',
    width : 170
}, {
    header : "Town/City",
    sortable : true,
    dataIndex : 'city',
    width : 120
}, {
    header : "Address",
    sortable : true,
    dataIndex : 'addr',
    width : 170
}, {
    header : "EST.Un/Load Time",
    sortable : true,
    dataIndex : 'estimatedTime',
    width : 100
} ]),
sm : selectModel,
/*viewConfig: {
    getRowClass: function(record, index) {
        var color = record.get('tourTypeColor');
        if (c == 'Kandy') {
            alert('Kandy');
        }
    }
},*/
//width : 570,
//height : 390,
autoHeight : true,
autoWidth : true,
frame : true,
iconCls : 'icon-grid',
renderTo : document.body
});

我的 Json 数据:

{'dropPickLoc':[{ 'locationName' : 'Gayan Hardware', 'city' : 'Galle', 'addr' : '121, Wijaya Rd, Galle', 'estimatedTime' : '120', 'tourTypeColor' : 'blue' } , { 'locationName' : 'Kandy Hardware', 'city' : 'Kandy', 'addr' : '11, Kurunagala Road, Kandy', 'estimatedTime' : '40', 'tourTypeColor' : 'blue' } , { 'locationName' : 'Mala Stores', 'city' : 'Colombo', 'addr' : '11B, Thimbirigasyaya, Colombo', 'estimatedTime' : '45', 'tourTypeColor' : 'yellow' } , { 'locationName' : 'Namal Ceramic', 'city' : 'Kurunagala', 'addr' : '12B, Lumbini Udyanaya, Kurinagala', 'estimatedTime' : '45', 'tourTypeColor' : 'yellow' } , { 'locationName' : 'Priya Ceramic', 'city' : 'Nugegoda', 'addr' : '15, Nugegoda', 'estimatedTime' : '40', 'tourTypeColor' : 'yellow' } , { 'locationName' : 'Sam Stores', 'city' : 'Galle', 'addr' : '46A, Galle', 'estimatedTime' : '120', 'tourTypeColor' : 'green' } , { 'locationName' : 'Saman Stores', 'city' : 'Polgahawela', 'addr' : '111, Kurunagala Rd, Kurunagala', 'estimatedTime' : '120', 'tourTypeColor' : 'blue' } , { 'locationName' : 'Sell-X Computors', 'city' : 'Ratnapura', 'addr' : '12, Tiriwanakatiya, Ratnapura', 'estimatedTime' : '120', 'tourTypeColor' : 'green' } , { 'locationName' : 'Super Stores', 'city' : 'Kandy', 'addr' : '16, Kandy Road', 'estimatedTime' : '120', 'tourTypeColor' : 'blue' } , { 'locationName' : 'Wijesingha Hardware', 'city' : 'Galle', 'addr' : '113A, Wackewella Road, Galle', 'estimatedTime' : '120', 'tourTypeColor' : 'green' } ]}

网格加载成功。现在我需要更改“位置名称”列的背景颜色。根据我的 Json 数据,每个位置名称都有一个 tourTypeColor。我需要根据这种颜色更改每个位置名称单元格。

欢迎提出任何建议。请帮我处理一些代码段。

非常感谢

【问题讨论】:

    标签: json extjs grid


    【解决方案1】:

    我在使用 ExtJS 3 方面不是那么坚定。

    我会尝试为您的列“Drop/Pick Loc”定义一个渲染,如下所示:

    {
        sortable : true,
        header : "Drop/Pick Loc",
        dataIndex : 'locationName',
        width : 170,
        renderer : function(value, metaData, record, rowIndex, colIndex, store) {
    
                       metaData.attr = 'style="color:' + record.get('tourTypeColor') + ';"';
                       return value;
                   }
    }
    

    【讨论】:

    • 感谢您的回复米歇尔,但它没有做任何事情
    • 没有错误发生?不可能错 :) 在这里我举了一些我发现的例子,它可以工作 jsfiddle.net/MichelH/F9QM7/1 我现在没有正确的 css。
    • 我没有将 tourTypeColor 添加到我的网格中。这就是它不起作用的原因吗?
    • 谢谢米歇尔。这是有效的。我不需要添加,{ dataIndex : 'tourTypeColor' }。问题是我错过了一些东西。我忘记将 tourTypeColor 添加到我的商店。感谢您的帮助。您的代码正在运行。
    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2011-09-24
    • 2013-04-12
    相关资源
    最近更新 更多