JS uses a recursive method to obtain multi-level key values.
The requirements are as follows: a subset of the list is to be expanded by default. The official property is ' defaultExpandedRowKeys ', which must be an array
componentDidMount(){
const {
dataSource} = this.state;
const getTableKey = (data) => {
const parentKey = [];
data.forEach((item) => {
if (item.subList && item.subList.length) {
parentKey.push(item.key);
const temp = getTableKey(item.subList);
if (temp.length) {
parentKey.push(...temp);
}
}
});
return parentKey;
};
const arrKey = getTableKey(dataSource);
this.setState({
arrKey})
};
<Table
columns={
columns}
dataSource={
dataSource}
childrenColumnName="subList"
bordered
pagination={
false}
defaultExpandedRowKeys={
this.state.arrKey}
/>
as the picture shows:
Note: The article focuses on this recursive method, because the structural subset of the table is unknown, so the recursive method is the best solution;