简体   繁体   English

在 ant 表中以展开形式显示动态添加的行

[英]Show dynamically added row in expanded form in ant tables

I have an ant table where I pass a parameter "defaultExpandAllRows=true" which renders the table in an expanded form.我有一个 ant 表,我在其中传递了一个参数“defaultExpandAllRows = true”,它以扩展形式呈现该表。

 <Table columns={columns} pagination={false} expandable={{ expandedRowRender: (record) => ( <p style={{ margin: 0 }}>{record.description}</p> ), defaultExpandAllRows: true, rowExpandable: (record) => record.name !== "Not Expandable" }} dataSource={customScopeTableData} />

My use case is to show the dynamically added row in expanded form.我的用例是以扩展形式显示动态添加的行。 Here is the working sample code https://codesandbox.io/s/dynamic-expandable-row-issue-f6bn5?file=/index.js这是工作示例代码https://codesandbox.io/s/dynamic-expandable-row-issue-f6bn5?file=/index.js

I couldn't find something in the API doc.我在 API 文档中找不到任何内容。 Any help on this would be appreciable.对此的任何帮助都是不言而喻的。

I think you should use expandedRowKeys instead of defaultExpandAllRows .我认为您应该使用expandedRowKeys而不是defaultExpandAllRows Because of defaultExpandAllRows only apply to the initial data, so newly added rows won't be affected.因为 defaultExpandAllRows 只适用于初始数据,所以新添加的行不会受到影响。

expandable={{
  expandedRowRender: (record) => (
    <p style={{ margin: 0 }}>{record.description}</p>
  ),
  rowExpandable: (record) => record.name !== "Not Expandable",
  expandedRowKeys: expandedRowKeys,
  onExpand: (expanded, record) => {
    updateExpandedRowKeys({ record });
  }
}}

First create a state for expandedRowKeys and a function for update expandedRowKeys首先为expandedRowKeys行键创建一个 state 并为更新expandedRowKeys行键创建一个 function

const [expandedRowKeys, setExpandedRowKeys] = useState(data.map(({ key }) => key));
const updateExpandedRowKeys = ({ record }) => {
  const rowKey = record.key;
  const isExpanded = expandedRowKeys.find(key => key === rowKey);
  let newExpandedRowKeys = [];
  if (isExpanded) {
    newExpandedRowKeys = expandedRowKeys.reduce((acc, key) => {
      if (key !== rowKey) acc.push(key);
      return acc;
    }, []);
  } else {
    newExpandedRowKeys = expandedRowKeys;
    newExpandedRowKeys.push(rowKey);
  }
  setExpandedRowKeys(newExpandedRowKeys);
};

Then insert new expandedRowKey when new row added然后在添加新行时插入新的expandedRowKey

const addCustomField = () => {
  const newIndex = customScopeTableData.slice(-1)[0].key + 1;
  setCustomScopeTableData([...customScopeTableData, newEntry(newIndex)]);
  setExpandedRowKeys([...expandedRowKeys, newIndex])
};

Column key for newEntry should be unique, Date.now() seems will be duplicated. newEntry的列键应该是唯一的, Date.now()似乎会重复。 I have changed it to incremental index.我已将其更改为增量索引。

const newIndex = customScopeTableData.slice(-1)[0].key + 1;

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Button } from "antd";

const columns = [
  { title: "Name", dataIndex: "name", key: "name" },
  { title: "Age", dataIndex: "age", key: "age" },
  { title: "Address", dataIndex: "address", key: "address" },
  {
    title: "Action",
    dataIndex: "",
    key: "x",
    render: () => <a>Delete</a>
  }
];

const data = [
  {
    key: 1,
    name: "John Brown",
    age: 32,
    address: "New York No. 1 Lake Park",
    description:
      "My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park."
  },
  {
    key: 2,
    name: "Jim Green",
    age: 42,
    address: "London No. 1 Lake Park",
    description:
      "My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park."
  },
  {
    key: 3,
    name: "Not Expandable",
    age: 29,
    address: "Jiangsu No. 1 Lake Park",
    description: "This not expandable"
  },
  {
    key: 4,
    name: "Joe Black",
    age: 32,
    address: "Sidney No. 1 Lake Park",
    description:
      "My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park."
  }
];

const newEntry = (key) => {
  return {
    key: key,
    name: "John Brown",
    age: 32,
    address: "New York No. 1 Lake Park",
    description:
      "My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park."
  }
};

const TableComponent = () => {
  const [customScopeTableData, setCustomScopeTableData] = useState(data);
  const addCustomField = () => {
    const newIndex = customScopeTableData.slice(-1)[0].key + 1;
    setCustomScopeTableData([...customScopeTableData, newEntry(newIndex)]);
    setExpandedRowKeys([...expandedRowKeys, newIndex])
  };

  const [expandedRowKeys, setExpandedRowKeys] = useState(data.map(({ key }) => key));
  const updateExpandedRowKeys = ({ record }) => {
    const rowKey = record.key;
    const isExpanded = expandedRowKeys.find(key => key === rowKey);
    let newExpandedRowKeys = [];
    if (isExpanded) {
      newExpandedRowKeys = expandedRowKeys.reduce((acc, key) => {
        if (key !== rowKey) acc.push(key);
        return acc;
      }, []);
    } else {
      newExpandedRowKeys = expandedRowKeys;
      newExpandedRowKeys.push(rowKey);
    }
    setExpandedRowKeys(newExpandedRowKeys);
  };
  return (
    <>
      <Table
        columns={columns}
        pagination={false}
        expandable={{
          expandedRowRender: (record) => (
            <p style={{ margin: 0 }}>{record.description}</p>
          ),
          rowExpandable: (record) => record.name !== "Not Expandable",
          expandedRowKeys: expandedRowKeys,
          onExpand: (expanded, record) => {
            updateExpandedRowKeys({ record });
          }
        }}
        dataSource={customScopeTableData}
      />
      <Button type="dashed" onClick={() => addCustomField()}>
        Add field
      </Button>
    </>
  );
};
ReactDOM.render(<TableComponent />, document.getElementById("container"));

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

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