简体   繁体   English

Antd 行选择显示在第二列

[英]Antd row selection showing in second column

I am trying to switch between a table with expanded rows and when a button is pressed, switching to row selection.我正在尝试在具有扩展行的表之间切换,当按下按钮时,切换到行选择。 When switching to row selection in this way, the select checkboxes are appearing in the second column instead of the first column.以这种方式切换到行选择时,select 复选框出现在第二列而不是第一列。 Try this in latest version:在最新版本中试试这个:

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Radio, Divider } from "antd";
const columns = [
  {
    title: "Name",
    dataIndex: "name",
    render: (text) => <a>{text}</a>
  },
  {
    title: "Age",
    dataIndex: "age"
  },
  {
    title: "Address",
    dataIndex: "address"
  }
];
const data = [
  {
    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, selectedRows) => {
    console.log(
      `selectedRowKeys: ${selectedRowKeys}`,
      "selectedRows: ",
      selectedRows
    );
  },
  getCheckboxProps: (record) => ({
    disabled: record.name === "Disabled User",
    // Column configuration not to be checked
    name: record.name
  })
};

const Demo = () => {
  const [selectionType, setSelectionType] = useState("checkbox");
  const [expanding, setExpanding] = useState(true);
  return (
    <div>
      <Radio.Group
        onChange={({ target: { value } }) => {
          setSelectionType(value);
        }}
        value={selectionType}
      >
        <Radio value="checkbox">Checkbox</Radio>
        <Radio value="radio">radio</Radio>
      </Radio.Group>
      <br />
      <Radio.Group
        onChange={({ target: { value } }) => {
          setExpanding(value === "true");
        }}
        value={expanding.toString()}
      >
        <Radio value="true">Expanding</Radio>
        <Radio value="false">Selecting</Radio>
      </Radio.Group>

      <Divider />

      <Table
        rowSelection={
          expanding
            ? undefined
            : {
                type: selectionType,
                ...rowSelection
              }
        }
        expandedRowRender={expanding ? () => <div>expanded</div> : undefined}
        columns={columns}
        dataSource={data}
      />
    </div>
  );
};

ReactDOM.render(<Demo />, document.getElementById("container"));

When you change the selecting/expanding radio button, you can see the problem with the checkbox column in selecting mode.当您更改选择/扩展单选按钮时,您可以在选择模式下看到复选框列的问题。 Is this a bug or am I using the component incorrectly?这是一个错误还是我错误地使用了该组件?

You don't have your expandable prop properly configured.您没有正确配置expandable道具。 expandedRowRender should be an attribute for the expandable prop that goes into the component. expandedRowRender应该是进入组件的expandable道具的属性。 In addition, to control the visibility of the expandable column, you can take advantage of the expandIconColumnIndex property to hide the column when the expanding state is true.此外,为了控制可扩展列的可见性,您可以利用expandIconColumnIndex属性在expanding state 为 true 时隐藏该列。

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Radio, Divider } from "antd";
const columns = [
  {
    title: "Name",
    dataIndex: "name",
    render: (text) => <a>{text}</a>
  },
  {
    title: "Age",
    dataIndex: "age"
  },
  {
    title: "Address",
    dataIndex: "address"
  }
];
const data = [
  {
    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, selectedRows) => {
    console.log(
      `selectedRowKeys: ${selectedRowKeys}`,
      "selectedRows: ",
      selectedRows
    );
  },
  getCheckboxProps: (record) => ({
    disabled: record.name === "Disabled User",
    // Column configuration not to be checked
    name: record.name
  })
};

const Demo = () => {
  const [selectionType, setSelectionType] = useState("checkbox");
  const [expanding, setExpanding] = useState(true);
  return (
    <div>
      <Radio.Group
        onChange={({ target: { value } }) => {
          setSelectionType(value);
        }}
        value={selectionType}
      >
        <Radio value="checkbox">Checkbox</Radio>
        <Radio value="radio">radio</Radio>
      </Radio.Group>
      <br />
      <Radio.Group
        onChange={({ target: { value } }) => {
          setExpanding(value === "true");
        }}
        value={expanding.toString()}
      >
        <Radio value="true">Expanding</Radio>
        <Radio value="false">Selecting</Radio>
      </Radio.Group>

      <Divider />

      <Table
        rowSelection={
          expanding
            ? undefined
            : {
                type: selectionType,
                ...rowSelection
              }
        }
        expandable={{
          expandedRowRender: () => <div>expanded</div>,
          expandIconColumnIndex: expanding ? 0 : -1
        }}
        columns={columns}
        dataSource={data}
      />
    </div>
  );
};

ReactDOM.render(<Demo />, document.getElementById("container"));

DEMO 演示

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

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