element UI 封装table组件

tableWrapper.js

<template>
  <div>
    <el-table
      :height="settings.height"
      v-loading="settings.isLoading"
      :header-cell-style="{background:'#FAFAFA',color:'rgba(0,0,0,0.85)',height:'54px'}"
      @selection-change="e => handleClick('select',e)"
      :data="data"
      style="width: 100%"
    >
      <el-table-column v-if="settings.isSelection" width="55" type="selection" fixed align="center"></el-table-column>
      <el-table-column v-if="settings.isIndex" type="index" :index="1" fixed align="center"></el-table-column>
      <template v-for="(item,index) in header">
        <el-table-column
          v-if="item.prop!=='action'"
          :key="item.prop"
          :type="item.type"
          :label="item.label"
          :width="item.width"
          :fixed="item.fixed"
          align="center"
        >
          <template slot-scope="scope">
          //过滤器
            <span v-if="item.filter">{{obj.sex(1)}}</span>
            <span v-if="!item.filter">{{
          item.formatter
          ? item.formatter(scope.row[item.prop || item.showField])
          : scope.row[item.prop || item.showField]
          }}</span>
          </template>
        </el-table-column>

        <el-table-column
          v-if="item.prop==='action'"
          :key="item.prop"
          :label="item.label"
          :width="item.width"
          :fixed="item.fixed"
          align="center"
        >
          <template slot-scope="scope">
            <template v-if="item.arr">
              <el-button
                size="mini"
                :type="item2.type=='delete'?'danger':'text'"
                v-for="item2 in item.arr"
                :key="item2.type"
                @click="item2.fun(scope.$index, scope.row)"
              >{{item2.name}}
              </el-button>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <el-pagination
      v-if="settings.isPagination"
      background
      style="text-align:right;padding:6px 0"
      @size-change="e=>handleClick('pageSize',e)"
      @current-change="e=>handleClick('currentPage',e)"
      :current-page="currentPage"
      :page-sizes="[20, 50, 100, 200]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="settings.total"
    ></el-pagination>
  </div>
</template>

<script>
  export default {
    name: "index",
    data() {
      return {
        obj: {
          sex: function (sex) {
            if (sex === 1) {
              return '女'
            }
            return '男'
          }
        }
      }
    },
    props: {
      data: {type: Array, default: () => []},
      header: {type: Array, required: true},
      settings: {
        type: Object,
        default: () => {
          return {
            height: null,
            isBorder: false,
            isLoading: false,
            isIndex: false,
            isSelection: false,
            isPagination: false,
            currentPage: 1,
            total: 20
          };
        }
      }
    },
    computed: {
      currentPage: function () {
        return this.settings.currentPage;
      }
    },
    
    methods: {
      handleClick(type, e, i) {
        this.$emit("select", e);
      }
    }
  };
</script>

<style lang="" scoped>
</style>

组件内引入

<table-wrapper @select="handleSelect" :data="tableData" :header="tableHeade" :settings="tableSettings"></table-wrapper>
import tableWrapper from '@/components/tableWrapper'
components: {
  tableWrapper,
},
data() {
  return {
    tableData: [],
    tableHeade: [
      {prop: "name", label: "姓名", width: 120},
      {prop: "phone", label: "联系方式", width: 120, fixed: true},
      {prop: "job", label: "工作", width: 80},
      {prop: "type", label: "类型", width: 160},
      {prop: "status", label: "信用金审核状态", width: 160},
      {prop: "time1", label: "时间1", width: 160},
      {prop: "time1", label: "时间2", width: 160},
      {
        prop: "action", label: "操作", fixed: 'right', arr: [{
          type: 'text',
          name: '编辑',
          fun: this.fun
        }]
      },
    ],
    tableSettings: {
      height: "calc(90vh - 300px)",
      title: "element UI 封装table组件",
      isPagination: true,
      total: 100,
      isSelection: true
    },
  }
},
methods:{
  fun(index, row) {
    console.log(index, row)
  },
  handleSelect(e) {
     console.log(e)
  },
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值