el-table 组件二次封装(vue2)

PublicTable.vue

<!-- 公共表格组件 -->
<template><div class="table-common"><el-table v-loading="loading" :ref="tableid" border style="width: 100%" :data="tableDatas" :row-key="rowKey"@selection-change="handleSelectionChange" @select="selectDataChange" @select-all="selectAllDataChange":height="tableHeight" :header-cell-style="headerCellStyle" :row-style="rowStyle" highlight-selection-row:max-height="maxHeight" @row-click="handleRowClick" :tooltip-effect="configFlag.tooltip || 'dark'"><!-- 当数据为空时,如果想添加一个表达是空数据的图片,可以在这里设置,如果没有就不用管了 --><template slot="empty"><el-empty class="empty_box" :image-size="55"><template slot="image"><img src="@/assets/icons/images/nodata.png"></template></el-empty></template><!-- 全选单选 --><el-table-column v-if="configFlag.selection" align="center" width="55" type="selection":reserve-selection="configFlag.reserveSelection" /><!-- 序号列 --><el-table-column type="index" v-if="configFlag.index" :label="configFlag.indexName || '序号'":width="configFlag.indexWidth || 50" align="center" :fixed="configFlag.indexFixed || 'left'"><template slot-scope="scope"><!-- 每页都是从 1 开始 -->{{ scope.$index + 1 }}<!-- 第二页从 11 开始 --><!-- {{ (pageValue.pageNum - 1) * 10 + (scope.$index + 1) }} --></template></el-table-column><!-- 循环遍历表头展示数据 --><el-table-column v-for="item in columnsCopy" :key="item.fieldIndex" :width="item.width || ''":min-width="item.minWidth || ''" :prop="item.fieldIndex" :label="item.label" :align="item.align || 'center'":sortable="item.sortable" :fixed="item.fixed || false" header-align="center":show-overflow-tooltip="item.showOverFlowTooltip" :filters="item.filters ? item.filters : null":filter-method="item.filters ? item.filtersMethod : null" :selectable="(row) => !row.isSelected"><template slot="header" slot-scope="{ column }">{{ column.label }}<el-tooltip class="item" effect="dark" v-if="item.headertip" :content="item.headertip" placement="top-start"><i class="el-icon-warning"></i></el-tooltip></template><template slot-scope="scope"><!-- {{ scope }} --><!-- 枚举值 --><div v-if="item.type == 'statMap'">{{ statMaps(item.options)[scope.row[item.fieldIndex]] || "--" }}</div><!-- 需提示过长信息 --><div v-else-if="item.showOverFlowTooltip"><div class="show-tooltip">{{ scope.row[item.fieldIndex] || "--" }}</div></div><!-- 保留两位小数 --><div v-else-if="item.type == 'tofix2'">{{scope.row[item.fieldIndex]? Number(scope.row[item.fieldIndex]).toFixed(2): "--"}}</div><!-- 金额千分位展示,项目需求,所以暂时就加上了,主要是做个示例,大家可以参考怎么自定义列的一些方法 --><div v-else-if="item.type == 'money'"><span>{{ getMoneyK(scope.row[item.fieldIndex]) || "--" }}</span></div><!-- 根据需求添加效果 返回的slot可以优化.自己来吧.可以实现操作列等 --><slot v-else-if="item.type == undefined && item.slotname" :scope="scope" :field="item.fieldIndex":name="item.slotname"></slot><!-- 最普通的情况 --><div v-else><span>{{ scope.row[item.fieldIndex] || "--" }}</span></div></template></el-table-column></el-table><el-pagination small v-if="configFlag.needPage" background :total="pageValue.total" :page-count="pageValue.pageNum":page-size="pageValue.pageSize" :page-sizes="pageSizes" :current-page.sync="pageValue.pageNum":layout="pageValue.pageLayout || pageLayout" class="_pagination" @size-change="sizeChange"@current-change="currentChange" @prev-click="prevClick" @next-click="nextClick" /></div>
</template><script>
export default {name: "Table",data() {return {tableDatas: [],loading: true,pageLayout: "total,prev, pager, next, jumper",columnsCopy: []};},props: {// 多选保存选中数据rowKey: {default: () => (row) => row.index,},// 为表头设计样式headerCellStyle: {default: () => {return { background: "rgb(243,248,254)" };},},// 为每一行设计样式,比如设置每行的高度等等rowStyle: {default: () => {return { height: "50px" };},},columns: {// 表头数据  文案和绑定值,以及需要特殊处理的slottype: Array,default: () => [],},tableData: {type: Array, // 后台数据default: () => [],},tableid: {type: String,default: "publicTable",},// 分页参数pageValue: {// 分页数据type: Object,default: () => {return {pageNum: 1,pageSize: 10,total: 0,currentPage: 1, //当前页};},},// 每页多少条的选项pageSizes: {type: Array,default: () => {return [10, 20, 50, 100];},},// 表格配置项configFlag: {// 配置  其他table配置依次添加type: Object,default: () => {return {needPage: false, // 是否需要分页selection: false, // 是否需要多选index: false, // 是否需要序号indexWidth: 70, //序号列宽btn: false, //序号添加自定义htmlreserveSelection: false,tooltip: 'dark'// 这里不全面,可根据实际情况添加};},},tableHeight: {// 可以监听屏幕高度获取。// 高度// type: Number || String ,default: () => null,},maxHeight: {// 可以监听屏幕高度获取。// 最大高度type: Number || String,default: () => 900,},// 是否可以点击行选中多选框rowClickSelect: {type: Boolean,default: true}},watch: {tableData: {handler: function (newvalue) {this.tableDatas = newvalue;this.loading = false;},deep: true,},columns: {handler: function (newvalue) {this.columnsCopy = newvalue;},deep: true,}},beforeUpdate() {this.tableDatas = this.$props.tableData;this.loading = false;},computed: {},mounted() {this.columnsCopy = this.$props.columns;},methods: {// 用于表格中字段是枚举值的列 比如性别 0 代表女 1代表男,就不需要对后台数据单独处理了handleRowClick(row) {if (this.rowClickSelect) {row.isSelected = !row.isSelected;this.$refs[this.tableid].toggleRowSelection(row);}},statMaps(list) {if (!list) return;let obj = {};list.forEach((item) => {obj[item.value || item.id] = item.label || item.value;});return obj;},// 金额千位展示:1,234,567,890getMoneyK(money) {if (typeof money === "number") {money = money.toString();}var pattern = /(-?\d+)(\d{3})/;while (pattern.test(money)) {money = money.replace(pattern, "$1,$2");}return money;},// 清空选中clearSelected() {// 父组件通过ref调用clearSelected方法,例如 this.$refs.clearTable.clearSelected()this.$refs[this.tableid]?.clearSelection();},/*默认选中需要默认选中的在组件中通过this.$refs[tableid].selected(默认选中的数据:Array)*/selected(data) {if (data.length > 0) {data.forEach((item) => {this.$refs[this.tableid].toggleRowSelection(item, true);});}},// 设置条数sizeChange(size) {this.$emit("sizeChange", size);},// 翻页,直接跳转currentChange(page) {this.$emit("handleChange", page);},//上一页prevClick(val) {this.$emit("prevClick", val);},//下一页nextClick(val) {this.$emit("nextClick", val);},// 多选handleSelectionChange(val) {this.$emit("handleSelectionChange", val);},//点击选中取消选中selectDataChange(val, row) {this.$emit("selectDataChange", val, row);},selectAllDataChange(val) {this.$emit("selectAllDataChange", val);},// 多选handleSelection(val, row) {this.$emit("handleSelection", { val, row });},handleCellEnter(row, column, cell, event) {this.$emit("handleCellEnter", { row, column, cell, event });},//编辑handleEdit(index, row, colIndex, field) {this.$emit("handleEdit", { index, row, colIndex, field });},//下拉框事件onSelected(index, row, field) {this.$emit("onSelected", { index, row, field });},//按钮点击事件onClickBtn(index, row) {this.$emit("onClickBtn", { index, row });},filterHandler(value, row, column) {const property = column["property"];return row[property] === value;},/**重构表格布局 */handleResize() {this.$nextTick(() => {this.$refs[this.tableid].doLayout();});},},
};
</script>
<style lang="scss" scoped>
.show-tooltip {overflow: hidden;white-space: nowrap;text-overflow: ellipsis;cursor: pointer;
}/* 修改Tooltip样式 */
::v-deep .el-tooltip__popper.is-dark {background-color: #fff;/* 设置白色背景 */color: #000;/* 设置黑色字体 */
}._pagination {width: 100%;margin: 5px 0;text-align: center;
}.table-common {height: calc(100% - 30px);
}::v-deep .empty_box {.el-empty__description {margin-top: 0;p {line-height: 50px;}}
}
</style>

用法:

<template><div><public-table ref="taskTable" :rowKey="(row) => row.id" :tableData="tableData" :columns="columns" :configFlag="tableConfig" @handleSelectionChange="selectChange" @sizeChange="handleSizeChange" @handleChange="handlePageChange" table-height="100%"></public-table></div>
</template>
<script>
export default {data(){columns:[{ label: '任务名称', fieldIndex: 'taskName', showOverFlowTooltip: true },],tableConfig:{needPage: false, // 是否需要分页index: true, // 是否需要序号selection: true, // 是否需要多选reserveSelection: false, // 是否需要支持跨页选择indexFixed: 'left', // 序号列固定},tableData:[], // 表格数据pageValue: {// 分页参数total: 0,pageNum: 1,pageSize: 10,pageLayout: "total,sizes,prev,pager,next,jumper"},selectList:[], // 表格选中数据},methods:{// 表格多选事件selectChange(e){this.selectList = e;    },// 表格分页-页容改变handleSizeChange(e) {this.pageValue.pageSize = e;this.pageValue.pageNum = 1;},// 表格分页-页数改变handlePageChange(e) {this.pageValue.pageNum = e;}}
}
</script>

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

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

相关文章

pgsql指令、pg在docker中打开

常规 linux 在安装pgsql的服务器上 root是用户名&#xff0c;test是数据库名 psql -U root -d test登录后显示&#xff0c;12,6是版本号 psql (12.6) 查询所有表 \dt查询表结构 \d table_name查询表所属 \dp manual_logistics_logSELECT n.nspname AS table_schema,c.re…

alpine openssl 编译

./config no-shared --prefix/usr/local/openssl apk add musl-dev gcc g apk add linux-headers ssh root 登录 编辑 SSH 配置文件 打开 SSH 配置文件 /etc/ssh/sshd_config&#xff1a; vi /etc/ssh/sshd_config PermitRootLogin yes

一文解决数据库【事务】相关问题

文章目录 前言一、事务的基本概念二、ACID 特性1.原子性&#xff08;Atomicity&#xff09;2.一致性&#xff08;Consistency&#xff09;3.隔离性&#xff08;Isolation&#xff09;4.持久性&#xff08;Durability&#xff09; 三、事务的操作1.开始事务&#xff08;BEGIN TR…

Mac苹果电脑 java前后端开发环境及软件安装教程

本文记录我初次使用macOS系统&#xff0c;m4 mini安装开发软件及环境的全过程&#xff0c;希望能帮助到你&#xff0c;好用的请点赞评论收藏增加热度&#xff0c;让更多Mac小白轻松体验开发&#xff0c;20241129 …

dfs—acwing

题目一&#xff1a;排序数字 842. 排列数字 - AcWing题库 分析 考虑dfs&#xff0c;其实stl——next_permutation也可以 路径存储开一个数组&#xff0c;不能重复搜索&#xff0c;加一个标记数组 代码 #include<bits/stdc.h> using namespace std;int path[10]; int…

一步一步写线程之十六线程的安全退出之一理论分析

一、多线程的开发 多线程的开发&#xff0c;在实际场景中几乎是无法避开的。即使是前端看似没有使用线程&#xff0c;其实在底层的框架中也使用了线程进行了支撑。至少到现在&#xff0c;不管是协程还是其它什么新的编程方式&#xff0c;仍然无法撼动线程的主流地位。 多线程的…

ISAAC SIM踩坑记录--添加第三方3D场景

ISAAC SIM仿真首先就是要有合适的3D场景&#xff0c;官方提供了一些场景&#xff0c;如果不能满足要求&#xff0c;那就只能自己建。 对于我这种不会3D建模的菜鸟&#xff0c;只能到网上下载了&#xff0c;sketchfab就是一个不错的平台&#xff0c;有不少免费资源可以下载。 …

人工智能_大模型091_大模型工作流001_使用工作流的原因_处理复杂问题_多轮自我反思优化ReAct_COT思维链---人工智能工作笔记0236

# 清理环境信息&#xff0c;与上课内容无关 import os os.environ["LANGCHAIN_PROJECT"] "" os.environ["LANGCHAIN_API_KEY"] "" os.environ["LANGCHAIN_ENDPOINT"] "" os.environ["LANGCHAIN_TRACING_V…

完全按照手册win10里装Ubuntu 虚拟机然后编译ESP32(主要是想针对ESP32C3和S3)开发板的鸿蒙系统(失败)

基本上完全按照手册来的&#xff0c;除了Ubuntu虚拟机使用了22.04 Jammy版本&#xff0c;鸿蒙手册里是20.04 版本&#xff0c;主要是鸿蒙里3年前的手册了&#xff0c;所以就擅自用了高版本。 据此还想到一点&#xff0c;鸿蒙LiteOS&#xff0c;还挺稳定的&#xff0c;3年也没有…

一文理解多模态大语言模型——下

作者&#xff1a;Sebastian Raschka 博士&#xff0c; 翻译&#xff1a;张晶&#xff0c;Linux Fundation APAC Open Source Evangelist 编者按&#xff1a;本文并不是逐字逐句翻译&#xff0c;而是以更有利于中文读者理解的目标&#xff0c;做了删减、重构和意译&#xff0c…

【Leetcode Top 100 - 扩展】328. 奇偶链表

问题背景 给定单链表的头节点 h e a d head head&#xff0c;将所有索引为奇数的节点和索引为偶数的节点分别组合在一起&#xff0c;然后返回重新排序的列表。 第一个 节点的索引被认为是 奇数 &#xff0c; 第二个 节点的索引为 偶数 &#xff0c;以此类推。 请注意&#xf…

使用伪装IP地址和MAC地址进行Nmap扫描

使用伪装IP地址和MAC地址进行Nmap扫描 在某些网络设置中&#xff0c;攻击者可以使用伪装的IP地址甚至伪装的MAC地址进行系统扫描。这种扫描方式只有在可以保证捕获响应的情况下才有意义。如果从某个随机的网络尝试使用伪装的IP地址进行扫描&#xff0c;很可能无法接收到任何响…

k8s 之 Role-Based Access Control

在 Kubernetes 中&#xff0c;RBAC&#xff08;Role-Based Access Control&#xff09;是一个用来控制对 Kubernetes 资源访问的授权机制。它通过定义不同角色&#xff08;Role&#xff09;和这些角色可以访问的权限&#xff0c;确保只有被授权的用户或服务能够执行特定的操作。…

什么是TCP的三次握手?

TCP的三次握手&#xff1a;深入理解建立可靠连接的过程 引言 在计算机网络中&#xff0c;传输控制协议&#xff08;TCP&#xff09;是确保数据可靠传输的核心协议之一。TCP通过三次握手机制来建立一个稳定的、双向的连接&#xff0c;这对于确保数据的完整性和顺序至关重要。本…

SpringBoot该怎么使用Neo4j - 优化篇

文章目录 前言实体工具使用 前言 上一篇中&#xff0c;我们的Cypher都用的是字符串&#xff0c;字符串拼接简单&#xff0c;但存在写错的风险&#xff0c;对于一些比较懒的开发者&#xff0c;甚至觉得之间写字符串还更自在快速&#xff0c;也确实&#xff0c;但如果在后期需要…

MySQL中on和where的区别

select name, bonus from Employee left join Bonus on Employee.EmpId Bonus.EmpId where bonus is null or bonus < 1000 作者&#xff1a;力扣官方题解 链接&#xff1a;https://leetcode.cn/problems/employee-bonus/ 来源&#xff1a;力扣&#xff08;LeetCode&#x…

数据科学与大数据之间的区别

什么是数据科学&#xff1f; 数据科学是一个跨学科领域&#xff0c;它将统计学和计算方法相结合&#xff0c;旨在从数据中提取见解和知识。它涉及收集、处理、分析以及解读数据&#xff0c;以揭示可用于为决策过程提供依据并推动创新的模式、趋势和关系。 数据科学涵盖了广泛…

neo4j如何存储关于liquidity structure的层次和关联结构

在 Neo4j 中存储关于流动性结构&#xff08;liquidity structure&#xff09;的层次和关联结构非常适合&#xff0c;因为 Neo4j 是一个基于图的数据库&#xff0c;能够自然地建模和存储复杂的关系和层次结构。下面是如何在 Neo4j 中设计和实现这样的数据模型的详细步骤和示例。…

七牛云成功保存但无法显示和访问{“error“:“download token not specified“}

在使用七牛云存储图片时&#xff0c;前端通过链接访问图片时遇到错误&#xff1a; {"error":"download token not specified"} 具体表现为&#xff1a; 后端通过 access_key 和 secret_key 生成了上传和下载的 Token。前端将域名与 res.key 拼接后生成图…

智慧银行反欺诈大数据管控平台方案(四)

智慧银行反欺诈大数据管控平台的核心内容&#xff0c;是通过整合多维度、多层次的金融交易信息&#xff0c;利用先进的大数据分析、机器学习与人工智能算法&#xff0c;构建一个系统性、实时性和智能化的反欺诈管控网络&#xff0c;旨在提供全面、高效、精准的风险评估机制。该…