Vue2官方地址

Ant Design Vue

官方给的地址表头数据、表格和ResizeableTitle都是写在外面的,不好用。

这里给一个经过请求获取表格数据之后在伸缩的写法

<template>
  <div>
    测试表格
    <a-table
      bordered
      :columns="headerData"
      :components="components"
      :data-source="tableData"
    >
    </a-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headerData: [],
      tableData: [],
      components: {},
    };
  },
  props: [

  ],
  created() {
    this.init(); 
    //2.把getTitle函数拆开放在这里,也没有作用   
    this.getTitle(this.headerData);//3.只有封装成函数才有用
  },
  methods: {
    init() {
      Object.assign(newParams, this.tbHeaderParams, params);
      this.$api.发请求.then(({ data }) => {
        data.map((v) => {
          this.headerData.push({
            title: "-----",
            dataIndex: "-----",
            key: "-----",
            width: 100,
          });
        });
        //1.把getTitle函数拆开放在这里,没有作用
      });
    },
    getTitle(columns) {
      const draggingMap = {};
      columns.forEach((col) => {
        draggingMap[col.key] = col.width; //添加键值对,有key的才添加
      });
      const resizeableTitle = (h, props, children) => {
        let thDom = null;
        const { key, ...restProps } = props;
        const col = columns.find((col) => {
          const k = col.dataIndex || col.key;
          return k === key;
        });
        if (!col.width) {
          return <th {...restProps}>{children}--</th>;
        }
        const onDrag = (x) => {
          draggingMap[key] = 0;
          col.width = Math.max(x, 1);
        };
        const onDragstop = () => {
          draggingMap[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={col.width}
              z={1}
              axis="x"
              draggable={true}
              resizable={false}
              onDragging={onDrag}
              onDragstop={onDragstop}
            ></vue-draggable-resizable>
          </th>
        );
      };
      this.components = {
        header: {
          cell: resizeableTitle,
        },
      };
    },
  },
};
</script>

<style>
.resize-table-th {
  position: relative;
}
.resize-table-th .table-draggable-handle {
  height: 100% !important;
  bottom: 0;
  /* left: auto !important; */
  /* right: -5px; */
  left: 0;
  cursor: col-resize;
  /* touch-action: none; */
  position: absolute;
  background-color: pink;
}
</style>

原理是把获取ResizeableTitle的过程提取出一个函数,在created生命周期获取表头数据的之后在调用,注意要封装成函数,分开好像没效果

注意:

1.表头的数据中需要有width

2.官网给的ResizeableTitle会报错,改成小写的resizeableTitle

3.const draggingState = Vue.observable(draggingMap);这行可以不需要,直接用draggingMap

4.下面的代码不必在当前vue文件中写,可以注册成全局组件,也是可以的

import Vue from 'vue';
import VueDraggableResizable from 'vue-draggable-resizable';

Vue.component('vue-draggable-resizable', VueDraggableResizable);

5.components自己写

效果图

 点击粉色位置即可伸缩表头列

GitHub 加速计划 / vu / Vue.Draggable
5
1
下载
SortableJS/Vue.Draggable: Vue.Draggable 是 Sortable.js 的 Vue.js 封装组件,提供了拖放排序功能,可以在 Vue 应用中轻松实现列表元素的可拖拽重排。
最近提交(Master分支:2 个月前 )
431db153 - 3 年前
017ab498 - 4 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐