el-table 列分页

在这里插入图片描述

<template>
  <div>
    <el-table
      :data="tableData"
      :key="tampTime"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄"
        width="180">
      </el-table-column>
      <el-table-column
        prop="gender"
        label="性别">
      </el-table-column>
      <el-table-column
        v-for="(item, index) in showColData"
        :key="index"
        :label="item.colLabel">
        <template slot="header">
          <div class="custom-header-cell">
            <span class="label">{{item.colLabel}}</span>
            <template v-if="colPage.totalPage > 1">
              <div v-if="!index" :class="['prev-btn', {disabled: colPage.current === 1}]" @click="handlePrev">上一页</div>
              <div v-if="index === showColData.length - 1" :class="['next-btn', {disabled: colPage.current === colPage.totalPage}]" @click="handleNext">下一页</div>
            </template>
          </div>
        </template>
        <template slot-scope="scope">
          {{ scope.row.colData[item.index].colValue }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>

export default {
  data () {
    return {
      tableData: [],
      fullColData: [],
      showColData: [],
      colPage: {
        current: 1,
        size: 10,
        totalPage: 0
      },
      tampTime: ''
    }
  },
  created () {
    this.initTableData()
  },
  methods: {
    // 模拟一些数据
    initTableData () {
      const result = []
      for (let i = 0; i < 10; i++) {
        const obj = {
          name: '张三',
          age: 18,
          gender: '男',
          colData: []
        }
        for (let j = 0; j < 40; j++) {
          obj.colData.push({
            colLabel: `${j + 1}`,
            colValue: `${i + 1}_${j + 1}`
          })
        }
        result.push(obj)
      }
      this.tableData = result
      this.initColData()
    },
    // 初始化列
    initColData () {
      const { tableData, colPage } = this
      const { colData: fullColData } = tableData[0]
      // 添加索引,用于取数据
      fullColData.forEach((item, index) => {
        item.index = index
      })
      this.fullColData = fullColData
      let showColData = fullColData
      if (fullColData.length > colPage.size) {
        showColData = fullColData.slice(0, 10)
      }
      this.showColData = showColData

      this.colPage.totalPage = Math.ceil(fullColData.length / colPage.size)
    },
    // 上一页
    handlePrev () {
      let { current } = this.colPage
      if (current > 1) {
        current--
        this.colPage.current = current
        this.changeCellData()
      }
    },
    // 下一页
    handleNext () {
      let { current, totalPage } = this.colPage
      if (current < totalPage) {
        current++
        this.colPage.current = current
        this.changeCellData()
      }
      this.$forceUpdate()
    },
    // 根据页数改变列数据
    changeCellData () {
      const { colPage, fullColData } = this
      const startSlice = (colPage.current - 1) * colPage.size
      const endSlice = startSlice + colPage.size
      const showColData = fullColData.slice(startSlice, endSlice)
      this.showColData = showColData
      this.tampTime = new Date().valueOf()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table {
  // 默认是 hidden 按钮超出会隐藏
  .el-table__header-wrapper,
  th.el-table__cell,
  .cell {
    overflow: visible;
  }
  .custom-header-cell {
    position: relative;
    .label {
      position: relative;
      z-index: 2;
      color: #000;
    }
    .prev-btn,
    .next-btn {
      width: 20px;
      padding: 6px 0;
      position: absolute;
      z-index: 9;
      top: -12px;
      background-color: #3f3fbb;
      color: #fff;
      text-align: center;
      font-weight: 400;
      line-height: 18px;
      transition: all .4s;
      &:hover {
        cursor: pointer;
        opacity: .8;
      }
      &.disabled {
        cursor: not-allowed;
        background-color: #a6a7e2;
      }
    }
    .prev-btn {
      left: -30px;
    }
    .next-btn {
      right: -10px;
    }
  }
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZionHH

落魄前端,在线炒粉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值