简体   繁体   English

如何在 react-bootstrap-table2 中动态禁用一行的复选框?

[英]How to dynamically disable a row's checkbox in react-bootstrap-table2?

I want to achieve something that is in my current sandbox:我想实现我当前沙箱中的一些东西:

在此处输入图像描述

Currently I've hardcoded the nonSelectable: [1, 2] so it will make row 1 and 2 unselectable.目前我已经对nonSelectable: [1, 2]进行了硬编码,因此它会使第 1 行和第 2 行不可选择。

Now I want to make the rows unselectable based on the file's createdByID.现在我想根据文件的 createdByID 使行不可选择。

If the file's createdByID is same with the user's login ID, then it will be selectable, else it won't, but my issue now is that how do I get each row's data and compare their createdByID with the user's ID then return the indexes that needs their checkbox to be disabled as an array to the nonSelectable prop?如果文件的createdByID与用户的登录 ID 相同,那么它将是可选的,否则它不会,但我现在的问题是如何获取每一行的数据并将它们的createdByID与用户的 ID 进行比较,然后返回索引需要将其复选框作为nonSelectable道具的数组禁用吗?

I've found this link but I'm not quite sure how to get it work as I am not sure where to get the key .我找到了这个链接,但我不太确定如何让它工作,因为我不确定从哪里获得key

CodeSandbox Demo代码沙盒演示

You can create a function to generate the nonSelectable array programmatically for you.您可以创建一个函数来为您以编程方式生成 nonSelectable 数组。 For example例如

function getNonSelectableRows(rows, userLoginId) {
  return rows
    .filter((r) => r.createdByID !== userLoginId)
    .reduce((acc, r) => {
      acc.push(r.fileID);
      return acc;
    }, []);
}

Then call the function for selectRow.nonSelectable然后调用 selectRow.nonSelectable 的函数

function App() {
  ...
  // I don't see the user login id in your code.
  // Thus I just add it as a constant at the top.
  const userLoginId = '200201';
  const [files] = useState([
    {
      fileID: 1,
      fileName: "Cardio Care Awareness Month",
      createdByID: "100101"
    },
    {
      fileID: 2,
      fileName: "MEMO COMPULSORY TO",
      createdByID: "100101"
    },
    {
      fileID: 3,
      fileName: "MEMO 2021 Covid19 Vaccination Leave",
      createdByID: "200201"
    },
    {
      fileID: 4,
      fileName: "Human Cell",
      createdByID: "200201"
    }
  ]);

  const selectRow = {
    mode: "checkbox",
    // nonSelectable = [1, 2]
    nonSelectable: getNonSelectableRows(files, userLoginId),
  ...
 <BootstrapTable
  keyField='id'
  columns={columns}
  rowStyle={rowStyle}
 data={data}
/>

 const rowStyle = (row) => {
        if (row.your_col_name=== 'your_condition') {
            return { backgroundColor: 'gray', color: '#999999', pointerEvents: 'none', cursor: 'not-allowed' };
        }
        return {};
    };

this works for me.这对我有用。

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

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