直接上代码
<a-table
:columns="columns"
:data-source="data"
size="small"
:pagination="false"
@expanded-rows-change="expandedRowsChange"
:expanded-row-keys="expRowKeys"
>
<template #expandIcon="props">
<span v-if="props.record.children">
<div
v-if="props.expanded"
style="display: inline-block; margin-right: 10px"
@click="
(e) => {
props.onExpand(props.record, e)
}
"
>
<CaretDownOutlined />
</div>
<div
v-else
style="display: inline-block; margin-right: 10px"
@click="
(e) => {
props.onExpand(props.record, e)
}
"
>
<CaretRightOutlined />
</div>
</span>
<span v-else style="margin-right: 29px"></span>
</template>
<template #operation="{ record }">
<a-space v-if="record.children">
<a-button
type="link"
block
@click="onAddcategoryBtn('addChild', record)"
v-if="!record.isShow"
>新增子集</a-button
>
<a-button type="link" block @click="onAddcategoryBtn('edit', record)"
>编辑</a-button
>
<a-button type="text" danger @click="onDelete(record)">删除</a-button>
</a-space>
<a-space v-else>
<a-button type="link" block @click="onAddcategoryBtn('edit', record)"
>编辑</a-button
>
<a-button type="text" danger @click="onDelete(record)">删除</a-button>
</a-space>
</template>
</a-table>
const isAllexp = ref<boolean>(true)
const expandedRowsChange = (val) => {
expRowKeys.value = val
}
// 点击按钮可实现展开与收缩
const expandAllRows = () => {
if (isAllexp.value) {
isAllexp.value = false
// 全部展开
const expandRows = []
for (let index = 0; index < data.length; index += 1) {
const element = data[index]
expandRows.push(element.key)
}
expandedRowsChange(expandRows)
} else {
isAllexp.value = true
// 全部收回
expandedRowsChange([])
}
}