基于vue2、element-ui封装的table组件。简单的render使用(可参考element中Transfer的render-content)

基于element-ui二次封装的表格

// 页面结构
<template>
  <div class="table-ui">
    <el-table ref="TableRef" :data="tableData" :header-cell-style="header_cell_style" :cell-style="cell_style"
      :loading="loading" :height="tableHeight" :row-class-name="rowClassName">
      <template v-for="(column, index) in columns">
        <el-table-column :key="column.dataIndex" :prop="column.dataIndex" :label="column.label" :type="column.type"
          :width="column.width" :fixed="column.fixed" :min-width="column.minWidth" v-if="column.render">
          <template slot-scope="scope">
            <OptionContent :option="column" :row="scope.row" v-if="column.render" />
          </template>
        </el-table-column>
        <el-table-column v-else :key="column.dataIndex + index" :prop="column.dataIndex" :label="column.label"
          :type="column.type" :width="column.width" :index="indexMethod" :formatter="column.format || format"
          :fixed="column.fixed" :min-width="column.minWidth" />
      </template>
    </el-table>
    <div class="bottom-pagination" v-if="total > 20">
      <el-pagination :current-page="page.page" :page-sizes="[20, 40, 80, 100]" :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeChange"
        @current-change="currentChange" />
    </div>
  </div>
</template>

//script逻辑块
<script>
export default {
  name: 'TableUi',
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    fetchPage: Function,
    rowClassName: {
      type: Function,
      default: () => { }
    },
    params: {
      type: Object,
      default: () => { }
    } // 表单搜索条件
  },
  components: {
    OptionContent: {
      props: {
        option: Object,
        row: Object
      },
      render(h) {
        return this.option.render && this.option.render(h, this.row)
      }
    }
  },
  computed: {
    tableHeight() {
      return this.total > 20 ? 'calc(100% - 0.6rem)' : '100%'
    }
  },
  data() {
    return {
      header_cell_style: configs.DIALOG_FORM_STYLE.HEADER_CELL_STYLE,
      cell_style: configs.DIALOG_FORM_STYLE.CELL_STYLE,
      debounceFetchTableData: debounce(this.fetchTableData),
      format,
      tableData: [],
      total: 0,
      loading: false,
      page: {
        pageSize: 20,
        page: 1
      }
    }
  },
  watch: {
    page: {
      handler() {
        this.debounceFetchTableData()
      },
      deep: true
    },
    params: {
      handler() {
        this.page = {
          pageSize: 20,
          page: 1
        }
      },
      deep: true
    }
  },
  mounted() {
    this.debounceFetchTableData()
  },
  methods: {
    fetchTableData(params = { ...this.params, ...this.page }) {
      this.loading = true
      this.fetchPage && this.fetchPage(params).then(res => {
        this.total = res.totalCount
        this.tableData = res.list
        this.loading = false
      })
    },
    currentChange(val) {
      this.page.page = val
    },
    sizeChange(val) {
      this.page.pageSize = val
    },
    indexMethod(idx) {
      const cacheIdx = (this.page.page - 1) * this.page.pageSize
      const curIdx = idx + 1
      return cacheIdx + curIdx
    },
    refresh(isResetFetch = false) {
      if (isResetFetch) {
        this.page = {
          pageSize: 20,
          page: 1
        }
      } else {
        this.page.page = this.tableData.length === 1 && this.page.page !== 1 ? this.page.page - 1 : this.page.page
      }
      isResetFetch ? this.debounceFetchTableData(this.page) : this.debounceFetchTableData()
    }
  }
}
</script>
//简单的样式
<style lang="scss" scoped>
.table-ui {
  box-sizing: border-box;
  background: $main-content-bg-color;
  height: calc(100% - .6rem);
  padding: 0.1rem 0.2rem 0;

  .el-table {
    background: $main-content-bg-color;

    /deep/.action {
      .el-button {
        display: inline-block;
        padding: 0;
        font-size: 0.2rem;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值