您可以通过修改 filteredColumns
计算属性,在 activeKey.value === 2
时动态调整列的宽度。以下是实现代码:
<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, ref } from 'vue';
const activeKey = ref(3); // 您的条件值
const columns = [
// ...其他列定义...
{
title: "操作",
key: "operation",
fixed: "right",
width: 200
}
];
// 动态过滤列和调整宽度
const filteredColumns = computed(() => {
return columns.map(col => {
// activeKey为2时修改操作列宽度
if (col.key === "operation" && activeKey.value === 2) {
return { ...col, width: col.width * 1.5 }; // width变为原来的1.5倍(300)
// return { ...col, width: yourCustomWidth }; // or use a custom value
}
// activeKey为3时隐藏操作列
if (col.key === "operation" && activeKey.value === 3) {
return null;
}
return col;
}).filter(Boolean); // filter out null values for hidden columns
});
</script>
关键点说明:
map()
+filter(Boolean)
:先映射处理所有列,再过滤掉可能被设置为null的列(如隐藏的操作列){ ...col, width: newValue }
:使用展开运算符创建新对象并覆盖width属性activeKey.value === N
:根据不同条件应用不同逻辑
您可以根据需要调整width的计算方式(固定值、百分比、基于原值的计算等)。
内容由零声教学AI助手提供,问题来源于学员提问