简体   繁体   English

保留手风琴的 state 和扩展表行

[英]preserving state of accordion and expanded table rows

Is there any way in Ant Design to capture the state of whether particular table rows are expanded, and apply that state if the page refreshes or if the component remounts (ie when navigating between different routes on a SPA?) Ant 设计中是否有任何方法可以捕获特定表行是否扩展的 state,如果页面刷新或组件重新挂载在不同的 SPA 之间导航时应用 state?

Also is there way to make the expansion of table rows to work like an accordion.还有办法让表格行的扩展像手风琴一样工作。 Ie only one row can be expanded at a time?即一次只能扩展一行?

At this point I am just investigating Ant-D, reading the documentation and playing with small code snippets.在这一点上,我只是在研究 Ant-D,阅读文档并使用小代码片段。

But I can find no documentation on how to do the above (ie preserve/restore the state of table/accordions), so I would not know what to try.但是我找不到有关如何执行上述操作的文档(即保留/恢复表/手风琴的 state),所以我不知道该尝试什么。 I certainly could build out custom bits and pieces, but then that would defeat the purpose of having a nice framework.我当然可以构建自定义的点点滴滴,但这会破坏拥有一个好的框架的目的。

Hoping for a more elegant solution.希望有更优雅的解决方案。

Check the next example, props you should refer to are:检查下一个示例,您应该参考的道具是:

  • onExpandedRowsChange
  • onExpand
  • expandedRowKeys
import { Table, Typography, Select, Radio } from 'antd';

const expandedRowRender = record => (
  <Typography.Text code>{record.key}</Typography.Text>
);

export default function App() {
  const [expandedRow, setExpandedRow] = useState(0);
  const [radioValue, setRadioValue] = useState('accordion');

  const [currentRow, setCurrentRow] = useState([]);
  return (
    <FlexBox>
      <Radio.Group
        style={{ width: 200 }}
        value={radioValue}
        onChange={e => setRadioValue(e.target.value)}
      >
        <Radio value="accordion">Accordion</Radio>
        <Radio value="rowState">rowState</Radio>
      </Radio.Group>
      {radioValue === 'accordion' ? (
        <Table
          dataSource={dataSource}
          columns={columns}
          onExpand={(isExpanded, record) =>
            setExpandedRow(isExpanded ? record.key : undefined)
          }
          expandedRowRender={expandedRowRender}
          expandedRowKeys={[expandedRow]}
        />
      ) : (
        <>
          <Typography.Title>{currentRow}</Typography.Title>
          <Table
            dataSource={dataSource}
            columns={columns}
            expandedRowRender={expandedRowRender}
            onExpandedRowsChange={setCurrentRow}
          />
        </>
      )}
    </FlexBox>
  );
}

编辑 Q-58136315-TableRows

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

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