Vue Element UI Table 组件 Table表自动生成 Table 封装 (详细教程)



Vue Element UI Table 组件封装


本文提及

Vue http 请求封装包
Element UI Table 分页教程
Vue 组件通信


文章解析

table 组件封装

条件分页获取数据
自定义table 表头
自定义table 权限
自定义字段是否必须
自定义是否自动分页
自定义日期格式
返回响应 修改 查看 删除 操作
返回调用者信息

Template



Script

import {get, getp, deletep} from '../../../config/http.js'
  import bus from '../../../src/eventBus.js'

  export default {
    name: 'Table',
    data () {
      return {
        centerDialogVisible: false,
        cols: [],
        cols_hidden: 'false',
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableData: [],
        jurisdictions: [],
        pageauto: true, 
        useridentification: ''
      } 
    }, methods: {
    // cols 表头   jurisdictions 权限     自动分页 pageauto url 数据地址 params 参数   useridentification 调用者标识
      async custom_table (cols, jurisdictions, pageauto, url, params, useridentification) {
        this.useridentification = useridentification
        this.tableData = ''
        this.cols = cols
        // this.tableData = tableData
        this.jurisdictions = jurisdictions
        this.pageauto = pageauto
        let result = ''
        if (pageauto) {
          if (params && params !== undefined && params.length !== 0 && params !== '' && params != null && params !== false) {
            console.log('有参查询' + params.toString())
            result = await getp(url, params)
          } else {
            console.log('无参查询')
            result = await get(url)
          }
          this.total = result.length
          this.currentPage = 1
          this.tableData = result
          console.log('custom table')
          console.log(this.tableData)
          console.log(this.jurisdictions)
          console.log(this.cols)
          console.log(url)
        } else {
          //手动分页

        }
      },
      handlejurisdiction (functon, data) {
        /* 方法名 数据 调用者*/
        let confrim = window.confirm('确认删除吗')
        if (functon === 'delete') {
          if (confrim) {
            bus.$emit('handlejurisdiction', functon, data, this.useridentification)
          } else {
            this.$message('删除取消')
          }
        } else {
          bus.$emit('handlejurisdiction', functon, data, this.useridentification)
        } 
      }, closedcenterDialogVisible () {
        // alert('关闭窗口')
      }, openedcenterDialogVisible () {
        // alert('打开窗口')
      },
      dateFormat: function (row, column) {
        var date = row[column.property]
        if (date === undefined) {
          return ''
        }
        // moment(date).format('YYYY-MM-DD')
        return date.slice(0, 10)
      }, handleSizeChange (pageSize) {
        this.pageSize = pageSize
        this.currentPage = 1
      }, handlecurrentPageChange (currentPage) {
        this.currentPage = currentPage
      }
    }, beforeDestroy () { 
      bus.$off('handlejurisdiction')
    },
  }




你可能感兴趣的:(Vue,Element,UI,Encapsulation,vue,element,ui,element,table,table,封装,vue,table)