antd树形表格的展开与折叠只需关注 expandedRowKeys属性 与 expandedRowsChange 事件,expandedRowKeys为表格数据id组成的数组,expandedRowsChange 表格展开与折叠时触发的回调。
以下为举例说明:
<a-button @click="expand">全部展开</a-button>
<a-button @click="expandClose">全部折叠</a-button>
<a-table
:dataSource="data"
:columns="columns"
:loading="formState.loading"
rowKey="id"
:expandedRowKeys="expandedRowKeys"
@expandedRowsChange="expandedRowsChange"
></a-table>
<script>
const expandedRowKeys = ref([])
let expandArr = []; // 全部展开数据的id
expandArr = res.data; // res.data为从后台获取到的数据
expandArr = expandArr.map(item => item.id);
expandedRowKeys.value = expandArr; // 给expandedRowKeys赋值为id组成的数组,期望首次渲染数据时即展开全部数据
// 全部展开
const expand = () => {
expandedRowKeys.value = expandArr; //点击【全部展开】按钮时,给expandedRowKeys赋值为id组成的数组
};
// 全部折叠
const expandClose = () => {
expandedRowKeys.value = []; //点击【全部折叠】按钮时,清空数组,表格行全部折叠
};
// 表格行操作(展开折叠)
const expandedRowsChange = expandedRows => {
expandedRowKeys.value = expandedRows; // 点击树形表格内的展开折叠图标时
};
</script>