el-table组件的封装

前言:仔细看懂本篇博客,玩转element table 不成问题 ,个人理解所谓封装,就是把经常都要公用的东西,拿出来,可以多次复用。公用方法,公用页面都可以封装。

其实封装也并不是有多难,思路也很简单,就是用JS来控制页面。页面动态性越强,组件越灵活,适用范围越广。

就vue+element的组件封装而言,先把所有功能在子页面实现。然后把js里面的动态值,拿到父组件里面去传过来,就完成了,其中技术也就掌握父子组件传值而已。
该组件封装适应于绝大多数table列表,可以自定义列

废话不多说看下例子

先上子组件代码 

<template><div class="table-box"><el-table ref="tableData" v-loading="loading" style="width: 100%" :row-key="rowKey" :data="tableData" :height="tableHeight" highlight-current-row v-bind="tableInfo" :span-method="objectSpanMethod" v-on="events" @selection-change="handleSelectionChange"><slot name="expand" /><el-table-column v-if="selectAllTemplate" min-width="140" label="全选模板列表"><template slot-scope="{ row }"><div><el-checkbox v-model="row.all" @change="muBanAllToggleSelection($event, tableData, row)">{{ row.muBanMC }}</el-checkbox></div></template></el-table-column><!-- 多选 --><el-table-column v-if="needSelect" type="selection" reserve-selection width="55" align="center" /><el-table-column v-if="hasIndex" label="序号" width="50" type="index" /><template v-for="(item, index) in tableColumn"><!-- 此列需要自定义 --><el-table-column v-if="item.isSlot" :key="'%' + index" :show-overflow-tooltip="showOverflowTooltip" v-bind="item" :render-header="item.renderHeader" v-on="events"><template slot-scope="{ row, $index }"><ex-slot v-if="item.render" :render="item.render" :row="row" :index="$index" :column="item" :class="item.prop" :default-value="item.defaultValue" /><slot v-else :name="item.prop" :row="row" /></template></el-table-column><!-- 正常列 --><el-table-column v-else :key="'%' + index" :show-overflow-tooltip="showOverflowTooltip" v-bind="item" v-on="events" /></template><el-table-column v-if="hasOperation" fixed="right" label="操作" :min-width="operationWidth"><!-- <template v-if="!btnButton || btnButton.length === 0" slot-scope="scope"><slot name="operation" :row="scope.row" :index="scope.$index" /></template> --><template v-if="btnButton.length" slot-scope="{ row, column, $index }"><el-button v-show="value.isShow(row, column, $index)" :disabled="value.isDisable(row, column, $index)" v-for="(value, i) in btnButton" :key="'$' + i" size="small" :type="value.type" :icon="value.icon" :class="value.class" @click="value.callback(row, column, $index)">{{ value.text }}</el-button></template></el-table-column></el-table><!-- 分页 --><div v-if="isNeedPagination" style="display: flex; justify-content: flex-end; align-item: centerl; padding-top: 15px"><el-paginationref="pagination":page-sizes="pageSizes":page-size.sync="computedPageSize":hide-on-single-page="isSinglePageHide"layout="total, sizes, prev, pager, next, jumper":current-page.sync="computedCurrentPage":total="total":pager-count="pagerCount"@current-change="currentChange"@size-change="sizeChange"/></div></div>
</template>
<script>
// 自定义组件的内容
const exSlot = {functional: true,props: {row: Object,render: Function,index: Number,column: {type: Object,default: null},defaultValue: [Number, String]},render: (h, ctx) => {const params = {row: ctx.props.row,index: ctx.props.index}const defaultValue = ctx.props.defaultValueparams.column = ctx.props.column || {}return h('div',{class: [params.column.prop || '', params.column.class || params.column.className || ''].join('')},[ctx.props.render(h, params) || defaultValue])}
}
export default {name: 'PublicTable',components: {'ex-slot': exSlot},props: {// keyrowKey: {type: String,default: ''},// 超出行是否隐藏不换行showOverflowTooltip: {type: Boolean,default: true},// 是否需要多选needSelect: {type: Boolean,default: false},// 是否需要序号hasIndex: {type: Boolean,default: false},// 是否需要分页isNeedPagination: {type: Boolean,default: true},// 是否单页隐藏,默认为trueisSinglePageHide: {type: Boolean,default: false},// 当前页页码,支持.sync修饰符currentPage: {type: Number,default: 1},// 页码显示数据量pagerCount: {type: Number,default: 7},// 每页数据条数, 支持.sync修饰符默认为每页10条pageSize: {type: Number,default: 20},// 数据总条数total: {type: Number,default: 0},// 每页多少数据pageSizes: {type: Array,required: false,default: () => [20, 40, 80, 100]},// 表格绑定的属性tableInfo: {type: Object,default: () => {}},// 获取数据时是否需要加载loadingloading: {type: Boolean,default: false},// table数据源tableData: {type: Array,default: () => []},// 表格项绑定的属性tableColumn: {type: Array,default: () => []},// 是否需要操作列hasOperation: {type: Boolean,default: true},// 操作列btnButton: {type: Array,default: () => []},// 操作列宽度operationWidth: {type: String,default: '120px'},// 表格方法events: {type: Object,default: () => {}},// 合并单元格objectSpanMethod: {type: Function,default: () => {}},// 全选项绑定的函数muBanAllToggleSelection: {type: Function,default: () => {}},// 是否显示全选项selectAllTemplate: {type: Boolean,default: false}},data() {return {}},computed: {computedCurrentPage: {get() {return this.currentPage},set(val) {this.$emit('update:currentPage', val)}},computedPageSize: {get() {return this.pageSize},set(val) {this.$emit('update:pageSize', val)}},tableHeight() {return !this.isNeedPagination ? '100%' : 'calc(100% - 47px)'}},mounted() {},methods: {getTableRef() {return this.$refs.tableData},getRefPagination() {return this.$refs.pagination},// 页面切换事件  通过 @currentChange 绑定currentChange(val) {this.$emit('currentChange', val)},// 每页条数切换事件,通过@sizeChange 绑定sizeChange(val) {this.$emit('sizeChange', val)},handleSelectionChange(val) {this.$emit('selectionChange', val)}}
}
</script>
<style lang="scss" scoped>
.table-box {flex: 1;overflow: hidden;width: 100%;height: 100%;
}
</style>

再上父组件代码

<template><div class="tableBox"><PublicTableref="zhenDuanWHTable"class="table":loading="loading":current-page="searchParams.pageNum":total="total":page-size="searchParams.pageSize":table-data="tableData":table-info="tableInfo":table-column="columns":btn-button="operations":events="events"operation-width="150px"@sizeChange="handleSizeChange"@currentChange="handleCurrentChange"/></div>
</template><script>
import tableInfo from './mixins/tableInfo.js'
import PublicTable from '@/components/PublicTable/index.vue'
export default {components: {PublicTable},mixins: [tableInfo],data() {return {}},methods: {}
}
</script><style lang="scss" scoped>
.tableBox {display: flex;flex-direction: column;width: 100%;height: 600px;.table {height: calc(100% - 48px);}
}
</style>

再上mixins/tableInfo.js代码

import mockData from './mockData'
export default {data() {return {// 获取列表前是否loading加载loading: false,// 搜索查询的参数searchParams: {pageNum: 1,pageSize: 10},// table数据源tableData: mockData,// 表格项绑定的属性columns: [{prop: 'title',label: '审核描述',minWidth: '100px',align: 'center',formatter: row => (row.title ? row.title : '暂无标题')},{prop: 'statusDesc',minWidth: '100px',align: 'center',label: '审核状态'},{prop: 'createBy',minWidth: '100px',align: 'center',label: '申请人'},{sortable: 'custom',prop: 'createdTime',minWidth: '100px',align: 'center',label: '申请时间'},{prop: 'auditBy',minWidth: '100px',align: 'center',label: '审核人'},{minWidth: '100px',align: 'center',prop: 'auditTime',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'A',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'B',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'C',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'D',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'},{minWidth: '100px',align: 'center',prop: 'E',label: '审核时间'}],// 表格绑定的属性tableInfo: {stripe: true,'highlight-current-row': true // 选中行高亮},events: {'row-dblclick': row => {//双击表格项触发的函数console.log(row)},'row-click': row => {// 单机表格项触发的函数console.log('row', row)}},// 操作列operations: [{text: '编辑',isShow: row => true, // 是否展示isDisable: row => true, // 是否禁用type: 'text',class: 'el-text-color',callback: row => {this.handleAddOrEdit('edit', row)}}],total: 0}},mounted() {},methods: {// 每也条数改变handleSizeChange(pageSize) {this.searchParams.pageSize = pageSize},// 改变页数handleCurrentChange(pageNum) {this.searchParams.pageNum = pageNum},handleAddOrEdit(type, row) {if (type === 'edit') {this.visible = truethis.infoData = row}}}
}

假数据mockData(不用管)

export default [{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'},{title: 'A',statusDesc: 1,createBy: '甲',createdTime: '2023-01-01 23:59:59',auditBy: '乙',auditTime: '2023-01-02 23:59:59'}
]

最后附上效果图

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/132730.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣 -- 516. 最长回文子序列

解题步骤&#xff1a; 参考代码&#xff1a; class Solution { public:int longestPalindromeSubseq(string s) {int ns.size();vector<vector<int>> dp(n,vector<int>(n));//记得从下往上填表for(int in-1;i>0;i--){//记得i是小于等于j的for(int ji;j&l…

SQL:left join、right join 究竟什么区别?

1、SQL join 分三种 1&#xff09;inner join&#xff08;内连接&#xff0c;也叫等值连接&#xff09; 显示两个表中有联系的所有数据&#xff0c;是默认方式。 2&#xff09;cross join&#xff08;交叉连接&#xff09; 两个表格做笛卡尔积&#xff0c;显示的数据行数是…

多服务器云探针源码(服务器云监控)/多服务器多节点_云监控程序python源码

源码简介&#xff1a; 多服务器云探针源码(服务器云监控),支持python多服务器多节点&#xff0c;云监控程序源码。它是一款很实用的云探针和服务器云监控程序源码。使用它可以帮助管理员能够快速监控和管理各种服务器和节点&#xff0c;实用性强。 源码链接&#xff1a; 网盘…

办公室人人在用的iTab桌面真的好用吗?

本人坐标北京&#xff0c;在一家中型互联网公司当社畜多年。最近发现一个奇怪的现象&#xff0c;我工位前后左右的同事都跟我在用一样的浏览器桌面——iTab新标签页。我表示莫非真的英雄所见略同&#xff1f; 我是去年1月份在刷B站时偶然刷到一条评论&#xff0c;有人分享自己…

Xception:使用Tensorflow从头开始实现

一、说明 近年来&#xff0c;卷积神经网络已成为计算机视觉领域的主要算法&#xff0c;开发设计它们的方法一直是相当的关注。Inception模型似乎能够用更少的参数学习更丰富的表示。它们是如何工作的&#xff0c;以及它们与常规卷积有何不同&#xff1f;本文将用tensorflow实现…

(部署服务器系列一)虚拟机模拟部署服务器

1、下载安装vmware 15 &#xff08;win7最高支持版&#xff09; 2、下载安装CentOS 配置2核2g&#xff08;最少&#xff09;磁盘100g&#xff08;不会实际占有&#xff09;选择时区-上海配置分区&#xff1a;https://blog.csdn.net/qq_35363507/article/details/127390889 &a…

Exposure Normalization and Compensation for Multiple-Exposure Correction 论文阅读笔记

这是CVPR2022的一篇曝光校正的文章&#xff0c;是中科大的。一作作者按同样的思路&#xff08;现有方法加一个自己设计的即插即用模块以提高性能的思路&#xff09;在CVPR2023也发了一篇文章&#xff0c;名字是Learning Sample Relationship for Exposure Correction。 文章的…

基于Docker来部署Nacos的注册中心

基于Docker来部署Nacos的注册中心 准备MySQL数据库表nacos.sql&#xff0c;用来存储Nacos的数据。 最终表结构如下&#xff1a; 在本地nacos/custom.env文件中&#xff0c;有一个MYSQL_SERVICE_HOST也就是mysql地址&#xff0c;需要修改为你自己的虚拟机IP地址&#xff1a; …

iview表格 异步修改列数据卡顿 滚动条失效

使用表格row-key属性 将row-key属性设置为true <Table ref"table" border :row-key"true" :columns"tableColumns" :loading"loading":data"tableData"></Table>

JFLASH基本使用总结

注意&#xff0c;不同版本的操作略有不同&#xff0c;本教程以J-Flash V5.12f为例。 烧录文件 如果是刚打开J-Flash&#xff0c;会弹出这样的一个工程选择界面&#xff0c;可以选择已有工程&#xff0c;或者创建新的工程&#xff0c;我们这里选择创建新工程。 注意&#xff0…

瑞芯微 | 如何固定以太口地址为指定ip?

rxw的RK3568的evb1公板&#xff0c;有2个以太口&#xff0c; 默认UI界面只能配置eth0&#xff0c;无法配置eth1&#xff0c; 实际应用中&#xff0c;有时需要一旦有网线插入&#xff0c;就需要该地址设置为指定IP地址。 本文介绍2个最简单的方法实现固定IP。 一、通过修改i…

ChatGLM流式输出的报错修复

ChatGLM中的openai_api.py中的代码如下&#xff1a; # codingutf-8 # Implements API for ChatGLM2-6B in OpenAIs format. (https://platform.openai.com/docs/api-reference/chat) # Usage: python openai_api.py # Visit http://localhost:8000/docs for documents.import …