ZBLOG

您可以通过修改 filteredColumns 计算属性,在 activeKey.value === 2 时动态调整列的宽度。以下是实现代码:

  1. <a-table
  2. :columns="filteredColumns"
  3. :data-source="data"
  4. :scroll="{ x: columns.reduce((sum, col) => sum + (col.width || 0), 0) > 1800 ? 'max-content' : undefined }"
  5. :pagination="{
  6. current: currentPage,
  7. pageSize: pageSize,
  8. total: totalRecords,
  9. pageSizeOptions: ['10', '20', '30', '40', '100'],
  10. onShowSizeChange: handlePageSizeChange,
  11. onChange: handlePageChange,
  12. showQuickJumper: true,
  13. showSizeChanger: true,
  14. showTotal: (total, range) => `显示 ${range[0]}~${range[1]}条记录,共 ${total} 条记录`
  15. }"
  16. >
  17. </a-table>
  18. <script setup>
  19. import { computed, ref } from 'vue';
  20. const activeKey = ref(3); // 您的条件值
  21. const columns = [
  22. // ...其他列定义...
  23. {
  24. title: "操作",
  25. key: "operation",
  26. fixed: "right",
  27. width: 200
  28. }
  29. ];
  30. // 动态过滤列和调整宽度
  31. const filteredColumns = computed(() => {
  32. return columns.map(col => {
  33. // activeKey为2时修改操作列宽度
  34. if (col.key === "operation" && activeKey.value === 2) {
  35. return { ...col, width: col.width * 1.5 }; // width变为原来的1.5倍(300)
  36. // return { ...col, width: yourCustomWidth }; // or use a custom value
  37. }
  38. // activeKey为3时隐藏操作列
  39. if (col.key === "operation" && activeKey.value === 3) {
  40. return null;
  41. }
  42. return col;
  43. }).filter(Boolean); // filter out null values for hidden columns
  44. });
  45. </script>

关键点说明:

  1. map() + filter(Boolean):先映射处理所有列,再过滤掉可能被设置为null的列(如隐藏的操作列)
  2. { ...col, width: newValue }:使用展开运算符创建新对象并覆盖width属性
  3. activeKey.value === N:根据不同条件应用不同逻辑

您可以根据需要调整width的计算方式(固定值、百分比、基于原值的计算等)。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=23112

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?