简体   繁体   English

如何在反应虚拟化表中的行中选择 select 复选框?

[英]How to select checkboxes in row in react-virtuaized Table?

I have implemented a Table using the react-virtualized Table component.我已经使用 react-virtualized Table 组件实现了一个 Table。 I have added a checkbox column in the table and now want to implement the select all/select row functionality using those checkboxes.我在表中添加了一个复选框列,现在想使用这些复选框实现 select 全部/选择行功能。

Now, for adding select all/select row functionality, I am using react hooks for changing the checked prop of CheckBox but it doesn't seem to work.现在,为了添加 select 全部/选择行功能,我正在使用反应挂钩来更改CheckBoxchecked属性,但它似乎不起作用。 Below is the code link and when I scroll down the page with actual data, the checkbox checked state is removed.下面是代码链接,当我用实际数据向下滚动页面时,选中的复选框 state 被删除。

Here's the code that I am trying to do, checked is always remaining false for rows except for the header.这是我想要做的代码,除了 header 之外, checked的行总是保持错误。 Header select all is functioning fine Header select 一切正常

...
import {Checkbox} from 'semantic-ui-react';
const[selectAll, setSelectAll] = useState(false):
const checked = [];
function _checkboxState(data) {
    if (checked.includes(data.index)) {
      checked.pop(data.index);
    } else {
      checked.push(data.index);
    }
  }
<Column
    disableSort
    dataKey="checkbox"
    width={30}
    headerRenderer={() => (
      <Checkbox
        checked={selectAll}
        onChange={() => {
          if (selectAll === true) {
            setSelectAll(false);
          } else {
            setSelectAll(true);
          }
        }}
      />
    )}
    cellRenderer={({rowIndex}) => (
      <Checkbox
        checked={selectAll || checked.includes(rowIndex)}
        onChange={(e, data) => {
          _checkboxState(data);
        }}
        index={rowIndex}
      />
    )}
/>
...
  1. How to maintain the checks after even scrolling the table?滚动表格后如何维护检查?
  2. Where I am doing wrong for checking individual rows?检查个别行我在哪里做错了?

Any help on this is appreciated!对此的任何帮助表示赞赏!

In the meantime, while I was waiting for an answer, I spent more time and found that I was mutating checked and not using useState hook.与此同时,在等待答案的过程中,我花了更多时间,发现我正在变异checked并没有使用useState钩子。

This is the updated code:这是更新的代码:

.....
const [checked, setChecked] = useState([]);

const _onChangeHeaderCheckbox = data => {
    data.checked ? setChecked(sortedList.map(row => row.id)) : setChecked([]);
  };

const _onChangeRowCheckbox = data => {
    const newRow = sortedList[data.index].id;
    checked.includes(newRow)
      ? setChecked(old => old.filter(row => row !== newRow))
      : setChecked(old => [...old, newRow]);
  };

....
<Column
  disableSort
  dataKey="checkbox"
  width={30}
  headerRenderer={() => (
    <Checkbox
      indeterminate={
        checked.length > 0 && checked.length < sortedList.length
      }
      checked={checked.length === sortedList.length}
      onChange={(e, data) => _onChangeHeaderCheckbox(data)}
    />
  )}
  cellRenderer={({ rowIndex }) => (
    <Checkbox
      checked={checked.includes(sortedList[rowIndex].id) === true}
      onChange={(e, data) => _onChangeRowCheckbox(data)}
      index={rowIndex}
    />
  )}
/>
....

Here's the working preview with dummy data:这是带有虚拟数据的工作预览:

编辑 react-virtualized table 复选框 stackoverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM