Vue+Ant Design of Vue二次封装Table+Pagination 组件

前言  

在Vue的Ant-Design-Of-Vue-Table组件中,你可以对ant-design-of-vue-Table-pagination组件进行二次封装,这样可以减少页面代码重复,使页面代码更简洁、更容易操作。      

1.新建一个table.js文件(Table+Pagination+查询按钮+重置按钮二次封装)   

import { Table } from 'ant-design-vue'
import Vue from 'vue'
const componentName = 'my-table-option'
export default {
  name: 'myTable',
  props: Object.assign({}, Table.props, {
    // 返回 Promise<{ currPage, totalCount, list: any[] }> 的获取数据的函数,用于内部管理数据加载
    data: { type: Function },
    // 查询条件
    dataParameters: null,
    // 是否开启:单击行则选中行
    selectOnClick: { type: Boolean, default: true },
    // 默认翻到第 1 页
    pageNum: { type: Number, default: 1 },
    // 默认分页大小 10 行
    pageSize: { type: Number, default: 10 },
    // 是否显示分页大小切换下拉框
    showSizeChanger: { type: Boolean, default: true },
    // 是否显示分页器
    showPagination: { type: [String, Boolean], default: 'auto' },
    pageURI: { type: Boolean, default: false },
    bordered: { type: Boolean, default: true }
  }),
  data () {
    return {
      localLoading: false,
      localDataSource: [],
      localPagination: Object.assign({}, this.pagination),
      localScroll: {},

      // 表格列显示隐藏
      filterValue: [],
      filterShow: false,
      fullDataSource: null, //合计
    }
  },
  computed: {
    // 判断是否在弹出框里,用于定义表格是否可以滚动
    isInDialog () {
      let parent = this.$parent
      while (parent) {
        if (parent.$options._componentTag === 'a-modal') {
          return true
        }
        parent = parent.$parent
      }
      return false
    },

    localKeys () {
      return [...Object.keys(this.$data), ...Object.keys(this._computedWatchers), ...Object.keys(this).filter(k => k.startsWith('local'))]
    },

    // 处理最大显示长度后的列
    allColumns () {
      let results = []
      // 设置 scroll 属性后,需要设置所有列的 width 来避免表头和内容的错位。
      // 对未设置 width 的 col 设置平均宽度
      const fullWidth = this.localScroll.x || (this.$el || {}).offsetWidth - 85
      const remainWidth = this.columns.reduce(
        (remain, { width }) => width
          ? typeof width === 'string' && width.endsWith('%')
            ? remain - parseFloat(width) * fullWidth / 100
            : remain - parseFloat(width)
          : remain,
        fullWidth
      )
      const noWidthColCount = this.columns.reduce((count, { width }) => width ? count : count + 1, 0)
      const averageWidth = remainWidth / noWidthColCount
      this.columns.forEach(col => !col.width && Vue.set(col, 'width', averageWidth))

      results = this.columns.reduce((results, col) => {
        // 超出后显示省略号
        if (this.isDrag || col.key !== 'action') {
          // 组件不支持排序省略一起使用,使用排序后,表头省略会失效
          col.ellipsis = true
        }
        const result = Object.assign({}, col)
        results.push(result)
        return results
      }, results)

      return results
    },
    localColumns () {
      return this.allColumns.filter(col => this.filterValue.includes(col.dataIndex || col.key || col.title))
    },
    // 不支持表格中有子表格,需要屏蔽
    localComponents () {
      const headerComponent = {}
      if (!this.isDrag) return headerComponent
      headerComponent.header = {}
      headerComponent.header.cell = (h, props, chi
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值