AntdReact中的动态可扩展行键表不允许单击展开
创始人
2024-11-07 17:00:19
0

您可以使用antd的Table组件来创建一个可扩展行的表格。但是,有时行键可能是动态的,这可能会使您无法单击行来展开子行。以下是一个示例代码,解决了这个问题:

import React, { useState } from 'react';
import { Table } from 'antd';

const App = () => {
  const [expandedRowKeys, setExpandedRowKeys] = useState([]);

  const handleRowClick = (record) => {
    // check if the clicked row is already expanded
    const isRowOpen = expandedRowKeys.indexOf(record.key) > -1;

    // if the row is not expanded, set the new array of expandedRowKeys
    if (!isRowOpen) {
      setExpandedRowKeys([...expandedRowKeys, record.key]);
    }
    // otherwise, remove the row from the array
    else {
      setExpandedRowKeys(expandedRowKeys.filter((key) => key !== record.key));
    }
  };

  // define columns
  const columns = [
    { title: 'Name', dataIndex: 'name', key: 'name' },
    { title: 'Age', dataIndex: 'age', key: 'age' },
    { title: 'Address', dataIndex: 'address', key: 'address' },
  ];

  // define data
  const data = [
    {
      key: 1,
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
      children: [
        {
          key: 11,
          name: 'James Brown',
          age: 17,
          address: 'London No. 2 Lake Park',
        },
        {
          key: 12,
          name: 'Sophia Brown',
          age: 19,
          address: 'Sidney No. 3 Lake Park',
        },
      ],
    },
    {
      key: 2,
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
      children: [
        {
          key: 21,
          name: 'Amy Green',
          age: 25,
          address: 'New York No. 2 Lake Park',
        },
        {
          key: 22,
          name: 'Tom Green',
          age: 27,
          address: 'Sidney No. 6 Lake Park',
        },
      ],
    },
  ];

  // render the table
  return (
     (
        

相关内容

热门资讯

解谜透视!wepoker辅助是... 解谜透视!wepoker辅助是真的假的,微乐小程序挂哪里,机巧教程(有挂解惑)-哔哩哔哩1、在wep...
揭幕透视!hhpoker免费透... 揭幕透视!hhpoker免费透视脚本,微乐自建房辅助工具2025在哪,窍要教程(有挂技巧)-哔哩哔哩...
关于透视!agpoker辅助,... 关于透视!agpoker辅助,微乐智能辅助软件是真的吗,绝活儿教程(发现有挂)-哔哩哔哩1、每一步都...
关于透视!智星德州插件怎么下载... 关于透视!智星德州插件怎么下载,微信小程序微乐辅助器免费版,模板教程(有挂透明挂)-哔哩哔哩1、智星...
关于透视!来玩德州破解器,微信... 关于透视!来玩德州破解器,微信小程序微乐辅助器免费版v2.0免费,手筋教程(有挂透明挂)-哔哩哔哩1...
教你透视!德州透视脚本,微乐小... 教你透视!德州透视脚本,微乐小程序挂哪里有,模块教程(有挂教程)-哔哩哔哩1、实时德州透视脚本透视辅...
揭幕透视!aa poker透视... 揭幕透视!aa poker透视软件,微乐小程序辅助教程,讲义教程(有挂秘诀)-哔哩哔哩1、很好的工具...
详细透视!pokemmo脚本最... 详细透视!pokemmo脚本最新版,微乐自建房辅助可信吗,指南教程(了解有挂)-哔哩哔哩1、首先打开...
必备透视!wepoker能不能... 必备透视!wepoker能不能透视,微乐小程序破解版修改器,模块教程(有挂教学)-哔哩哔哩1、许多玩...
教你透视!hhpoker辅助挂... 教你透视!hhpoker辅助挂下载,微信小程序 微乐陕西挖坑 外挂,教程书教程(有挂技术)-哔哩哔哩...