material-table 如何在选择时选择改变背景颜色的行

Ude*_*esh 2 reactjs material-ui material-table

使用材料表库。我想复制此示例中显示的行为。

https://codesandbox.io/s/table-hover-colors-zw9nt

https://www.npmjs.com/package/material-table https://material-table.com/#/ 在此处输入图片说明

我在考虑使用 onRowClick={}

逻辑是

onRowClick =>

  1. 在组件状态中设置值,将单击的行背景呈现为不同的颜色
  2. 将所有其他行设置为背景为原始颜色

我可以使用基于状态中保持的值的条件渲染来更改背景。尽管这会更改所有行的背景。

options={
   rowStyle:{backgroundColor: this.state.selected ? '#fff' : this.state.c}
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我当前的工作示例在这里 https://codesandbox.io/s/peaceful-haibt-2nefw

谢谢你的帮助

Low*_*key 6

你还需要通过selectedRowId否则一切都会是蓝色的。此外,rowStyle选项接受回调,您可以像这样调用:

rowStyle: rowData => ({
backgroundColor: this.state.selected && rowData.tableData.id === this.state.selectedRowId 
?   this.state.c
    : "#fff" 
})
Run Code Online (Sandbox Code Playgroud)

onRowClick还需要一些工作(选择/取消选择条件不正确)。 https://codesandbox.io/embed/select-one-row-160vm