有谁知道用React-Table选择多个行的方法吗?假设我们要单击一行中的一个单元格,然后按SHIFT键,然后选择另一行,也许用CSS对选定的行进行颜色编码?这有可能吗?
答案 0 :(得分:1)
我找到了一种方法。如果您有任何疑问,请告诉我。基本上只需要自己实施即可。
state = {
selected: null,
selectedRows: [],
}
previousRow = null;
handleClick = (state, rowInfo) => {
if (rowInfo) {
return {
onClick: (e) => {
let selectedRows = [];
// if shift key is being pressed upon click and there is a row that was previously selected, grab all rows inbetween including both previous and current clicked rows
if (e.shiftKey && this.previousRow) {
// if previous row is above current row in table
if (this.previousRow.index < rowInfo.index) {
for (let i = this.previousRow.index; i <= rowInfo.index; i++) {
selectedRows.push(state.sortedData[i]);
}
// or the opposite
} else {
for (let i = rowInfo.index; i <= this.previousRow.index; i++) {
selectedRows.push(state.sortedData[i]);
}
}
} else {
// add _index field so this entry is same as others from sortedData
rowInfo._index = rowInfo.index;
selectedRows.push(rowInfo);
this.previousRow = rowInfo;
}
this.setState({ selected: rowInfo.index, selectedRows })
},
style: {
// check index of rows in selectedRows against all rowInfo's indices, to match them up, and return true/highlight row, if there is a index from selectedRows in rowInfo/the table data
background: this.state.selectedRows.some((e) => e._index === rowInfo.index) && '#9bdfff',
}
}
} else {
return {}
}