将全选复选框添加到material-ui表标题停止在每个单元格上接受输入 - ReactJS

发布于 2025-01-09 20:03:59 字数 3408 浏览 2 评论 0原文

首先,请注意-此代码已移交给我,我已尝试尽可能简化问题以在此处呈现。

背景

有一个二维表正在从 < 获取列标题来自样本数组的strong>data数组和行标题。

样本.sampletests是跨功能对象,如果sample.sampletests[n].testId与表头(data)中的testId匹配,那么它将被检查。

现在我在标题中添加了“全选”功能 - 引入了一个对象数组状态变量 testSelectAll ,具有 testidisselectall 属性,我工作得很好能够随时选中/取消选中每列的复选框为此,

我引入了之前未使用过的复选框的 checked 属性,仅使用 onChangedefaultChecked 正在被使用..

问题

,但现在各个单元格复选框已停止从用户处获取输入(选中/取消选中) - 我知道需要在此处实现 onchange 处理程序,但现有代码似乎令人困惑(addItems 数组正在添加对象isselected 属性) - 我就快到了,但只需要理清一下 addItems 减速器变量

这是一个先睹为快的

在此处输入图像描述

我的完整运行代码可在codesandbox selectAllCheckbox_Example

{data.map((test, index) => (
              <StyledTableCell>
                {test?.sampleIds !== null &&
                test?.sampleIds.includes(linkData.id) === true ? (
                  <GreenCheckbox
                    icon={<CircleUnchecked />}
                    checkedIcon={<CircleCheckedFilled />}
                    key={index}
                    defaultChecked={setCheckedData(linkData, test)}
                    onChange={(e) => {
                      linkDataHandler(linkData, test, e);
                    }}
                    inputProps={{ "aria-label": "uncontrolled-checkbox" }}
                    checked={
                      (testSelectAll.filter(
                        (t) => t.testid === test.testListId
                      ).length > 0
                        ? testSelectAll.filter(
                            (t) => t.testid === test.testListId
                          )[0].isSelectAllChecked
                        : false) ||
                      samples
                        .map((elm) => elm.sampleTests)
                        .flat()
                        .some(
                          (elm) =>
                            linkData.id === elm.sampleId &&
                            test.testListId === elm.testList.id
                        )/* ||
                      addItems.filter(
                        (a) =>
                          linkData.id === a.sampleId &&
                          test.testListId === a.TestId
                      ).isSelected*/
                    }
                  />
                ) : (
                  <div style={{ padding: "9px" }}>
                    <Tooltip
                      title={<Typography>Test not applicable</Typography>}
                    >
                      <RemoveCircleTwoToneIcon />
                    </Tooltip>
                  </div>
                )}
              </StyledTableCell>
            ))}
          

一旦我取消注释以下代码,默认和全选选项将停止工作

addItems.filter(
                        (a) =>
                          linkData.id === a.sampleId &&
                          test.testListId === a.TestId
                      ).isSelected

First a heads up - This code is handed over to me and I have tried to simplify the problem to present here as much as I could..

Background

There is a 2d table that is getting column header from data array and row header from samples array.

samples.sampletests are cross functional objects and if sample.sampletests[n].testId matches to the testId in table header (data) then it will be checked.

Now I have added Select All function in the header - introduced an object array state variable testSelectAll having testid and isselectall properties which is working fine I am able to check/uncheck the checkboxes per column as I go

To do this I introduced the checked property of checkbox which was not being used before, only onChange and defaultChecked were being used..

Problem

but now the individual cell checkboxes have stopped taking input(check/uncheck) from user - I understand there is a need to implement onchange handler here but the existing code seems confusing (addItems array is adding objects with isselected property) - I am almost there but just need to clear my head around addItems reducer variable

Here is a sneak peek

enter image description here

My complete running code is available in codesandbox selectAllCheckbox_Example

{data.map((test, index) => (
              <StyledTableCell>
                {test?.sampleIds !== null &&
                test?.sampleIds.includes(linkData.id) === true ? (
                  <GreenCheckbox
                    icon={<CircleUnchecked />}
                    checkedIcon={<CircleCheckedFilled />}
                    key={index}
                    defaultChecked={setCheckedData(linkData, test)}
                    onChange={(e) => {
                      linkDataHandler(linkData, test, e);
                    }}
                    inputProps={{ "aria-label": "uncontrolled-checkbox" }}
                    checked={
                      (testSelectAll.filter(
                        (t) => t.testid === test.testListId
                      ).length > 0
                        ? testSelectAll.filter(
                            (t) => t.testid === test.testListId
                          )[0].isSelectAllChecked
                        : false) ||
                      samples
                        .map((elm) => elm.sampleTests)
                        .flat()
                        .some(
                          (elm) =>
                            linkData.id === elm.sampleId &&
                            test.testListId === elm.testList.id
                        )/* ||
                      addItems.filter(
                        (a) =>
                          linkData.id === a.sampleId &&
                          test.testListId === a.TestId
                      ).isSelected*/
                    }
                  />
                ) : (
                  <div style={{ padding: "9px" }}>
                    <Tooltip
                      title={<Typography>Test not applicable</Typography>}
                    >
                      <RemoveCircleTwoToneIcon />
                    </Tooltip>
                  </div>
                )}
              </StyledTableCell>
            ))}
          

As soon as I uncomment the following code, the default and selectall options would stop working

addItems.filter(
                        (a) =>
                          linkData.id === a.sampleId &&
                          test.testListId === a.TestId
                      ).isSelected

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文