因为发现ant-design-vue组件库中的table组件,当数据源中的children属性为空数组[]时,在树型表格中该条数据没有子数据展开项,但是左侧展开按钮同样会显示,以下将解决这一问题,当数据源中的数据中的children属性为空数组 [] 时,控制左侧展开按钮不显示。
//只有实现该功能的必要代码,缺失属性请忽略
<template>
<a-table
children-column-name="childList"
row-key="id"
:expanded-row-keys="state.expandRowKeys"
>
<template #expandIcon="{ record }">
<template v-if="record.childList && record.childList.length === 0"
><span style="margin-right: 24px"></span //占位控制带有展开按钮的表格列缩进一致
></template>
<template v-else
><button
:class="[
'ant-table-row-expand-icon',
!state.isExpand[record.id]
? 'ant-table-row-expand-icon-collapsed' //使用组件库原展开按钮的样式
: 'ant-table-row-expand-icon-expanded'
]"
@click="expandRow(record.id)"
></button
></template>
</template>
</a-table>
</template>
<script>
import { reactive } from "vue"
const state = reactive({
expandRowKeys: [],
isExpand: {}
})
const expandRow = (id) => {
state.isExpand[id] = !state.isExpand[id];
if (!state.expandRowKeys.includes(id)) {
state.expandRowKeys.push(id);
} else {
const closedIndex = state.expandRowKeys.indexOf(id);
state.expandRowKeys.splice(closedIndex, 1);
}
};
</script>