当前位置:   article > 正文

封装el-table

封装el-table

封装组件

<template>
  <!-- 表格分页 -->
  <div class="table_box">
    <el-table :data="tableData" style="width: 100%" :border="border" :header-cell-style="{background:'#f6f9ff'}" :cell-class-name="cellStyle">
      <el-table-column type="index" :index="indexMethod" align="center" label="序号" width="65" v-if="showIndex">
      </el-table-column>
      <el-table-column v-for="(item, index) in columns" :key="index" :align="item.align" :prop="item.prop" :label="item.label" :fixed="item.fixed" :width="item.width" :sortable="item.sortable" :min-width="item.minWidth" :show-overflow-tooltip="item.showOverflowTooltip">
        <template slot-scope="scope">
          <slot v-if="item.slot" :row="scope.row" :name="item.slot"></slot>
          <span v-else>{{ scope.row[item.prop] || "--" }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total, sizes, prev, pager, next, jumper" :current-page="currentPage" :page-size="pageSize" @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total > 10000 ? 10000 : total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  props: {
    border: {
      type: Boolean,
      default: false,
    },
    showIndex: {
      type: Boolean,
      default: false,
    },
    tableData: {
      type: Array,
      default: [],
    },
    columns: {
      type: Array,
      default: [],
    },
    total: {
      type: Number,
      default: 0,
    },
    currentPage: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    cellStyle: {
      type: Function,
    },
  },
  data() {
    return {};
  },
  created() {},
  mounted() {
    this.cellStyle;
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("changeSize", val);
    },
    handleCurrentChange(val) {
      this.$emit("changeCurrent", val);
    },
    //表单选择全选
    handleSelectionChange(val) {
      this.$emit("handleSelectionChange", val);
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * 10;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  padding: 10px;
  text-align: right;
  background: #fff;
}
// .table_box {
//   /deep/.el-table__header-wrapper {
//     .tableHeader {
//       th {
//         background-color: #f6f9ff !important;
//       }
//       .el-table_1_column_1 {
//         background-color: #f6f9ff !important;
//       }
//     }
//   }
//   // .tableHeader {
//   //   border: 1px solid #f40;
//   //   background-color: #f6f9ff !important;
//   // }
// }
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

使用

1.引入组件

import Table from "@/components/Table/index.vue";
  • 1

2.注册组件

components: {
    Table,
  },
  • 1
  • 2
  • 3

3.配置表头数据

data() {
    return {
      columns: [
        {
          prop: "userId",
          label: "用户ID",
          minWidth: 70,
          showOverflowTooltip: true,
        },
        {
          prop: "username",
          label: "用户账号",
          minWidth: 100,
          showOverflowTooltip: true,
        },
        {
          prop: "authUserName",
          label: "认证姓名",
          minWidth: 80,
          showOverflowTooltip: true,
        },
        {
          prop: "authIdNo",
          label: "认证身份证号",
          minWidth: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "authMobile",
          label: "认证手机号码",
          minWidth: 100,
          showOverflowTooltip: true,
        },
        {
          prop: "authBankNo",
          label: "认证银行卡号",
          minWidth: 140,
          showOverflowTooltip: true,
        },
        {
          slot: "personAuthResult",
          label: "认证状态",
          minWidth: 80,
          showOverflowTooltip: true,
        },
        {
          prop: "registerTime",
          label: "注册时间",
          minWidth: 120,
          showOverflowTooltip: true,
        },
        {
          slot: "lockFlag",
          label: "账号状态",
          minWidth: 80,
          showOverflowTooltip: true,
        },
        {
          slot: "action",
          label: "操作",
          minWidth: 130,
        },
      ],
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
    };
  },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

4.配置组件

<div class="tableWrap">
      <Table :tableData="tableData" :columns="columns" :cellStyle="cellStyle" :total="total" :currentPage="currentPage" :pageSize="pageSize" @changeSize="handleSizeChange" @changeCurrent="handleCurrentChange">
        <template slot="personAuthResult" slot-scope="scope">
          {{ personAuthResultObj[scope.row.personAuthResult] }}
        </template>
        <template slot="lockFlag" slot-scope="scope">
          <el-switch v-model="scope.row.lockFlag" active-color="#13ce66" inactive-color="#ff4949" active-value="0" inactive-value="1" @change="changelockFlag(scope.row)"></el-switch>
        </template>
        <template slot="action" slot-scope="scope">
          <el-button type="text" @click="particular(scope.row)">详情</el-button>
        </template>
      </Table>
    </div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/621609
推荐阅读
相关标签