Antd Tree:我想默认禁用检查子项

发布于 2025-01-11 11:12:39 字数 246 浏览 0 评论 0原文

我正在使用 React 和 Antd 并使用 Tree 组件,我有我的树数据,我希望能够只检查父母,当我检查父母时,他的所有孩子也会被检查,但我不想只能检查父母的一些孩子.. 我可以禁用孩子的选中功能并仅向父母显示它吗?

这是我的代码

谢谢

I'm using React with Antd and working with Tree Component, i have my Tree Data and I want to be able to check only parents, and when I check a parent all his child are checked also, but I don't want to be able to check only some child of a parent..
Can I disable checked feature on childs and display it only for parent ?

This is my code

Thank you

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2025-01-18 11:12:39

您必须仅为 children 节点添加一个属性 checkable: false,不要用于父节点,到您的数据 treeData

const treeData = [
  {
    title: "0-0",
    key: "0-0",
    children: [
      {
        checkable: false,
        title: "0-0-0",
        key: "0-0-0",
        children: [
          {
            title: "0-0-0-0",
            key: "0-0-0-0"
          },
          {
            title: "0-0-0-1",
            key: "0-0-0-1"
          },
          {
            title: "0-0-0-2",
            key: "0-0-0-2"
          }
        ]
      },
      {
        checkable: false,
        title: "0-0-1",
        key: "0-0-1",
        children: [
          {
            title: "0-0-1-0",
            key: "0-0-1-0"
          },
          {
            title: "0-0-1-1",
            key: "0-0-1-1"
          },
          {
            checkable: false,
            title: "0-0-1-2",
            key: "0-0-1-2"
          }
        ]
      },
      {
        title: "0-0-2",
        key: "0-0-2"
      }
    ]
  },
  {
    title: "0-1",
    key: "0-1",
    children: [
      {
        checkable: false,
        title: "0-1-0-0",
        key: "0-1-0-0"
      },
      {
        checkable: false,
        title: "0-1-0-1",
        key: "0-1-0-1"
      },
      {
        checkable: false,
        title: "0-1-0-2",
        key: "0-1-0-2"
      }
    ]
  },
  {
    title: "0-2",
    key: "0-2"
  }
];

工作代码 < a href="https://codesandbox.io/s/control-tree-antd-4-18-9-forked-i52zr8?file=/index.js" rel="nofollow noreferrer">https://codesandbox.io/s/control-tree-antd-4-18-9-forked-i52zr8?file=/index.js

you have to add one property, checkable: false for the children nodes only, dont use for parent, to your data treeData,

const treeData = [
  {
    title: "0-0",
    key: "0-0",
    children: [
      {
        checkable: false,
        title: "0-0-0",
        key: "0-0-0",
        children: [
          {
            title: "0-0-0-0",
            key: "0-0-0-0"
          },
          {
            title: "0-0-0-1",
            key: "0-0-0-1"
          },
          {
            title: "0-0-0-2",
            key: "0-0-0-2"
          }
        ]
      },
      {
        checkable: false,
        title: "0-0-1",
        key: "0-0-1",
        children: [
          {
            title: "0-0-1-0",
            key: "0-0-1-0"
          },
          {
            title: "0-0-1-1",
            key: "0-0-1-1"
          },
          {
            checkable: false,
            title: "0-0-1-2",
            key: "0-0-1-2"
          }
        ]
      },
      {
        title: "0-0-2",
        key: "0-0-2"
      }
    ]
  },
  {
    title: "0-1",
    key: "0-1",
    children: [
      {
        checkable: false,
        title: "0-1-0-0",
        key: "0-1-0-0"
      },
      {
        checkable: false,
        title: "0-1-0-1",
        key: "0-1-0-1"
      },
      {
        checkable: false,
        title: "0-1-0-2",
        key: "0-1-0-2"
      }
    ]
  },
  {
    title: "0-2",
    key: "0-2"
  }
];

working code https://codesandbox.io/s/controlled-tree-antd-4-18-9-forked-i52zr8?file=/index.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文