简体   繁体   English

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

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

I try to understand antd.我试着理解 antd。

I have here from the documentation Table Example 3. When the radio button is clicked, this state is set and the row is selected.我有来自文档表示例 3 的此处。单击单选按钮时,将设置此 state 并选择行。 If i clicked a cell of a row, the row should also be selected and the ckeck state of the radio button should be set.如果我单击一行的单元格,则还应选择该行,并应设置单选按钮的 ckeck state。

Does anyone have any idea how to do that?有谁知道该怎么做?

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>
    );
}

Current v4 docs for table are pretty extensive.当前用于表格的 v4 文档非常广泛。 Buried in there is this.埋在那里的是这个。

"Rows can be selectable by making first column as a selectable column. You can use rowSelection.type to set selection type. Default is checkbox. “可以通过将第一列设为可选列来选择行。您可以使用 rowSelection.type 设置选择类型。默认为复选框。

selection happens when clicking checkbox by default.默认情况下单击复选框时会发生选择。 You can see https://codesandbox.io/s/000vqw38rl if you need row-click selection behavior."如果需要行单击选择行为,可以查看https://codesandbox.io/s/000vqw38rl 。”

Seems like what you're looking for.好像你在找什么。 Easy to miss, though.不过很容易错过。

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

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