英文:
How can one set different styles for cells in selected rows in react-data-grid?
问题
我使用版本为7.0.0-beta.28的adazzle中的react-data-grid。我的问题是,我有一个表格,其中一个列不应该是可编辑的。因此,我想通过将该列的背景变成灰色来可视化这一点。另外,我想要在选择的行中有一种颜色来表示选择。我的问题是:如果选择了灰色单元格,而选择的行中的其他单元格是不同颜色的,我该如何更改灰色单元格的颜色?在下面的图片中,您可以看到一个示例,其中左侧的“Numbers”列不应该是可编辑的,呈灰色。第二行被选择了,但只有“Banana”被涂成蓝色来表示行的选择,“2”仍然是灰色。我希望“2”单元格的颜色与“Numbers”列的其余部分不同。
我使用以下方式在列定义中设置灰色颜色:
const columns = [
{key: "numbers", name: "Numbers", editable: false, cellClass: "text-bg-secondary"},
{key: "things", name: "Things", editable: true},
];
选择功能的实现如下:
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number>>(() => new Set([1]));
const handleRowClick = (args: CellClickArgs<RowType, any>) => {
setSelectedRows(new Set([args.row.id]));
};
const rowKeyGetter = (row: RowType) => row.id;
function App() {
return <DataGrid columns={columns} rows={rows} rowKeyGetter={rowKeyGetter} selectedRows={selectedRows} onSelectedRowsChange={setSelectedRows} onCellClick={handleRowClick}/>;
}
我知道可以使用RowRenderer更改行的颜色,但我不知道如何在这里单独更改单元格的颜色。否则,可以在RowRenderer中使用div标签构建单元格,但我不知道如何保留单元格的默认格式。使用CellFormatter可以更改单元格内容,但然后无法涂抹整个背景,因为单元格内容有外部单元格div标签的边距。
我深入研究了库的代码,但找不到解决我的简单问题的方法。真的不可能吗,还是我漏掉了什么?
英文:
I use react-data-grid from adazzle in version 7.0.0-beta.28. My problem is that I have a table where one column should not be editable. Therefore, I want to visualise this by making the background of this column grey. Additionally, I want to have a colour visualising a selected row. My question is: how can I change the colour of the grey cells if they are selected while the other cells in the selected row are in a different colour? In the following picture you see an example, where the left "Numbers" column should not be editable and is coloured grey. The second row is selected, but only "Banana" is coloured in blue marking the selection of the row and "2" is still grey. I want to have that the "2" cell is in another colour than the rest of the "Numbers" column.
I set the grey colour with cellClass in the columns definition like this:
const columns = [
{key: "numbers", name: "Numbers", editable: false, cellClass: "text-bg-secondary"},
{key: "things", name: "Things", editable: true},
];
The selection is implemented as follows:
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number>>(() => new Set([1]));
const handleRowClick = (args: CellClickArgs<RowType, any>) => {
setSelectedRows(new Set([args.row.id]));
};
const rowKeyGetter = (row: RowType) => row.id;
function App() {
return <DataGrid columns={columns} rows={rows} rowKeyGetter={rowKeyGetter} selectedRows={selectedRows} onSelectedRowsChange={setSelectedRows} onCellClick={handleRowClick}/>;
}
I know that I can change the colour of a row with a RowRenderer, but I do not see how to change the cells individually here. Otherwise, one can build cells in the RowRenderer with div tags, but I do not know how to keep the default formatting of the cells there. And with a CellFormatter one can change the cell content but then one cannot paint the whole background because the cell content has margins from the outer cell div tag.
I looked deep into the code of the library but could not find a way to solve my simple problem. Is it really not possible or do I miss something?
答案1
得分: 0
我终于找到了解决我的问题的方法。我现在利用了 cellClass
可以是一个获取当前行的函数的事实。此外,我在我的行类型中添加了一个布尔值,用来保存行是否被选中的信息,这个信息可以在 cellClass
函数中使用。
type RowType = { id: number, title: string, isSelected: boolean }
const columns = [
{ key: 'numbers', name: 'Numbers', cellClass: (row: RowType) => row.isSelected ? "text-bg-light" : "text-bg-secondary" },
{ key: 'things', name: 'Things' }
];
const rows = [
{ numbers: 1, things: "Apple", isSelected: false },
{ numbers: 2, things: "Banana", isSelected: true },
{ numbers: 3, things: "Tomato", isSelected: false },
{ numbers: 4, things: "Salad", isSelected: false },
{ numbers: 5, things: "Carrot", isSelected: false }
];
const handleRowClick = (args: CellClickArgs<RowType, any>) => {
setSelectedRows(new Set([args.row.id]));
rows.map((row: RowType) => { if (row.id == args.row.id) row.isSelected = true; else row.isSelected = false });
};
在 handleRowClick
函数中,我需要通过设置每一行的 isSelected
来设置实际的选中状态。这将导致正确的结果,如下图所示,其中 "2" 现在与 "Numbers" 列中的其他单元格颜色不同。
英文:
I finally found a solution to my problem. I use now the fact that cellClass
can be a function getting the current row. Additionally, I added a boolean to my row type holding the information if a row is selected which can be used in the function from cellClass
.
type RowType = { id: number, title: string, isSelected: boolean }
const columns = [
{ key: 'numbers', name: 'Numbers', cellClass: (row: RowType) => row.isSelected ? "text-bg-light" : "text-bg-secondary" },
{ key: 'things', name: 'Things' }
];
const rows = [
{numbers: 1, things: "Apple", isSelected: false},
{numbers: 2, things: "Banana", isSelected: true},
{numbers: 3, things: "Tomato", isSelected: false},
{numbers: 4, things: "Salad", isSelected: false},
{numbers: 5, things: "Carrot", isSelected: false}
];
const handleRowClick = (args: CellClickArgs<RowType, any>) => {
setSelectedRows(new Set([args.row.id]));
rows.map((row: RowType) => { if(row.id == args.row.id) row.isSelected = true; else row.isSelected = false });
};
Here in handleRowClick
, I need to set the actual selection by setting isSelected
of each row. This leads then to the correct result as seen below where "2" now has another colour than the other cells in the "Numbers" column.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论