表格组件二次封装(vue-elementui)

TableVue组件

<template>
  <!--  table再组件化-->
  <!-- 通过v-bind="$attrs" v-on="$listeners",把父组件传的属性全部绑定到子组件上,保证了api和el-table一致 -->
  <el-table style="width: 100%" v-bind="$attrs" v-on="$listeners" border >
    <!-- 多选框   -->
<!--    <el-table-column type="selection" width="55"></el-table-column>-->
    <!-- :scope是绑定的动态属性,可以起任意喜欢的名字,但要注意在父组件中获取数据时key要对应 -->
    <el-table-column v-for="(item, index) in columns" :key="index" v-bind="item.attrs">
      <el-table-column type="selection" v-if="item.type==='selection'" width="55" />
      <template slot-scope="scope" >
        <slot :scope="scope" :name="item.slot" v-if="item.slot"></slot>
        <template v-else>
          {{scope.row[item.attrs.prop]}}
        </template>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  props: {
    columns: {
      type: Array,
      default: () => []
    }
  }
}
</script>

<style scoped>
  .el-table{
    margin-top: 10px;
  }
</style>

在父组件引入

html部分:

 <!--引入表格组件        -->
 <TableVue v-loading="loadingTable" :columns="columns" :data="list" empty-text="暂无数据">
     <!-- #是v-slot的简写,{scope: {row, $index}}是属性对象slot双重解构,注意这里的scope要与子组件插槽绑定的属性名对应 -->
     <template #handle="{scope: {row, $index}}">
       <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)">编辑</el-button>
       <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row, $index)">删除</el-button>
     </template>
   </TableVue>

js部分:

import TableVue from '@/components/TableVue'
export default {
  components: { TableVue },
  data() {
    return {
    }
  }
}

export default {data(){return{ }}}里面

loadingTable: false,
list: [],
columns: Object.freeze([
    { attrs: { prop: 'communityName', label: '小区', width: '140', align: 'center', 'show-overflow-tooltip': true }},
    { attrs: { prop: 'buildingName', label: '栋', width: '100', align: 'center', 'show-overflow-tooltip': true }},
    { slot: 'handle', attrs: { label: '操作', width: '120', 'class-name': 'small-padding fixed-width', align: 'center' }}
])

methods:{}里面:

// 按编辑按钮,弹出对话框
    handleUpdate(row) {
      this.dialogVisible = true
      this.isEdit = true
      this.editInfo = Object.assign({}, row)
    },
handleDelete(row) {
      console.log(row)
      this.query.communityId = row.communityId
      this.query.buildingId = row.buildingId
      this.query.unitId = row.unitId
      this.query.residentId = row.id
      const query = this.query
      this.$confirm('是否确认删除"' + row.residentName + '"的住户信息?', '警告',
        { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }
      ).then(function() {
        return delResident(query)
      }).then((res) => {
        this.getList()
        this.$message({
          message: res.message,
          type: res.code === 2000 ? 'success' : 'error'
        })
      }).catch(function() {
      })
    },

按编辑按钮出现的弹出框自己写,或者参考下面的(这个是另外写的组件,编辑和新增共用了同一个)。不了解的戳这里了解更多:vue-elementui新增编辑共用弹出框组件

<el-dialog :title="isEdit?'编辑住户信息':'添加住户信息'" :visible.sync="dialogVisible" :edit.sync="isEdit" width="700px">
   <new-dialog v-if="dialogVisible" :visible.sync="dialogVisible" :edit.sync="isEdit" :edit-info="editInfo" />
</el-dialog>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值