vue + element ui table表格二次封装 常用功能

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

组件封装代码:

<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>

原文地址:https://www.cnblogs.com/puerile/p/11898086.html

时间: 2024-08-30 01:54:40

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

vue+element ui table组件封装,使用render渲染

后台管理经常会用到表格,一开始封装了一个常用的功能性表格,点击这里: 后来由于需求增加,在表格中还会用到switch,select,input等多种组件,每次都要在html中增加<el-table-column></el-table-column>, 维护起来相当麻烦,就想到了使用render渲染. 组件内部封装代码: <template> <el-table :data="tableData" size="medium"

vue + element ui 阻止表单输入框回车刷新页面

问题 在 vue+element ui 中只有一个输入框(el-input)的情况下,回车会提交表单. 解决方案 在 el-form 上加上 @submit.native.prevent 这个则会阻止表单回车提交. 测试 下面的代码出现表单回车提交 <body> <div id="app"> <el-form ref="form" :model="form" label-width="80px"&

Vue框架Element UI教程(二)

原文:https://www.jianshu.com/p/1704b5935a8 [时间选择器] Element UI手册:https://cloud.tencent.com/developer/doc/1270 中文文档:http://element-cn.eleme.io/#/zh-CN github地址:https://github.com/ElemeFE/element 前一篇已经安装好了Element UI环境,现在开始来实际操作框架提供的一些组件的运用了. 在准备好以下文章里面的内容

vue+element UI实现分页组件

1.前言 在web页面中,常常需要将后台查询到的数据以表格形式展示出来,而这些数据量往往会非常庞大,如果将所有要展示的数据一次性请求获取并展示到页面上,那页面长度势必会变得非常的长,很不美观.更重要的是,如果数据量过于大,在页面加载时一次性请求全部数据将会耗费大量网络资源,性能极低.但是,如果我们可以将数据分页展示,这样页面首先不会变的冗长,另外只有用户点击页码才会发出请求并且每次请求的数据量也不会很大,这就极大的节省了网络资源,提高了性能.本文就以vue结合element UI实现一个数据分页

vue+element UI实现多级导航菜单

1.前言 在某次日常开发中,项目要求页面的导航菜单需要动态加载,即菜单不能在页面上写死,菜单上的数据由后端开发从数据库中获取返回给前端使用,前端拿到数据后再通过解析数据最终将菜单渲染出来.由于菜单有可能是多级的,所以我们需要使用递归的方式,一层一层的递归数据,将多级菜单完整显示出来.本篇博文借助element UI组件库中的Navmenu导航菜单组件,将其进行改造后封装成一个多级导航菜单组件. 2.工作流程 组件封装好之后,由父组件调用该组件,父组件先向后端发送请求获取菜单数据,然后将菜单数据传

vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、一级mockjs用法、路由懒加载三种方式

不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p>Element UI简单Cascader级联选择器使用</p> <el-cascader :options='options' v-model="selectedOptions" @change="handleChange"> </el-

vue+element ui项目总结点(三)富文本编辑器 vue-wangeditor

1.参考 https://www.npmjs.com/package/vue-wangeditor 使用该富文本编辑器 <template> <div class="egit_box"> <p>富文本编辑器试用</p> <div class="text_box" style="width: 100%;display: flex;justify-content: center;"> <

Vue+element UI实现“回到顶部”按钮组件

介绍 这是一个可以快速回到页面顶部的组件,当用户浏览到页面底部的时候,通过点击按钮,可快速回到页面顶部. 使用方法 由于该组件是基于element-UI进行二次封装的,所以在使用该组件时请务必安装element-UI(安装方式猛戳这里),安装好element-UI后,只需将该组件文件夹BackToTop导入到现有项目中即可使用. 使用示例 <template> <div id="app"> <!--可自定义按钮的样式.show/hide临界点.返回的位置

VUE+Element UI实现简单的表格行内编辑效果

原理是通过Css控制绑定的输入控件与显示值,在选中行样式下对控件进行隐藏或显示 1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <!-- 引入样式 --> 7 <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-default/