antd-vue的table二次封装

<template>
  <a-table
    :pagination="pagination"
    :dataSource="tableList"
    :columns="columns"
    :rowKey="rowKey"
    v-bind="$attrs"
    @change="onTableChange"
  >
    <template v-for="custom in columnsCustoms" v-slot:[custom.customRender]="text, record, index">
      <slot :name="custom.customRender" :text="text" :record="record" :index="index"></slot>
    </template>
  </a-table>
</template>
<script>
export default {
  data() {
    return {
      tableList: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showTotal: (total, range) => `共${total}条记录`,
        showSizeChanger: true,
        showQuickJumper: true
      }
    }
  },
  props: {
    fetchData: {
      type: Function,
      required: true
    },
    columns: {
      type: Array,
      required: true
    },
    rowKey: {
      type: String,
      default: 'id'
    }
  },
  computed: {
    columnsCustoms() {
      return this.columns.filter(record => record.scopedSlots).map(item => item.scopedSlots)
    }
  },
  mounted() {
    this.getTableList(this.pagination)
  },
  methods: {
    getTableList(pagination) {
      const { data } = this.fetchData(pagination)
      if (data.code === 200) {
        const { records, total, current, pageSize } = data.data
        this.tableList = records
        this.pagination.total = total
        this.pagination.current = current
        this.pagination.pageSize = pageSize
      }
    },
    onTableChange(pagination) {
      this.getTableList(pagination)
    }
  }
}
</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。