【发布时间】:2020-10-26 08:01:06
【问题描述】:
我需要一些帮助,在 Ant Design 表中,我需要根据状态值隐藏/显示表的特定列。在给定的沙箱链接中,我需要在开关关闭时隐藏姓氏列,并在开关打开时显示。
请有人调查一下,帮帮我。
【问题讨论】:
我需要一些帮助,在 Ant Design 表中,我需要根据状态值隐藏/显示表的特定列。在给定的沙箱链接中,我需要在开关关闭时隐藏姓氏列,并在开关打开时显示。
请有人调查一下,帮帮我。
【问题讨论】:
有一个工作代码,但它应该根据您的需要进行更多的自定义、交互和重构:
// You can also modify the data in the `handleChnage`
// Or conditionally display it like here:
class EditableTable extends React.Component {
state = {
surNameShow: false
};
constructor(props) {
super(props);
this.columns = [
{
title: "Name",
dataIndex: "name",
width: "30%"
},
{
title: "Surname",
dataIndex: "surname",
width: "30%"
}
];
this.state = {
dataSource: [
{
key: "0",
name: "Edward 1",
surname: "King 1"
},
{
key: "1",
name: "Edward 2",
surname: "King 2"
}
]
};
}
handleChnage = key => {
this.setState({ surNameShow: !this.state.surNameShow }, () => {
console.log(this.state.surNameShow);
});
};
render() {
const { dataSource } = this.state;
const columns = this.columns;
return (
<div>
<p className="mr-3"> Show surname</p>
<Switch onChange={() => this.handleChnage()} />
<Table
bordered
dataSource={dataSource}
columns={
this.state.surNameShow
? columns
: columns.filter(ea => ea.dataIndex !== "surname")
}
pagination={false}
/>
</div>
);
}
}
ReactDOM.render(<EditableTable />, document.getElementById("container"));
【讨论】: