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

组件封装代码:

<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. 题解 CF546B Soldier and Badges

    CF546B Soldier and Badges 简单的贪心qwq 排个序,如果当前数与之前的数相重,已经用过,则加到一个之前没有用过的数 #include<cstdio> #inclu ...

  2. mybatis - 执行 getById

    1. getById 的执行 前面一篇 提到过, Mapper.java 创建的时候, 会通过 jdk 代理的方式来创建, 且代理处理类为: MapperProxy . 所以当执行 UserMappe ...

  3. Java开发中模拟接口工具moco的使用

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...

  4. 深度学习之反向传播算法(BP)代码实现

    反向传播算法实战 本文仅仅是反向传播算法的实现,不涉及公式推导,如果对反向传播算法公式推导不熟悉,强烈建议查看另一篇文章神经网络之反向传播算法(BP)公式推导(超详细) 我们将实现一个 4 层的全连接 ...

  5. 1、TensorFlow如何工作?

    TensorFlow特殊的张量计算引擎使得TensorFlow能够很好的满足机器学习的计算需要,从2015年开始发起 本书基于TensorFlow0.12+和python3.0+ 环境安装要求 pip ...

  6. Linux格式化数据盘

      一块全新的数据盘挂载到ECS实例后,您必须创建并挂载至少一个文件系统.本示例使用I/O优化实例,操作系统为CentOS 7.6,为一块新的20GiB数据盘(设备名为/dev/vdb)创建一个MBR ...

  7. POJ - 1845 Sumdiv(分治)

    题意:求$A^{B}$的所有约数之和$mod\ 9901$ 思路:由结论有,一个数$n$进行质因数分解得到$n={p_{1}}^{c_{1}} * {p_{2}}^{c_{2}} *...* {p_{ ...

  8. tp5的输入和验证

    规则和模板 好像要写一样名字,只需要引入模板

  9. Java的单例模式(singleton)

    为什么需要单例?只因为国家的独生子女政策(当然现在可以生2个) 单例是一个很孤独的物种,因为它的类里面做多只有也仅只有它一个. 常见的是懒汉及饿汉模式, 1.懒汉,为什么这么叫,看看英文,原为lazy ...

  10. bitnami-redmine 一键安装

    下载bitnami-redmine https://bitnami.com/stack/redmine 安装 选择语言 设置登陆用户名和密码,数据库用户名root,数据库密码也是这个设置的密码 其他下 ...