根据您的需求,您需要在 a-table
的操作列中根据 activeKey.value
的值动态控制列的显示/隐藏。以下是修改后的代码示例:
<a-table
:columns="filteredColumns"
:data-source="data"
:scroll="{ x: columns.reduce((sum, col) => sum + (col.width || 0), 0) > 1800 ? 'max-content' : undefined }"
:pagination="{
current: currentPage,
pageSize: pageSize,
total: totalRecords,
pageSizeOptions: ['10', '20', '30', '40', '100'],
onShowSizeChange: handlePageSizeChange,
onChange: handlePageChange,
showQuickJumper: true,
showSizeChanger: true,
showTotal: (total, range) => `显示 ${range[0]}~${range[1]}条记录,共 ${total} 条记录`
}"
>
</a-table>
<script setup>
import { computed } from 'vue';
const activeKey = ref(3); // 您的条件值
const columns = [
// ...其他列定义...
{
title: "操作",
key: "operation",
fixed: "right",
width: 200
}
];
// 动态过滤列
const filteredColumns = computed(() => {
return columns.filter(col => {
if (col.key === "operation" && activeKey.value === 3) {
return false; // activeKey为3时隐藏操作列
}
return true;
});
});
</script>
Key Points:
- 动态列过滤:使用
computed
计算属性生成filteredColumns
- 条件判断:当
activeKey.value === 3
时过滤掉操作列(key=operation
) - 响应式更新:当
activeKey
变化时表格会自动重新渲染
⚠️注意:
- Vue的响应式系统会自动处理依赖追踪,无需手动触发更新
- Ant Design Vue的Table组件会响应columns数组的变化自动调整布局
内容由零声教学AI助手提供,问题来源于学员提问