ant design vue的table表头拖拉适应宽度。

在data中的columns中每一列会有固定的宽度(如果不给定宽度,拉拽则不起作用)。如果是这样的结果那么我们需要根据文档来设置components以及结合《vue-draggable-resizable》插件。在这里插入图片描述
在data数据中定义,

data (){
    return {
        components: { header: { cell: this.changeTitle } },
    }
}
methods(){
   changeTitle(h, props, children) {
      let draggingState = {};
      this.columns.forEach(col => {
        draggingState[col.key] = col.width;
      });

      const { key, ...restProps } = props;
      let thDom = null;
      const col = this.columns.find(col => {
        const k = col.dataIndex || col.key;
        return k === key;
      });
      if (!col || !col.width) {
        return <th {...restProps}>{children}</th>;
      }
      const onDrag = x => {
        draggingState[key] = 0;
        col.width = Math.max(x, 10);
      };

      const onDragstop = () => {
        draggingState[key] = thDom.getBoundingClientRect().width;
      };
      return (
        <th
          {...restProps}
          v-ant-ref={r => (thDom = r)}
          width={col.width}
          class="resize-table-th"
        >
          {children}
          <vue-draggable-resizable
            key={col.key}
            class="table-draggable-handle"
            w={10}-默认宽度
            x={draggingState[key] || col.width}
            z={1}
            axis="x"
            draggable={true}
            resizable={false}
            onDragging={onDrag}-每当拖动组件时调用
            onDragstop={onDragstop}-每当组件停止拖动时调用
          ></vue-draggable-resizable>
        </th>
      );
    },
}

大家有兴趣可以尝试一下。有什么问题,欢迎在楼下评论。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值