封装通用mixins,在vue中实现a-table组件的可伸缩列(详细且使用便捷)

本文主要是介绍封装通用mixins,在vue中实现a-table组件的可伸缩列(详细且使用便捷),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、实现效果

 2、使用场景

  • vue2 + antd-vue 1.x版本
  • 由于antd-vue 1.x版本的组件库没有提供可伸缩列的功能,才需要我们手动开发
  • 在antd-vue 3.x版本以上的表格已经支持这个功能,不需要我们再去手动开发

3、话不多说,上代码

首先安装vue-draggable-resizable,版本2.1.0可以使用,其他版本未尝试

yarn add vue-draggable-resizable@2.1.0
or
npm i vue-draggable-resizable@2.1.0

src/mixins目录下建一个DraggableResizable.js

import Vue from 'vue'
import VueDraggableResizable from 'vue-draggable-resizable'
Vue.component('vue-draggable-resizable', VueDraggableResizable)export default {created() {this.componentsColumns = {header: {cell: (h, props, children) => {const { key, ...restProps } = propsconst col = this.columns.find((col) => {const k = col.dataIndex || col.keyreturn 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])},},}},computed: {// 动态获取scrollX,防止数组固定列的大小变化scrollX() {return this.columns.reduce((preVal, curVal) => {// console.log(preVal + curVal.width);return preVal + curVal.width}, 0)}}
}

页面中使用

import DraggableResizable from '@/mixins/DraggableResizable'
export default {mixins: [DraggableResizable],
}

绑定到a-table组件上,组件上新增:components="componentsColumns"和:scroll="{ x: scrollX }"

<a-table:loading="loading"bordered@change="handleTableChange":row-key="(record, index) => index":columns="columns":data-source="list":pagination="pagination":row-class-name="isRedRow"filtered:components="componentsColumns":scroll="{ x: scrollX }"
></a-table>

调整列配置项集合columns

  • 由于我封装的mixins中对当前页面中的columns做的操作,所以你表格中columns属性绑定的数据也应该叫做columns
  • width和dataIndex为必传项,如果你的width是字符串'100px',请改为数字类型100,因为涉及到某些计算
  • 当然如果某一列不想伸缩,可以不传dataIndex,但是width是必传哦,例如如下代码中的序号列
data() {return {columns: [{title: '序号',width: 65,customRender: (text, record, index, column) => {return index + 1},align: 'center',},{title: '订单编号',dataIndex: 'orderNumber',align: 'center',ellipsis: true,width: 200,},{title: '创建时间',dataIndex: 'createTime',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 200,},{title: '创建人',dataIndex: 'createByName',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 100},{title: '客户名称',dataIndex: 'clientName',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 200,}] }  
}
最后是当前页面的css,如果是less或者scss记得带上/deep/或者::v-deep
/deep/.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;
}
/deep/.resize-table-th {position: relative;
}

4、页面使用完整代码

<template><div><a-tablebordered@change="handleTableChange":columns="columns":data-source="list":pagination="pagination"filtered:components="componentsColumns":scroll="{ x: scrollX }"></a-table></div>
</template><script>
import DraggableResizable from '@/mixins/DraggableResizable'
export default {mixins: [DraggableResizable],data() {return {pagination: {current: 1,pageSize: 100,pageSizeOptions: ['10', '20', '50', '100', '200'],showQuickJumper: false,showSizeChanger: true,total: 0,},list: [],columns: [{title: '序号',width: 65,customRender: (text, record, index, column) => {return index + 1},align: 'center',},{title: '订单编号',dataIndex: 'orderNumber',customRender: (text, record) => (text ? (text + (record.divideOrderNumber || '')) : '--'),align: 'center',ellipsis: true,width: 200,},{title: '创建时间',dataIndex: 'createTime',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 200,},{title: '创建人',dataIndex: 'createByName',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 100},{title: '客户名称',dataIndex: 'clientName',customRender: (text) => (text ? text : '--'),align: 'center',ellipsis: true,width: 200,},{title: '操作',dataIndex: 'action',scopedSlots: {customRender: 'action'},align: 'center',width: 120,fixed: 'right'},],}},
}
</script>
<style lang="less" scoped>
/deep/.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;
}
/deep/.resize-table-th {position: relative;
}
</style>
如果解决了你的问题点个赞吧^_^

这篇关于封装通用mixins,在vue中实现a-table组件的可伸缩列(详细且使用便捷)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/659687

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求