Ant Design of Vue 表格使用 vue-draggable-resizabl -- 固定列问题修复

表格如果加了列拖拽功能,并且设置了列fixed。
出现了一个头疼的现象

问题:

因为拖拽需要设置width。这个时候。你屏幕宽度超过了他们设置的widht之和。Ant Des的表格自适应放宽。可是加了resizabl的列依旧是固定宽度。导致。显示了两个相同的列。还是不一样的长度



 

解决方式 :

在拖拽/加载时判断是否超长。动态去掉fixed配置项

  • 页面引入
import vdr from "vue-draggable-resizable-gorkys";
import "vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css";
import debounce from "lodash/debounce";
  • :components='components'
    this.components = {
      header: {
        cell: (h, props, children) => {
          const { key, ...restProps } = props;
          // 此处的this.columns 是定义的table的表头属性变量
          const col = this.columns.find((col) => {
            const k = col.dataIndex || col.key;
            return k === key;
          });

          if (!col || !col.width) {
            return h("th", { ...restProps }, [...children]);
          }
          const { minWidth = 200, maxWidth } = col || {};
          const dragProps = {
            key: col.dataIndex || col.key,
            class: "",
            attrs: {
              w: col.width,
              h: "auto",
              axis: "x",
              minWidth,
              maxWidth,
              draggable: false,
              resizable: true,
              handles: ["mr"]
            },
            on: {
              resizing: (l, t, w) => {
                col.width = Math.max(w, 1);
              },
              resizestop: () => {
                this.resize();
              }
            }
          };
          const drag = h(vdr, { ...dragProps }, [...children]);
          restProps.class += " resize-table-th";
          return h(
            "th",
            {
              ...restProps
            },
            [drag]
          );
        }
      }
    };
  • 重新计算表列:this.resize();

        记得给表格添加 样式:.draggable-table,设置::scroll="{x:true}"

// 动态计算Columns
export function calculateColumns(columns) {
  const domTable =
    window.document.querySelector(".draggable-table .ant-table-body") || {};
  const { scrollWidth = 0,clientWidth = 0 } = domTable;
  scrollX = clientWidth < scrollWidth - 1;
  if (scrollX) {
    columns = columns.map(o => {
      if (o.fixedInit) {
        o.fixed = o.fixedInit; // 恢复浮动列配置
      }
      return o;
    });
  } else {
    columns = columns.map(o => {
      if (o.fixed) {
        o.fixedInit = o.fixed;
        o.fixed = undefined;
      }
      return o;
    });
  }
  return columns
}


 窗口变化都要去更新
 

  mounted() {
    this.$options._resize = debounce(this.resize, 150);
    window.addEventListener("resize", this.$options._resize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.$options._resize);
  },

样式:

// 可拖拽表头表格样式
.resize-table-th .vdr {
  height: 100% !important;
  width: 100% !important;
  padding: @rem7 @rem16 !important;
  border: none;
  transform: none !important;
  position: relative !important;
}

.resize-table-th {
  padding: 0 !important;
  &:hover .handle-mr {
    background: @yn-auxiliary-color;
  }
}

.resize-table-th .handle {
  cursor: col-resize;
  border: none !important;
  box-shadow: none !important;
}

.resize-table-th .handle-mr {
  width: 2px !important;
  z-index: 2;
  cursor: col-resize;
  background: inherit;
  border: none;
  height: calc(100% - @rem12) !important;
  top: @rem6 !important;
  right: 0 !important;
  display: block !important;
}

然后不论怎么拖拽。变窗口大小。固定列都能正常展示了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Vue3中,Ant-Design-Vue的Tree组件的用法与Vue2有些不同。以下是示例代码: ```html <template> <a-tree :tree-data="treeData" :draggable="true" :block-node="true" :show-line="true" :default-expanded-keys="defaultExpandedKeys" v-model:selected-keys="selectedKeys" @select="onSelect"> <template #title="{ key, title }"> <span> {{ title }} <a @click.stop="addNode(key)">Add</a> <a @click.stop="removeNode(key)">Delete</a> </span> </template> </a-tree> </template> <script> import { ref } from 'vue' export default { setup() { const treeData = ref([ { title: 'Parent 1', key: '0-0', children: [ { title: 'Child 1', key: '0-0-0' }, { title: 'Child 2', key: '0-0-1' } ] } ]) const defaultExpandedKeys = ref(['0-0']) const selectedKeys = ref([]) const addNode = (parentKey) => { const newNode = { title: 'New Node', key: `${parentKey}-${treeData.value.length}` } const parentNode = treeData.value.find(node => node.key === parentKey) if (!parentNode.children) { parentNode.children = [] } parentNode.children.push(newNode) treeData.value = [...treeData.value] } const removeNode = (key) => { const parentKey = key.split('-').slice(0, -1).join('-') const parentNode = treeData.value.find(node => node.key === parentKey) parentNode.children = parentNode.children.filter(node => node.key !== key) treeData.value = [...treeData.value] } const onSelect = (selectedKeys) => { console.log(selectedKeys) } return { treeData, defaultExpandedKeys, selectedKeys, addNode, removeNode, onSelect } } } </script> ``` 这个示例与Vue2中的示例类似,只是我们使用Vue3的Composition API来编写代码。我们使用了ref函数来创建响应式变量。在addNode和removeNode方法中,我们使用Vue3的响应式API来更新数据。当我们改变treeData的值时,我们必须通过解构赋值来创建一个新的数组来触发更新。注意,在Vue3中,我们使用v-model来绑定selected-keys。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高高i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值