react可以通过以下代码实现,在表格中根据行数据的 表格中的age 字段动态显示 rowSelection:

实现的时候踩坑:https://blog.csdn.net/lml21lml/article/details/123359850?spm=1001.2014.3001.5506
一定要制定选中项的key数组

import {
    
     Table } from 'antd';

const MyTable = () => {
    
    
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  const dataSource = [
    {
    
    
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
    
    
      key: '2',
      name: 'Joe Black',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
    
    
      key: '3',
      name: 'Jim Green',
      age: 31,
      address: 'Sidney No. 1 Lake Park',
    },
    {
    
    
      key: '4',
      name: 'Jim Red',
      age: 28,
      address: 'London No. 2 Lake Park',
    },
  ];

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

  const handleRowSelection = (selectedRowKeys, selectedRows) => {
    
    
    const filteredRows = selectedRows.filter((row) => row.age > 32);
    const filteredRowKeys = filteredRows.map((row) => row.key);
    setSelectedRowKeys(filteredRowKeys);
  };

  const rowSelection = {
    
    
    type: 'checkbox',
    selectedRowKeys,
    onChange: handleRowSelection,
    getCheckboxProps: (record) => ({
    
    
      disabled: record.age <= 32,
    }),
  };

  return (
    <Table dataSource={
    
    dataSource} columns={
    
    columns} rowSelection={
    
    rowSelection} />
  );
};

猜你喜欢

转载自blog.csdn.net/LRQQHM/article/details/131209421
今日推荐