因为在做后台管理项目的时候用到了大量的表格, 且功能大多相同,因此封装了一些常用的功能, 方便多次复用。

组件封装代码:

<template>
<el-table :data="tableData" size="medium"
ref="multipleTable" border fit
@sort-change="handleSort"
@filter-change="filterHandler"
@selection-change="handleSelectionChange">
<!-- 多选框 -->
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column v-for="(th, key) in tableHeader"
min-height="46"
:key="key"
:prop="th.prop"
:label="th.label"
:fixed="th.fixed"
:sortable="th.custom?'custom':false"
:filters="th.filters"
:column-key="th.columnKey"
:filtered-value="th.filteredValue"
:filter-multiple="th.filterMultiple"
:min-width="th.minWidth" align="center">
<template slot-scope="scope">
<!-- 操作按钮 -->
<div v-if="th.operation">
<el-button v-for="(o, key) in th.operation" :key="key"
@click="o.clickFun(scope.row)"
style="width:100%"
type="text" size="mini">
{{o.name}}
      </el-button>
</div>
<!-- 点击跳转页面 -->
<div v-else-if="th.router">
<router-link :to="{path: th.router.path, query: {expertFields: scope.row['fieldName']}}">{{scope.row[th.prop]}}</router-link>
</div>
<div v-else>
<!-- 鼠标滑过显示气泡框 -->
<el-popover v-if="th.describe"
popper-class="popover-el-class"
placement="bottom"
width="200"
trigger="hover"
:content="scope.row[th.prop]">
<span class="describe-wrap" slot="reference" style="-webkit-box-orient:vertical">{{ scope.row[th.prop] }}</span>
</el-popover>
<!-- 下拉选择框 -->
<el-select v-else-if="th.selected" :disabled="!th.disabled" v-model="scope.row[th.prop]" @change="th.changeFunc" clearable>
<el-option v-for="(item, index) in th.selected" :value="item.value" :label="item.text" :key="index"></el-option>
</el-select>
<!-- 纯展示数据 -->
<span v-else-if="!th.formatData">{{ scope.row[th.prop] }}</span>
<!-- 需要格式化的数据结构 -->
<span v-else>{{ scope.row[th.prop] | formatters(th.formatData) }}</span>
</div>
</template>
</el-table-column>
</el-table>
</template> <script>
export default {
name: 'comp-table',
props: {
tableData: {
type: Array,
default: function () {
return []
}
},
tableHeader: {
type: Array,
default: function () {
return []
}
},
multipleSelection: {
type: Array,
default: function () {
return []
}
}
},
  filters: {  
formatters (val, format) {
if (typeof (format) === 'function') {
return format(val)
} else return val
}
  },
methods: {
handleSelectionChange (val) {
this.$emit('update:multipleSelection', val)
},
handleSort (sort) {
this.$emit('sort-events', sort)
},
filterHandler (filters) {
this.$emit('filter-events', filters)
}
}
}
</script>

页面内调用:

<comp-table :tableData="tableData"
:tableHeader="tableHeader"
:multipleSelection.sync="multipleSelection"
@filter-events="filterEvents"
@sort-events="sortEvents">
</comp-table> <script>
export default {
data () {
return {
tableData: [], // 请求到的表格数据
tableHeader: [ // 表头信息
   { prop: 'fieldName',
  label: '领域',
  filters: domainTypeData,
  columnKey: 'fieldType',
  filterMultiple: false,
  minWidth: '150px',
   fixed: true
  },
  { prop: 'fieidDetails', label: '详情', minWidth: '180px' },
  { prop: 'lawyerQuantity',
  label: '关联律师数量',
   minWidth: '150px',
   router: {path: '/'}
   },
  { prop: 'articlesNumber',
   label: '相关文章数量',
  router: {path: '/case-management'},
  minWidth: '150px'
   },
  { prop: 'operation',
  label: '相关服务',
  minWidth: '260px',
  style: {display: 'block'},
   operation: [
  {name: '服务方案一', clickFun: this.getServiceOne},
  {name: '服务方案二', clickFun: this.getServiceTwo},
  {name: '服务方案三', clickFun: this.getServiceThird}
  ]
  },
  { prop: 'gmtModified', custom: 'custom', label: '最后更新时间', minWidth: '180px', formatData: this.formatDate },
  { prop: 'updater', label: '最后更新人', minWidth: '120px' },
  { prop: 'operation',
  label: '操作',
  minWidth: '260px',
   operation: this.fieldStatus ? [
  {name: '上线', disabled: true, clickFun: this.onLineField},
  {name: '下线', underline: true, clickFun: this.underField}
   ] : [
   {name: '编辑', clickFun: this.editDomain},
  {name: '删除', clickFun: this.delField},
  {name: '待审核', clickFun: this.examineField}
  ]
  }
   ]
}
}
}
</script>

vue + element ui table表格二次封装 常用功能的更多相关文章

  1. Vue+element ui table 导出到excel

    需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...

  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 table组件封装,使用render渲染

    后台管理经常会用到表格,一开始封装了一个常用的功能性表格,点击这里: 后来由于需求增加,在表格中还会用到switch,select,input等多种组件,每次都要在html中增加<el-tabl ...

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

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

  6. element ui table 表格排序

    实现elementui表格的排序 1:给table加上sort-change,给table每一项加上sortable和column-key,排序是根据column-key来进行排序的 <el-t ...

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

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

  8. vue + element ui 表格自定义表头,提供线上demo

    前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...

  9. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

随机推荐

  1. Springmvc-crud-03(静态资源错误)

    错误描述:静态资源加载失败 原因:spring会拦截静态资源 解决办法: <!-- 配置spring支持静态资源请求 --> <mvc:default-servlet-handler ...

  2. 6_8 树(UVa548)<从中序和后序恢复二叉树>

    你的任务是找出一棵二叉树中最小路径上终端节点(树叶,leaf node)的值.所谓路径乃指从根节点(root)旅行到任一终端节点.路径的值为所经过的节点的值的和(包含根节点及终端节点).而最小路径就是 ...

  3. pod的状态及操作

    查看pod的标签 [root@master pod]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cv- ...

  4. docker安装后启动报错

    docker安装后启动不起来: 查看日志  /var/log/message    其中有一行为:  Your kernel does not support cgroup memory limit ...

  5. 吴裕雄 python 人工智能——基于Mask_RCNN目标检测(3)

    import os import sys import random import math import re import time import numpy as np import cv2 i ...

  6. TensorFlow:使用inception-v3实现各种图像识别

    程序来自博客: # https://www.cnblogs.com/felixwang2/p/9190740.html上面这个博客是一些列的,所以可以从前往后逐一练习. # https://www.c ...

  7. FTD vs FMC

    要管理思科防火墙(ASA或Firepower),我们有两种方法: 1.FirePower Threat Defense software (FTD) 2.Firesight Management Ce ...

  8. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  9. Django框架之登录案例

    内容: (1)request.GET和request.POST (2)获取get方法提交和post方法提交的数据 一.登录案例 登录逻辑代码 def login(request): if reques ...

  10. 《Web安全攻防 渗透测试实战指南 》 学习笔记 (五)

    Web安全攻防 渗透测试实战指南   学习笔记 (五)   第四章 Web安全原理解析  (一) (一)SQL注入的原理 1.web应用程序对用户输入数据的合法性没有判断. 2.参数用户可控:前端传给 ...