{categorys && categorys.length ? <Table
className="m-cover-ant-table"
columns={this.columns}
expandIconAsCell={false}
expandRowByClick={true}
defaultExpandAllRows={true}
dataSource={categorys}
pagination={false}/> : '暂无数据'}
点开这篇文章的你肯定碰到和我一样的问题设置了defaultExpandAllRows={true}默认的全部展开,但是设置了expandedRowKeys后就无效了
这个怎么解决呢?
继续看代码...
onExpand={(bool, row) => {
if (bool) {
this.setState({
expandedRowKeys: [...expandedRowKeys, row.id]
})
} else {
const index = expandedRowKeys.findIndex((e) => e === row.id)
const newArray = [...expandedRowKeys]
newArray.splice(index, 1)
this.setState({
expandedRowKeys: newArray
})
}
}}
在table标签里面加上这段代码,问题就可以完美的解决
代码的目的也很简单,当你点击的某行时,判断是不是要展开,是要展开的就把key加到expandedRowKeys里面,
要收缩就把key值从expandedRowKeys中踢除掉