简体   繁体   English

Ant 设计中的 React 和 ExpandedRow 渲染

[英]React and ExpandedRow Render in Ant-design

I am using Ant Design table in which I am using row expanded functionality.我正在使用 Ant 设计表,其中我正在使用行扩展功能。 my query is when I click any row of the table the row is opening but the issue is when i click on other row the previous row still open, I just want such functionality in which if I click any row the other which is already open should close..... if any one have solution please show me demo in stackBlitz..... Thanks In advance我的查询是当我单击该行正在打开的表格的任何行时,但问题是当我单击其他行时,前一行仍然打开,我只想要这样的功能,如果我单击任何行,另一个已经打开的行应该关闭.....如果有人有解决方案,请在stackBlitz中给我演示......提前谢谢

Check the antd docs AntD/#expandable .检查 antd 文档AntD/#expandable

Its simple.这很简单。 AntD tables have parameter expandable that accepts many variables such as expandedRowKeys that holds witch of the rows have been expanded and onExpand that is amethod that gets called when you click the icon to expand it. AntD 表具有expandable参数,可以接受许多变量,例如expandedRowKeys行的扩展行键(expandedRowKeys)和onExpand (当您单击图标以展开它时调用的方法)。 A simple way would be to create your own variable to hold the key that is expanded like so一种简单的方法是创建自己的变量来保存像这样扩展的键

const [expandedKey, setExpandedKey] = useState(null);

And create a new method for when the row is being expanded as so并为当行被扩展时创建一个新方法

const onExpand = (_, { key }) => setExpandedKey(key);

Then when rendering a AntD table pass these variables然后在渲染 AntD 表时传递这些变量

<Table
  columns={columns}
  expandable={{
    expandedRowRender: (record) => (
      <p style={{ margin: 0 }}>{record.description}</p>
    ),
    onExpand: onExpand,
    expandedRowKeys: [expandedKey]
  }}
  dataSource={data}
/>

Live example: https://codesandbox.io/s/eloquent-http-jxf1m?file=/src/App.js实例: https://codesandbox.io/s/eloquent-http-jxf1m?file=/src/App.js


EDIT编辑

To make it close whenever clicking the _ icon (when its open), change the onExpand method to this:要使其在单击_图标(打开时)时关闭,请将onExpand方法更改为:

const onExpand = (_, { key }) =>
    expandedKey === key ? setExpandedKey(null) : setExpandedKey(key);

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

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