vue+element table的二次封装

iTable.vue文件

<template>
  <div>
    <el-table
      :data="data"
      :show-header="showHeader"
      :border="border"
      :max-height="maxHeight"
      :v-loading="loading"
      :class="className"
      element-loading-text="给我一点时间"
      stripe
      fit
      highlight-current-row
      style="width: 100%"
    >
      //使用template 顺序不乱
      <template v-for="colConfig in colConfigs">
        //slot 添加自定义配置项
        <slot
          v-if="colConfig.slot"
          :name="colConfig.slot"
        ></slot>
        //component 特殊处理某一项
        <component
          v-else-if="colConfig.component"
          :is="config.component"
          :col-config="colConfig"
        ></component>
        <el-table-column
          v-else
          v-bind="colConfig"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
      </template>

    </el-table>
  </div>
</template>
<script>
export default {
  name: 'iTable',
  props: {
    colConfigs: {
      type: Array,
      default: function() {
        return [];
      }
    },
    data: {
      type: Array,
      default: function() {
        return [];
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    border: {
      type: Boolean,
      default: true
    },
    maxHeight: {
      type: [String, Number],
      default: null
    },
    loading: {
      type: Boolean,
      default: false
    },
    className: {
      type: String,
      default: ""
    }
  }
};
</script>

<style scoped>
</style>

在组件中使用

先引入组件 

import iTable from "@/components/i-table";

注册组件

components: { iTable }

使用

html代码

      <iTable :data="userList" :col-configs="colConfigs" row-key="id">

        <el-table-column slot="sex" label="性别">
          <template slot-scope="scope">
            {{scope.row.sex === 0 ? "男" : "女"}}
          </template>
        </el-table-column>
        <el-table-column slot="operate" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="deleteUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </iTable>

script代码

    colConfigs: [
        
        { slot: "operate" },
        { prop: "name",label: "姓名" },
        { slot: "sex" },
        { prop: "birth", label: "出生日期" },
        { prop: "age", label: "年龄" },
        { prop: "addr", label: "地址" },
        
      ],

就可以使用了

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值