在后台管理和中台项目中, table是使用率是特别的高的, 虽然element已经有table组件, 但是分页和其他各项操作还是要写一堆的代码, 所以就在原有的基础上做了进一步的封装

所涵盖的功能有: 内容展示 , 操作栏 , 选择框 , 分页 , 图片渲染 , 开关 , 过滤器(时间格式化)

直接上代码

组件:

<template>
<div class="hello">
<el-table
:data="tableData"
style="width: 98%"
@selection-change="handleSelectionChange"
border>
<el-table-column v-if="type=='checkbox'" label="选择">
<template slot-scope="{ row }">
<el-checkbox v-model="row.isChecked" @change="handleChecked(row)"></el-checkbox>
</template>
</el-table-column>
<el-table-column v-if="type=='selection'" :reserve-selection="true" type="selection" width="55" />
<el-table-column v-if="type=='index'" type="index" label="序号" width="55" />
<template v-for="(item, index) of tableTitle">
<el-table-column
:prop="item.prop"
:label="item.label"
:key="index"
:min-width="item.width"
>
<template slot-scope="{ row, $index }" style="height: 100%;">
<span v-if="item.filter == 'date'">
{{ row[item.prop] | dateFilter }}
</span>
<span v-else-if="item.filter == 'time'">
{{ row[item.prop] | timeFilter }}
</span>
<span v-else-if="item.filter == 'image' && row[item.prop]">
<img :src="row[item.prop]" alt="" style="height: 45px;">
</span>
<span v-else-if="item.filter == 'switch'">
<el-switch
v-model="row[item.prop]"
@change="change(row, $index)"
/>
</span>
<span v-else>
{{ row[item.prop] }}
</span>
</template>
</el-table-column>
</template>
<!-- 插槽: 操作-->
<el-table-column label="操作" v-if="ishandle" :width="handleWidth">
<template slot-scope="scope">
<slot name="handle" :row="scope.row" :index="scope.$index"></slot>
</template>
</el-table-column>
</el-table> <el-pagination
background
layout="total, sizes, prev, pager, next, jumper"
:total="total"
:page-size="pageSize"
:current-page.sync="current"
:page-sizes="[10, 20, 30, 40, 50, 100]"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
>
</el-pagination> </div>
</template> <script>
export default {
name: 'HelloWorld',
props: {
handleWidth: { // 操作宽度
default: 200
},
ishandle: { // 是否有操作按钮
type: Boolean,
default: true
},
type: String, // 单选/多选/或值展示
tableTitle: Array, // 表头
tableData: Array, // 数据
},
data () {
return {
total: 1000,
pageSize: 10,
current: 1
}
},
methods: {
handleSizeChange (size) { // 改变每页数量
this.pageSize = size
this.$emit('handleChange', this.pageSize, this.current) },
handleChecked (row) { // 单选
if (row.isChecked) {
this.tableData.map(item => {
if (item.id != row.id) {
this.$set(item, 'isChecked', false)
}
})
this.$emit('handleChecked', row)
} else {
this.$emit('handleChecked', '', row)
}
},
handleSelectionChange (row) { // 多选
this.$emit('handleChecked', row)
},
handleCurrentChange (current) { //换页
this.current = current
this.$emit('handleChange', this.pageSize, this.current)
},
change (row, index) { // 切换开关
this.$emit('handleSwitch', row, index)
},
}
}
</script> <style scoped lang="scss">
</style>

在父组件中调用:

<template>
<div class="home">
<ComTable
:handleWidth="200"
:tableTitle="tableTitle"
:tableData="tableData"
@handleChange="handleChange"
@handleSwitch="handleSwitch"
@handleChecked="handleChecked"
>
<template slot="handle" slot-scope="scope">
<el-button type="text" size="small">编辑{{scope.index}}</el-button>
</template>
</ComTable>
</div>
</template> <script>
import ComTable from '@/components/Com_Table.vue' export default {
name: 'Home',
components: {
ComTable
},
data () {
return {
tableTitle: [{
prop: 'name',
label: '姓名',
width: '200',
},{
prop: 'sex',
label: '性别',
width: '200',
filter: 'switch',
},{
prop: 'url',
label: '头像',
width: '200',
filter: 'image',
},{
prop: 'date',
label: '出生日期',
width: '200',
filter: 'date'
},],
tableData: [{
id: 1,
name: '张三',
sex: true,
url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3202059567,1723387850&fm=26&gp=0.jpg',
date: new Date()
},{
id: 2,
name: '张三',
sex: true,
url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3202059567,1723387850&fm=26&gp=0.jpg',
date: new Date()
},{
id: 3,
name: '张三',
sex: true,
url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3202059567,1723387850&fm=26&gp=0.jpg',
date: new Date()
},{
id: 4,
name: '张三',
sex: true,
url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3202059567,1723387850&fm=26&gp=0.jpg',
date: new Date()
},],
}
},
mounted() {
},
methods: {
handleChange (size, current) {
// 分页改变时的回调---- size: 每页的数量 current: 第几页
console.log(size, current, 'tableData')
},
handleSwitch (row, index) {
// 切换开关时的回调-======== this.tableData: 滑块值改变后的数据.row: 当前行数据 index: 当前行的索引
console.log(this.tableData, '--tableData---', row, index)
},
handleChecked (val) {
// 勾选时的回调---- val: 选中的数据 多选是val是数组, 单选时是对象
console.log(val, 'val===') }
}
}
</script>

组件中有使用过滤器, 可以定义一下全家的过滤器,然后引入, 这里要根据自己的文件来进行调整. 送上我这里用的两个过滤器

// 注册全局的过滤器  {{ msg | dateFilter }}
import Vue from 'vue'
import moment from 'moment' // 展示日期格式: YYYY-MM-DD
Vue.filter('dateFilter', function (dataStr, pattern = 'YYYY-MM-DD') {
if (dataStr) {
return moment(dataStr).format(pattern)
} else {
return dataStr
}
}) // 展示日期格式: YYYY-MM-DD HH:mm:ss
Vue.filter('timeFilter', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
if (dataStr) {
return moment(dataStr).format(pattern)
} else {
return dataStr
}
})

下面是相关参数的说明:

type: 表格类型. 非必传. 值: selection(多选) / checkbox(单选) 类型: string /index:序号1.2.3...

handleWidth: 操作栏宽度    非必传   默认200

tableTitle: 表头.  必传.   类型: 数组   例:
tableTitle: [{
prop: 'name', 绑定的字段
label: '姓名', 表头名称
width: '200', 列宽度
filter: 'date' 过滤器. 需要展示的类型. 非必传. 值:
date: 日期格式(YYYY-MM-DD)
time: 时间格式(YYYY-MM-DD : HH:mm:ss)
image: 图片
}] > tableData: 要展示的数据. 必传 类型: array 例:

插槽:

  slot="handle":    handle: 插槽名称
slot-scope="scope": scope: 组件传递给插槽的值 scope.row: 当前行的内容 scope.index: 当前行的索引

事件:

handleChange (size, current) {}, //分页改变时的回调----   size: 每页的数量   current: 第几页
handleSwitch (row, index) {}, // 切换开关时的回调-======== this.tableData: 滑块值改变后的数据.row: 当前行数据 index: 当前行的索引
handleChecked (val) {}, // 勾选时的回调---- val: 选中的数据 多选是val是数组, 单选时是对象

封装并不是很全面很精致, 但是至少可以省点事~~~

以上代码还未经过项目的检验, 属于雏形, 还需要不断的优化和改进, 如遇坑, 请留言. 谢谢!!!

vue+element对常用表格的简单封装的更多相关文章

  1. vue + element ui table表格二次封装 常用功能

    因为在做后台管理项目的时候用到了大量的表格, 且功能大多相同,因此封装了一些常用的功能, 方便多次复用. 组件封装代码: <template> <el-table :data=&qu ...

  2. vue+element ui 的表格列使用组件

    前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...

  3. 封装Vue Element的table表格组件

    上周分享了几篇关于React组件封装方面的博文,这周就来分享几篇关于Vue组件封装方面的博文,也好让大家能更好地了解React和Vue在组件封装方面的区别. 在封装Vue组件时,我依旧会交叉使用函数式 ...

  4. Vue+element UI实现表格数据导出Excel组件

    介绍 这是一个可以将页面中的表格数据导出为Excel文件的功能组件,该组件一般与表格一起使用,将表格数据传给组件,然后通过点击组件按钮可将表格中的数据导出成Excel文件. 使用方法 由于封装该组件内 ...

  5. vue element UI el-table 表格调整行高的处理方法

    这是我在工作项目中遇到的问题,我想将标记处下方的表格高度调低一点,也就是想实现下面的这个效果: 代码调整如下: 说明: 缩小:行高到一定程度之后便不能缩小. 好像最小35px.各位可以试一下. 升高: ...

  6. vue+element 实现在表格内插入其他组件,每行数据独立存储

    使用  v-slot row代表当前行

  7. 封装Vue Element的可编辑table表格组件

    前一段时间,有博友在我那篇封装Vue Element的table表格组件的博文下边留言说有没有那种"表格行内编辑"的封装组件,我当时说我没有封装过这样的组件,因为一直以来在实际开发 ...

  8. 封装Vue Element的form表单组件

    前两天封装了一个基于vue和Element的table表格组件,阅读的人还是很多的,看来大家都是很认同组件化.高复用这种开发模式的,毕竟开发效率高,代码优雅,逼格高嘛.虽然这两天我的心情很糟糕,就像& ...

  9. Element ui结合springboot的简单实战

    Eelment UI简单实战 前端开发 1 创建项目,导入element ui(略) 2 大致设计出想要的效果,如下 3 创建包 根据设计的大致模样在项目的components中创建对应的包,方便以后 ...

随机推荐

  1. 配置Nginx 扩展实现图片剪裁

    在此之前需要安装ngx_http_image_filter_module,如果是采用的Docker的话可以看看我历史文章. 然后修改配置文件,增加几个location模块,配置如下,仅供参考 serv ...

  2. pyhon的6大基本数据类型

    1.数字型(Number) 1.1 整型(int) 整型包括所有的正整数,负整数还有0. 在python中所有的整型数据全部默认采用十进制进行表示,但我们还可以手动表示其他进制的整型,具体表示如下: ...

  3. unity官网安装教程

    于今天起记录与回忆游戏开发相关一些小目标 2020-12-01 第一篇正式的博客就从软件的安装开始吧 unity个人版是免费的不用刻意去找破解版什么的 unity官网安装 1.unity.cn uni ...

  4. PyQt学习随笔:Qt中tem Views(Model-Based)和Item Widgets(Item-Based)控件的用途和关系

    在界面程序开发中,数据的展示主要包括表格.简单列表.树状列表以及纯文本等多种方式,在Qt中将界面表格.简单列表.树状列表称为"表项视图类(item view class)",并提供 ...

  5. 哪些地方会出现css阻塞,哪些地方会出现js阻塞?

    js的阻塞特性: 所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等. 直到JS下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容. 为了提高用户体验,新一代浏 ...

  6. 【Codeforces 1097F】Alex and a TV Show(bitset & 莫比乌斯反演)

    Description 你需要维护 \(n\) 个可重集,并执行 \(m\) 次操作: 1 x v:\(X\leftarrow \{v\}\): 2 x y z:\(X\leftarrow Y \cu ...

  7. MySQL事务(二)事务隔离的实现原理:一致性读

    今天我们来学习一下MySQL的事务隔离是如何实现的.如果你对事务以及事务隔离级别还不太了解的话,这里左转. 好的,下面正式进入主题.事务隔离级别有4种:读未提交.读提交.可重复读和串行化.首先我们来说 ...

  8. MySql字符集与排序规则详解

    前段时间往MySQL中存入emoji表情或生僻字.繁体字时,报错无法添加,研究后发现这是字符集编码的问题,今天就来分析一下各个字符集与排序规则 一.字符集 先说字符,字符是各种文字和符号的总称,包括各 ...

  9. The Linux Scheduler: a Decade of Wasted Cores

    The Linux Scheduler: a Decade of Wasted Cores 这是一篇介绍Linux调度问题的文章,源自这篇文章.文章中涉及到的一些问题可能已经得到解决,但可以学习一下本 ...

  10. 为什么类只能用public修饰?

    为什么类只能使用public修饰? 首先,类只能使用public修饰是一个伪命题,应该说我们只见到过使用public修饰的类,还有一些类没有访问修饰符,此时访问权限为default.其次,类实际上分为 ...