table.vue 3.29 KB
<template>
  <div class="glsx_table_box">
    <el-table class="el_table" :data="tableData" border :size="size" v-loading="loading">
      <el-table-column v-for="(item,index) in column" :key="index" :label="item.label" :prop="item.prop" :formatter="item.formatter" :align="item.align" :fixed="item.fixed" :width="item.width" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" v-if="this.$scopedSlots.operate" width="120px">
        <template slot-scope="scope">
          <slot name="operate" :scope="scope.row"></slot>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination_list">
      <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size.sync="currentPage" :page-sizes="pageSizes" :current-page.sync="currentPage" @current-change="getList" @size-change="changePageSize" />
    </div>
  </div>
</template>

<script>
/**
 * request: 接口方法
 * list: 表格数据
 * border:是否带边框
 * size:表格尺寸
 * column: 列数据
 * layout:分页展示数据
 * pageSizes: 默认展示多少页列表
 * pageSize: 当前展示页条数
 * params: 外部查询参数
 */
export default {
  name: "GlsxTable",
  props: {
    request: {
      type: Function,
    },
    list: {
      type: Array,
      default: function () {
        return [];
      },
    },
    column: {
      type: Array,
      default: function () {
        return [];
      },
    },
    border: {
      type: Boolean,
      default: true,
    },
    size: {
      type: String,
      default: "small",
    },
    layout: {
      type: String,
      default: "sizes, total,prev, pager, next, jumper",
    },
    pageSizes: {
      type: Array,
      default: function () {
        return [10, 20, 30, 40];
      },
    },
    params: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      pageTotal:0,
      tableData:[],
      loading:false
    };
  },
  created() {},
  mounted() {
    if (this.request) {
      this.getList();
    }
  },
  methods: {
    /**
     * 每页展示
     */
    changePageSize: function (e) {
      this.pageSize = e;
      this.getList();
    },

    // 获取参数
    getParams(current){
      let oParams = {...this.params};
      oParams.pageSize = this.pageSize;
      oParams.pageNumber = current || this.currentPage;
      return oParams
    },

    // 获取列表
    getList(current) {
      let request = this.request;
      let oParams = this.getParams(current);
      this.loading = true
      request(oParams).then((res) => {
        this.loading = false
        if(res.code != 200){
          return false;
        }
        let {data:{list,totalElements}} = res;
        this.tableData = list||[]
        this.pageTotal = totalElements
      }).catch(error => {
        this.loading = false
      });
    },

    // 重置
    rest() {
      this.pageSize = 10;
      this.currentPage = 1;
      this.getList();
    },
  },
};
</script>
<style lang="scss">
.el_table {
  thead {
    th {
      //background: rgba(197, 197, 197, 0.1);
      background: #f5f7fa;
    }
  }
}
</style>
<style lang="scss" scoped>
.glsx_table_box {
  margin: 10px 0;
}
.pagination_list {
  text-align: right;
  padding: 10px 0;
}
</style>