如何仅更改 ExtJS 网格中一行的文本颜色?

发布于 2024-10-13 07:29:48 字数 2316 浏览 8 评论 0原文

我创建了以下 ExtJS 网格。

我想更改第二行中所有单元格的文本颜色。

但是,我只能找到如何更改所有行中的所有单元格的颜色,包括标题文本,如下所示:

var myData = [
    [4, 'This is a whole bunch of text that is going to be word-wrapped inside this column.', 0.24, 3.0, '2010-11-17 08:31:12'],
    [16, 'Computer2-this row should be red', 0.28, 2.7, '2010-11-14 08:31:12'],
    [5, 'Network1', 0.02, 2.5, '2010-11-12 08:31:12'],
    [1, 'Network2', 0.01, 4.1, '2010-11-11 08:31:12'],
    [12, 'Other', 0.42, 5.0, '2010-11-04 08:31:12']
];

var myReader = new Ext.data.ArrayReader({}, [{
        name: 'id',
        type: 'int'
    }, {
        name: 'object',
        type: 'object'
    }, {
        name: 'status',
        type: 'float'
    }, {
        name: 'rank',
        type: 'float'
    }, {
        name: 'lastChange',
        type: 'date',
        dateFormat: 'Y-m-d H:i:s'
    }]);

var grid = new Ext.grid.GridPanel({
    region: 'center',
    style: 'color:red', //makes ALL text in grid red, I only want one row to be red
    store: new Ext.data.Store({
        data: myData,
        reader: myReader
    }),
    columns: [{
            header: 'ID',
            width: 50,
            sortable: true,
            dataIndex: 'id',

            hidden: false

        }, {
            header: 'Object',
            width: 120,
            sortable: true,
            dataIndex: 'object',
            renderer: columnWrap

        }, {
            header: 'Status',
            width: 90,
            sortable: true,
            dataIndex: 'status'
        }, {
            header: 'Rank',
            width: 90,
            sortable: true,
            dataIndex: 'rank'
        }, {
            header: 'Last Updated',
            width: 120,
            sortable: true,
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
            dataIndex: 'lastChange'
        }],
    viewConfig: {
        forceFit: true
    },
    title: 'Computer Information',
    width: 500,
    autoHeight: true,
    frame: true,
    listeners: {
        'rowdblclick': function(grid, index, rec){
            var id = grid.getSelectionModel().getSelected().json[0];
            go_to_page('edit_item', 'id=' + id);
        }
    }
});

我必须做什么仅更改第二行单元格的文本颜色?

I've created the following ExtJS grid.

I want to change the text color of all the cells in the second row.

However, I can only find out how to change the color of all cells in all rows including the header text, as show here:

var myData = [
    [4, 'This is a whole bunch of text that is going to be word-wrapped inside this column.', 0.24, 3.0, '2010-11-17 08:31:12'],
    [16, 'Computer2-this row should be red', 0.28, 2.7, '2010-11-14 08:31:12'],
    [5, 'Network1', 0.02, 2.5, '2010-11-12 08:31:12'],
    [1, 'Network2', 0.01, 4.1, '2010-11-11 08:31:12'],
    [12, 'Other', 0.42, 5.0, '2010-11-04 08:31:12']
];

var myReader = new Ext.data.ArrayReader({}, [{
        name: 'id',
        type: 'int'
    }, {
        name: 'object',
        type: 'object'
    }, {
        name: 'status',
        type: 'float'
    }, {
        name: 'rank',
        type: 'float'
    }, {
        name: 'lastChange',
        type: 'date',
        dateFormat: 'Y-m-d H:i:s'
    }]);

var grid = new Ext.grid.GridPanel({
    region: 'center',
    style: 'color:red', //makes ALL text in grid red, I only want one row to be red
    store: new Ext.data.Store({
        data: myData,
        reader: myReader
    }),
    columns: [{
            header: 'ID',
            width: 50,
            sortable: true,
            dataIndex: 'id',

            hidden: false

        }, {
            header: 'Object',
            width: 120,
            sortable: true,
            dataIndex: 'object',
            renderer: columnWrap

        }, {
            header: 'Status',
            width: 90,
            sortable: true,
            dataIndex: 'status'
        }, {
            header: 'Rank',
            width: 90,
            sortable: true,
            dataIndex: 'rank'
        }, {
            header: 'Last Updated',
            width: 120,
            sortable: true,
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
            dataIndex: 'lastChange'
        }],
    viewConfig: {
        forceFit: true
    },
    title: 'Computer Information',
    width: 500,
    autoHeight: true,
    frame: true,
    listeners: {
        'rowdblclick': function(grid, index, rec){
            var id = grid.getSelectionModel().getSelected().json[0];
            go_to_page('edit_item', 'id=' + id);
        }
    }
});

What do I have to do to change the text color of only the cells in the second row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么这么可爱啊 2024-10-20 07:29:48

您想要重写getRowClass,它被传递到GridPanel 的viewConfig 中。 GridPanel 的 API 文档中提供了一个示例:

   viewConfig: {
        forceFit: true,

   //   Return CSS class to apply to rows depending upon data values
        getRowClass: function(record, index) {
            var c = record.get('change');
            if (c < 0) {
                return 'price-fall';
            } else if (c > 0) {
                return 'price-rise';
            }
        }
    },

GridView 包含该方法,因此请在 API 中查找更多信息:

getRowClass( Record record, Number index, Object rowParams, Store store )

重写此函数以应用自定义
渲染期间 CSS 类到行。
您还可以提供自定义参数
到当前的行模板
row 自定义其呈现方式
使用 rowParams 参数。这
函数应该返回 CSS 类
名称(或空字符串 '' 表示没有)
它将被添加到该行的
包装分区申请多个类别
名称,只需返回它们
字符串内以空格分隔
(例如,“我的班级另一个班级”)。

您可以使用 index 参数将 CSS 应用于特定行。

You want to override getRowClass, which is passed into the GridPanel's viewConfig. An example is provided in GridPanel's API docs:

   viewConfig: {
        forceFit: true,

   //   Return CSS class to apply to rows depending upon data values
        getRowClass: function(record, index) {
            var c = record.get('change');
            if (c < 0) {
                return 'price-fall';
            } else if (c > 0) {
                return 'price-rise';
            }
        }
    },

GridView contains the method, so look there in the API for more information:

getRowClass( Record record, Number index, Object rowParams, Store store )

Override this function to apply custom
CSS classes to rows during rendering.
You can also supply custom parameters
to the row template for the current
row to customize how it is rendered
using the rowParams parameter. This
function should return the CSS class
name (or empty string '' for none)
that will be added to the row's
wrapping div. To apply multiple class
names, simply return them
space-delimited within the string
(e.g., 'my-class another-class').

You can apply the CSS to a certain row by using the index argument.

御守 2024-10-20 07:29:48

不确定这是否有帮助,但你可以尝试。我在本地尝试过,它确实改变了第二行中所有字符的颜色。

向网格添加监听器:

listeners:{
    viewready: function(g){
        g.getView().getRow(1).style.color="#f30";
    }
}

“当viewready时,您可以使用GridView中的getRow()方法获取行的div。之后就正常了分配颜色的Javascript(或添加额外的类,如果您愿意)

干杯

编辑

我意识到如果您的商店正在远程检索数据,但数据尚未准备好,则此方法将失败。

not sure if this helps, but you can try. I tried locally, it does change the color of all the characters in 2nd row.

Add listener to the grid:

listeners:{
    viewready: function(g){
        g.getView().getRow(1).style.color="#f30";
    }
}

"When viewready, you can get the row's div by using getRow() method from GridView. After that is normal Javascript of assigning colors (or adding extra class, if you want)

Cheers

EDIT

I realize it's not working if your store is retrieving data remotely. View is ready but data is not ready. This method would fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文