• 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
  • 相关阅读:
    基于多种群机制的PSO算法(优化与探索三 *混合种群思想优化多种群与广义PSO求解JSP)
    设备启动顺序导致OpenWrt找不到网卡错误
    数据类型【MySQL】
    牛客网刷题-(5)
    Web前端:2022年最佳Web开发框架比较—你需要了解的一切
    JS之函数
    Java核心——面向对象编程(下)接口
    IDEA调试总结
    企业网络安全面临哪些困境?可以怎样应对?
    LeetCode #83. 删除排序链表中的重复元素
  • 原文地址:https://blog.csdn.net/z291493823/article/details/134203196