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

组件封装代码:

<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. 通过颜色绘制图片UIImage

    + (UIImage *)clearImageView { UIColor *color=[UIColor clearColor]; CGRect rect =CGRectMake(,,,); UIG ...

  2. jQuery的ajax方法的属性以及jsonp的随机数问题

    原文地址[ http://www.cnblogs.com/know/archive/2011/10/09/2204005.html ] <!DOCTYPE html PUBLIC "- ...

  3. 表与java类关系

    总结: 表名对应类名,字段名对应属性名 java:多对多:各自类中添加一个对方类集合的属性 一对多:一的一方添加一个对方类集合的属性  ,多的一方添加一个对方类的属性    一对一:各自类中添加一个对 ...

  4. animate使用方法

    链接:https://www.cnblogs.com/xiaohuochai/p/7372665.html

  5. java作业 11.10

    package text3; import java.io.File; import java.io.IOException; import java.nio.file.Files; public c ...

  6. 消息队列(五)--- RocketMQ-消息存储2

    概述 RocketMQ存储中主要用到以下知识点: mmap 文件映射 内存池 异步刷盘 consumeQueue 同时本节将介绍各个重要的类,本篇文章将介绍 mmap 文件映射的相关方法和内存池相关知 ...

  7. MYSQL双查询错误2

    一.关键点 MYSQL双查询错误之所以产生,有两个关键点: (1)SQL语句中使用GROUP BY语句时会生成临时表: (2)RAND()在查询和存储时生成的随机数有可能不同. 补充:======== ...

  8. Unable to create a debugging engine.

    用QT Creator调试的时候报如下错误: Unable to create a debugging engine. QT里面打开Tools -> Options -> Kits 发现D ...

  9. C:clock() 计算代码执行时间

    clock():捕捉从程序开始运行到clock()被调用时所耗费的事件. 这个时间的单位是 clock tick,即时钟打点 常数 CLK_TCK:机器时钟每秒走的时钟打点数 要使用这个函数需要包含头 ...

  10. 杭电 1059 Dividing

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...