<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