后台系统,table被用的次数比较多,所以决定提出来作为组件

1.新建一个Table.vue文件

<!--region 封装的分页 table-->
<template>
 <div class="table">
 <el-table id="iTable" v-loading.iTable="options.loading" :data="list" :stripe="options.stripe"
    ref="mutipleTable"
    @selection-change="handleSelectionChange">
  <!--region 选择框-->
  <el-table-column v-if="options.mutiSelect" type="selection" style="width: 55px;">
  </el-table-column>
  <!--endregion-->
  <!--region 数据列-->
  <template v-for="(column, index) in columns">
  <el-table-column :prop="column.prop"
:key='column.label'
       :label="column.label"
       :align="column.align"
       :width="column.width">
   <template slot-scope="scope">
   <template v-if="!column.render">
    <template v-if="column.formatter">
    <span v-html="column.formatter(scope.row, column)"></span>
    </template>
    <template v-else>
    <span>{{scope.row[column.prop]}}</span>
    </template>
   </template>
   <template v-else>
    <expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom>
   </template>
   </template>
  </el-table-column>
  </template>
  <!--endregion-->
  <!--region 按钮操作组-->
  <el-table-column ref="fixedColumn" label="操作" align="center" :width="operates.width" :fixed="operates.fixed"
      v-if="operates.list.filter(_x=>_x.show === true).length > 0">
  <template slot-scope="scope">
   <div class="operate-group">
   <template v-for="(btn, key) in operates.list">
    <div class="item" v-if="btn.show" :key='btn.id'>
    <el-button :type="btn.type" size="mini" :icon="btn.icon" :disabled="btn.disabled"
       :plain="btn.plain" @click.native.prevent="btn.method(key,scope.row)">{{ btn.label }}
    </el-button>
    </div>
   </template>
   </div>
  </template>
  </el-table-column>
  <!--endregion-->
 </el-table>  </div>
</template>
<!--endregion-->
<script>
 export default {
 props: {
  list: {
  type: Array,
  default: []
  }, // 数据列表
columns: {
  type: Array,
  default: []
  }, // 需要展示的列 === prop:列数据对应的属性,label:列名,align:对齐方式,width:列宽
  operates: {}, // 操作按钮组 === label: 文本,type :类型(primary / success / warning / danger / info / text),show:是否显示,icon:按钮图标,plain:是否朴素按钮,disabled:是否禁用,method:回调方法
  options: {
  type: Object,
  default: {
   stripe: false, // 是否为斑马纹 table
   highlightCurrentRow: false // 是否要高亮当前行
  },
  } // table 表格的控制参数
 },
//组件
 components: {
  expandDom: {
  functional: true,
  props: {
   row: Object,
   render: Function,
   index: Number,
   column: {
   type: Object,
   default: null
   }
   },
  render: (h, ctx) => {
   const params = {
   row: ctx.props.row,
   index: ctx.props.index
  }
   if (ctx.props.column) params.column = ctx.props.column
    return ctx.props.render(h, params)
  }
  }
 },
// 数据
 data () {
  return {
  pageIndex: 1,
  multipleSelection: [] // 多行选中
  }
 },
 mounted () {
 },
 computed: {
 },
 methods: {   // 多行选中
  handleSelectionChange (val) {
  this.multipleSelection = val
  this.$emit('handleSelectionChange', val)
  },
  // 显示 表格操作弹窗
  showActionTableDialog () {
console.log(4444)
  this.$emit('handelAction')
  }
 }
 }
</script>
<style lang="scss" > </style>

 2.调用组件

<template>
 <div class="table-page">
 <i-table :list="list"
@handleSelectionChange="handleSelectionChange"
    :options="options"
    :columns="columns"
    :operates="operates"
  >
 </i-table>
 </div>
</template>
<script>
 import iTable from '../components/Table'
 export default {
  components: {iTable},
  data () {
   return {
    list: [
     {
id: '24',
title: '编号3',
state:0
      },
     {
id: '23',
title: '编号3',
state:1
      },
     {
id: '23',
title: '编号3',
state:2
      },
     {
id: '2',
title: '编号3',
state:0
      },
     {
id: '223',
title: '编号3',
state:1
      },
     {
id: '2444',
title: '编号3',
state:1
      },
], // table数据
    options: {
     stripe: true, // 是否为斑马纹 table
     loading: false, // 是否添加表格loading加载动画
     highlightCurrentRow: true, // 是否支持当前行高亮显示
     mutiSelect: true, // 是否支持列表项选中功能
    }, // table 的参数
    columns: [
     {
      prop: 'id',
      label: '编号',
      align: 'center',
     },
     {
      prop: 'title',
      label: '标题',
      align: 'center',
      formatter: (row, column, cellValue) => {
       return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
      }
     },
     {
      prop: 'state',
      label: '状态',
      align: 'center',
      render: (h, params) => {
       return h('el-tag', {
       props: {type: params.row.state === 0 ? 'success' : params.row.state === 1 ? 'info' : 'danger'} // 组件的props
       }, params.row.state === 0 ? '上架' : params.row.state === 1 ? '下架' : '审核中')
      }
     },     ], // 需要展示的列
    operates: {
     width: 200,
     fixed: 'right',
     list: [
      {
id:'1',
       label: '编辑',
       type: 'warning',
       show: true,
       icon: 'el-icon-edit',
       plain: true,
       disabled: false,
       method: (index, row) => {
        this.handleEdit(index, row)
       }
      },
      {
id:'2',
       label: '删除',
       type: 'danger',
       icon: 'el-icon-delete',
       show: true,
       plain: false,
       disabled: false,
       method: (index, row) => {
        this.handleDel(index, row)
       }
      }
     ]
    } // 列操作按钮
   }
  },
  methods: {
   // 选中行
   handleSelectionChange (val) {
    console.log('val:', val)
   },
   // 编辑
   handleEdit (index, row) {
    console.log(' index:', index)
    console.log(' row:', row)
   },
   // 删除
   handleDel (index, row) {
    console.log(' index:', index)
    console.log(' row:', row)
   }
  }
 }
</script>

  https://juejin.im/post/5a6941dc518825732258e321

vue封装element中table组件的更多相关文章

  1. Vue. 之 Element获取table中选中的行

    Vue. 之 Element获取table中选中的行 问题描述: 如下截图,在Table中选择数据后,然后在点击“统计”按钮,获取Table表中选择的行 解决方案: 1. 给“统计”这个按钮添加一个点 ...

  2. VUE -- 对 Element UI table中数据进行二次处理

    时间——日期 后台经常给我们返回的是 时间戳 (例如:1535620671) 这时候我们页面展现的时候需要将时间戳转换为我们要的格式 例如 (YYYY-MM-DD HH:mm:ss) 如果是在Elem ...

  3. Element UI table组件源码分析

    本文章从如下图所示的最基本的table入手,分析table组件源代码.本人已经对table组件原来的源码进行削减,源码点击这里下载.本文只对重要的代码片段进行讲解,推荐下载代码把项目运行起来,跟着文章 ...

  4. react ,ant Design UI中table组件合并单元格并展开详情的问题

    需求:购物车订单列表,如图: 一:单元格合并 遇到这种你会怎么办呢?  单元格合并?  还是其他的方法? 下面是我的处理方式,就是在table 组件的columns上处理,这里拿商品举例,其余的类似, ...

  5. element ui table组件自定义合计栏,后台给的数据

    合计的数据是后台传的,所以用table组件自定义一行用来合计 <el-table border fit v-loading.body="listLoading" elemen ...

  6. vue封装一个弹框组件

    这是一个提示框和对话框,例:   这是一个组件 eject.vue <template> <div class='kz-cont' v-show='showstate'> &l ...

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

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

  8. 纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件

    现在实现的有基础用法.可清空.密码框,参考链接:https://element.eleme.cn/#/zh-CN/component/input HTML代码:想要测试哪个组件,直接将对应组件解开注释 ...

  9. 普通element ui table组件的使用

    1.使用基础的element ui 的table的基础使用 首先,使用前要先引用element库到项目中,可以直接引入element的js和css或者在vue项目下按需加载不同的组件 废话不多说,直接 ...

随机推荐

  1. css隐藏滚动条 兼容谷歌、火狐、IE等各个浏览器

    项目中,页面效果需要展示一个页面的移动端效果,使用的是一个苹果手机样式背景图,咋也没用过苹果,咋也不敢形容. 如下图所示: 在谷歌浏览器如图一滚动条顺利隐藏,但是火狐就如图二了,有了滚动条丑的一批. ...

  2. 连接数据库出现错误:1045-Access denied for user 'root'@'localhost'解决方法

    Navicat for MySQL 链接: https://pan.baidu.com/s/1slwQxVB 密码: r737 1.出现这个问题的原因之一是权限的问题,也就是说你的电脑可能没有权限访问 ...

  3. docker持久化之存储卷

         启动时敲:   docker run -it -v /var/mydata:/abc myos   #前面写真实机里的文件夹,冒号后面写docker里面的文件夹,最后写镜像名 (可以同时挂载 ...

  4. 【转】交换分区SWAP

    SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对 ...

  5. win32 界面 背景图片

    case WM_PAINT: { HBITMAP hbm; BITMAP bminfo; hbm = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE ...

  6. Qt的QSettings类和.ini文件读写

    Detailed Description QSettings类提供了持久的跨平台的应用程序设置.用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表 ...

  7. python3-继承和多态

    在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Supe ...

  8. python字典总结

    今天总结一篇关于字典的知识点> 字典也是python提供的一种常用的数据结构,它用于存放具有映射关系的数据 比如成绩表,语文:34,数学:99,如果单纯的使用两个列表保存这组数据,则无法记录两组 ...

  9. maven 依赖显示红线 pom文件不显示红线的一种可能问题

    pom文件引用的是CDH的jar包 而没有配置CDH的仓库 导致maven找不到资源  ,依赖显示红色波浪,并且在仓库内生成了一堆.lastupdate文件 解决: 1. 删除本地仓库内所有的.las ...

  10. 基于flask的可视化动漫分析网站【python入门必学】

    课程设计项目名称:基于flask的可视化动漫分析网站,如果你在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而而流一思(数 ...