Vue+ant-design-vue 表格实现可拖动的伸缩列

Vue+ant-design-vue 表格实现可拖动的伸缩列

应客户要求,表格要实现左右拖动列边框实现列宽的扩大和缩小ant-design-vue官方文档中,table组件中提供了此功能的示例代码。尝试过程中处处报错。经过多次百度与尝试最后整理出一套可用的代码。这篇笔记以 步骤 的形式一步一步的记录如何实现这个功能。

步骤一:安装集成的 vue-draggable-resizable 插件
npm install --save vue-draggable-resizable
步骤二:在项目的main.js中引入插件
// 挂载全局使用的方法
import VueDraggableResizable from 'vue-draggable-resizable'
Vue.component('vue-draggable-resizable', VueDraggableResizable)
步骤三:在使用页面中重新引入根vue实例和插件
import Vue from 'vue'
import VueDraggableResizable from 'vue-draggable-resizable'
步骤四:ant-design-vue 的 table 组件中添加components属性
<a-table
    size="small"
    bordered
    :columns="columns"
    :dataSource="dataSource"
    :pagination="ipagination"
    :scroll="{x:'max-content'}"
    :rowKey="(record,index)=>{return index+1}"
    :components="components"
  >
</a-table>
步骤五:定义components属性代码(表头columns中,每一列都要设置width,如果不设置width属性,拖动时不生效)
  data() {
    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 dragProps = {
            key: col.dataIndex || col.key,
            class: 'table-draggable-handle',
            attrs: {
              w: 10,
              x: col.width,
              z: 1,
              axis: 'x',
              draggable: true,
              resizable: false,
            },
            on: {
              dragging: (x, y) => {
                col.width = Math.max(x, 1)
              },
            },
          }
          const drag = h('vue-draggable-resizable', { ...dragProps })
          return h('th', { ...restProps, class: 'resize-table-th' }, [...children, drag])
        },
      },
    }
    return {
	  dataSource:[],
	  defColumns:[{
	    title: '序号',
	    dataIndex: 'rowIndex',
	    key: 'rowIndex',
	    width: 80,
	    align: 'center',
	    customRender: function (text, r, index) {
	      return parseInt(index) + 1
	    },
	    className: 'columnsColor',
	 },{
	    title: '价格状态',
	    dataIndex: 'customerName1',
	    className: 'columnsColor',
	    scopedSlots: { customRender: 'customerName1' },
	    width: 200,
	 }]
    },
步骤六:样式(style一定要记得添加.table-draggable-handle 和 .resize-table-th 两个class。并且style标签不能家scoped属性)
<style>
	.table-draggable-handle {
	  /* width: 10px !important; */
	  height: 100% !important;
	  left: auto !important;
	  right: -5px;
	  cursor: col-resize;
	  touch-action: none;
	  border: none;
	  position: absolute;
	  transform: none !important;
	  bottom: 0;
	}
	.resize-table-th {
	  position: relative;
	}
</style>
完整代码
<template>
	<a-table
	    size="small"
	    bordered
	    :columns="columns"
	    :dataSource="dataSource"
	    :pagination="ipagination"
	    :scroll="{x:'max-content'}"
	    :rowKey="(record,index)=>{return index+1}"
	    :components="components"
	  >
	</a-table>
</template>
 
<script>
import Vue from 'vue'
import VueDraggableResizable from 'vue-draggable-resizable'
 
export default {
  components: {
    VueDraggableResizable
  },
  data() {
    this.components = {
      header: {
        cell: (h, props, children) => {
          const { key, ...restProps } = props
          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 dragProps = {
            key: col.dataIndex || col.key,
            class: 'table-draggable-handle',
            attrs: {
              w: 10,
              x: col.width,
              z: 1,
              axis: 'x',
              draggable: true,
              resizable: false,
            },
            on: {
              dragging: (x, y) => {
                col.width = Math.max(x, 1)
              },
            },
          }
          const drag = h('vue-draggable-resizable', { ...dragProps })
          return h('th', { ...restProps, class: 'resize-table-th' }, [...children, drag])
        },
      },
    }
    return {
      dataSource:[],
	  defColumns:[{
	    title: '序号',
	    dataIndex: 'rowIndex',
	    key: 'rowIndex',
	    width: 80,
	    align: 'center',
	    customRender: function (text, r, index) {
	      return parseInt(index) + 1
	    },
	    className: 'columnsColor',
	 },{
	    title: '价格状态',
	    dataIndex: 'customerName1',
	    className: 'columnsColor',
	    scopedSlots: { customRender: 'customerName1' },
	    width: 200,
	 }]
    }
  },
  created() {},
}
</script>
 
<style>
	.table-draggable-handle {
	  /* width: 10px !important; */
	  height: 100% !important;
	  left: auto !important;
	  right: -5px;
	  cursor: col-resize;
	  touch-action: none;
	  border: none;
	  position: absolute;
	  transform: none !important;
	  bottom: 0;
	}
	.resize-table-th {
	  position: relative;
	}
</style>
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在Vue3中使用Ant Design Vue的样式,可以按照以下步骤进行操作: 1. 首先,在你的项目中安装Ant Design Vue组件库。你可以通过npm或者yarn进行安装,具体的安装命令可以在Ant Design Vue的官方文档中找到。 2. 在你的Vue组件中引入Ant Design Vue的样式文件。你可以在组件所在的Vue文件中使用import语句引入Ant Design Vue的样式文件。 例如,在你的Vue文件中添加以下代码: ```javascript <style lang="less" scoped> @import '~ant-design-vue/dist/antd.less'; </style> ``` 这将会导入Ant Design Vue的样式文件,并使它只在当前组件中生效。 3. 接下来,你可以根据需要自定义你的组件样式。你可以在style标签中使用普通的CSS语法来定义和修改组件的样式。 例如,你可以在style标签中添加以下代码来自定义一个按钮组件的样式: ```javascript <style lang="less" scoped> .my-button { border-radius: 10px; } </style> ``` 这将会给按钮组件添加一个圆角为10px的边框样式。 通过以上步骤,你就可以在Vue3中使用Ant Design Vue的样式了。记得按照官方文档中的指引导入需要的组件,并在模板中使用它们。如果你需要更多示例和帮助,可以参考官方文档或者Ant Design Vue的开源项目。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3+ant design vue+ts实战【ant-design-vue组件库引入】](https://blog.csdn.net/XSL_HR/article/details/127396384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue3+ant-design-vue按需加载组件](https://blog.csdn.net/qq_42263570/article/details/130143934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vue3+element-plus的后台管理系统模板 和 vue3+ant-design-vue的后台管理系统模板](https://blog.csdn.net/qq_61233877/article/details/131008600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值