element-table二次封装

封装

<template>
  <el-table :data="data" v-bind="attrs" >
    <el-table-column 
      v-for="(item,index) in columnOptions"
      :key="index" 
      :label="item.label" 
      :prop="item.prop"
      >
        <template slot-scope="scope">
          <slot :name="item.slotName?item.slotName:''" :row="scope.row" >
            {{scope.row[item.prop]}}
          </slot>
        </template>
      </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope" >
        <slot name="operation" :row="scope.row">
          <el-button icon="el-icon-search" @click="search(scope.row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" @click="deleteButton(scope.row)" ></el-button>
        </slot>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  props:{
    // 数据
    data:{
      type: Array,
      require: true,
    },
    // 列的属性数组
    columnOptions:{
      type: Array,
      require: true,
    },
    // element table 的属性
    attrs: Object,
  },
  methods:{
    // 删除按钮
    deleteButton(row){
      this.$emit('deleteBtn',row)
    },
    search(row){
      this.$emit("searchBtn",row)
    },
    edit(row){
      this.$emit("editBtn",row)
    },
  }
}
</script>

<style>

</style>

使用

<template>
  <div>
    <h1>table</h1>
    <Mytable :data="data" :attrs="attrs" :columnOptions="columnOptions" @deleteBtn="del" @searchBtn="searchBtn" @editBtn="editBtn">
      <template slot="statusName" slot-scope="scope">
        <el-tag
          :type="scope.row.status ? 'success' : 'danger'"
        >{{ scope.row.status ? "是" : "否" }}</el-tag>
      </template>
    </Mytable>
  </div>
</template>

<script>
import Mytable from './Mytable.vue';
export default {
  name:'test',
  components:{Mytable},
  data(){
    return {
      attrs: {
        border: true,
      },
      data: [
        {id:'001',title:'title11111',status:0,},
        {id:'002',title:'title22222',status:1,},
        {id:'003',title:'title33333',status:0,},
        {id:'004',title:'title44444',status:1,},
      ],
      columnOptions:[
        {label:'条目ID',prop:'id'},
        {label:'条目title',prop:'title'},
        {label:'条目status',prop:'status',slotName:'statusName'},
      ]
    }
  },
  methods:{
    del(row){
      console.log('del',row);
    },
    searchBtn(row){
      console.log('searchBtn',row);
    },
    editBtn(row){
      console.log('editBtn',row);
    },
  }
}
</script>

<style>

</style>

attrs  element table 的属性

columnOptions  配置列的属性

columnOptions 里的 slotName:'statusName'  可以配置具名插槽  有 scoped 数据

<template slot="statusName" slot-scope="scope">
  <el-tag
    :type="scope.row.status ? 'success' : 'danger'"
   >{{ scope.row.status ? "是" : "否" }}</el-tag>
</template>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值