Elemnt ui 组件封装(table)

<template>
 <div class="table">
  <el-table
    :data="tableData2"
    :border="tableConfig.border"
    style="min-width: 800px"
    :size="tableConfig.size"
    :cell-style="tableConfig.cellStyle"
    :header-cell-style="tableConfig.headCellStyle"
    :stripe="tableConfig.stripe"
  >
    <slot name="col"></slot>
    <el-table-column
      v-for="(col, index) in colsData"
      :prop="col.prop"
      :key="col.prop"
      :label="col.label"
      :width="tableConfig.colWidth"
    >
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <slot name="operate">
          <el-button-group>
            <el-button
              style="margin-right: 20px"
              type="primary"
              size="small"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="warning"
              size="small"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </el-button-group>
        </slot>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      style="margin-top: 20px"
      v-if="tableConfig.pagination"
      :currentPage="currentPage"
      :page-size="pageSize"
      :page-sizes="[5, 10, 20, 30]"
      background
      layout="sizes, prev, pager, next"
      :total="tableData.length"
      @size-change = "handleSizeChange"
      @current-change = "handleChange"
    >
  </el-pagination>
 </div>
</template>

<script>
export default {
  name: "rTable",
  data() {
    return {
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: { 
    tableData2() {
       return this.tableData.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize);
    }
  },
  props: {
    tableData: {
      type: Array,
      required: true,
    },
    tableConfig: {
      type: Object,
      //表单配置
      default: function() {
        return {
          border: true,
          size: "small",
          cellStyle: { textAlign: "center" },
          headCellStyle: { textAlign: "center" },
          stripe: true,
          colWidth: "180",
          //是否显示分页
          pagination: true
        };
      },
    },
    colsData: {
      type: Array,
      required: true,
    }
  },
  methods: {
    handleEdit: function (row) {
      this.$emit("edit", row);
    },
    handleDelete: function (row) {
      this.$emit("delete", row);
    },
    //页码改变时触发
    handleSizeChange: function(val) {
      this.pageSize = val;
    },
    handleChange(val) {
      this.currentPage = val;
    }
  },
  mounted() {},
};
</script>

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

posted @ 2022-03-16 13:56  ajajaz  阅读(100)  评论(0编辑  收藏  举报