繁体   English   中英

react app 如何在antd表中设置单选按钮的state?

[英]react app how can I set the state of the radio button in an antd table?

我试着理解 antd。

我有来自文档表示例 3 的此处。单击单选按钮时,将设置此 state 并选择行。 如果我单击一行的单元格,则还应选择该行,并应设置单选按钮的 ckeck state。

有谁知道该怎么做?

import React from 'react';
import { Table } from 'antd';
import 'antd/dist/antd.css';

const columns = [
    {
        title: 'Name',
        dataIndex: 'name',
        render: (text: string) => <a>{text}</a>,
    },
    {
        title: 'Age',
        dataIndex: 'age',
    },
    {
        title: 'Address',
        dataIndex: 'address',
    },
];

interface DataType {
    key: React.Key;
    name: string;
    age: number;
    address: string;
}

const data: DataType[] = [
    {
        key: '1',
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park',
    },
    {
        key: '2',
        name: 'Jim Green',
        age: 42,
        address: 'London No. 1 Lake Park',
    },
    {
        key: '3',
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
    },
    {
        key: '4',
        name: 'Disabled User',
        age: 99,
        address: 'Sidney No. 1 Lake Park',
    },
];

// rowSelection object indicates the need for row selection
const rowSelection = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
        console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
};


export default function TableDemo() {

    return (
        <React.Fragment>
            <Table
                rowSelection={{
                    type: 'radio',
                    ...rowSelection,
                }}
                columns={columns}
                dataSource={data}
                
            />

        </React.Fragment>
    );
}

当前用于表格的 v4 文档非常广泛。 埋在那里的是这个。

“可以通过将第一列设为可选列来选择行。您可以使用 rowSelection.type 设置选择类型。默认为复选框。

默认情况下单击复选框时会发生选择。 如果需要行单击选择行为,可以查看https://codesandbox.io/s/000vqw38rl 。”

好像你在找什么。 不过很容易错过。

暂无
暂无

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

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