el-table前端分页
- 前端分页:后台只需将数据返回,不需要做过多处理,前端一次请求拿到所有数据后做分页处理。但数据量不能太大,因为前端是先一次性加载所有数据,然后在做分页处理。在数据量多的情况下,加载相对应的会变慢。所有在前端做分页时要先考虑一下后期的数据量。
- 后端分页:因为是后端分页,前端在每点击一次分页时,便向后台请求一次数据。其实就是避免前端一次性从数据库获取大量数据
- tableData.slice((currentPage-1)_PageSize,currentPage_PageSize) 这是重点
<template>
<div class="app">
<!-- 将获取到的数据进行计算 -->
<el-table :data="tableData.slice((pageData.pageNum-1)*pageData.pageSize,pageData.pageNum*pageData.pageSize)"> //对数据请求的处理,最为重要的一句话
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
<div class="tabListPage">
<el-pagination
:page-size="pageData.pageSize"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
:current-page="pageData.pageNum"
:total="pageData.total"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
/>
</div>
</div>
</template>
<script>
export default {
data(){
return {
// 总数据
tableData:[],
// 默认显示第几页
pageData:{
pageNum: 1,
pageSize: 10,
total:0
},
}
},
mounted(){
this.getList();
},
methods:{
getList(){
this.tableData = res.data;
this.pageData.total= res.data.length
},
// 分页
// 每页显示的条数
handleSizeChange(pageSize) {
// 改变每页显示的条数
this.pageData.pageSize= pageSize
// 注意:在改变每页显示的条数时,要将页码显示到第一页
this.pageData.pageNum= 1
},
// 显示第几页
handleCurrentChange(currentPage) {
// 改变默认的页数
this.pageData.pageNum = currentPage
},
},
}
</script>
表格跨分页多选
- 看到项目里有小伙伴手动添加代码去处理这个问题,其实根据文档,只需加上row-key和reserve-selection即可
<el-table row-key="id">
<el-table-column type="selection" reserve-selection></el-table-column>
</el-table>
表格内容超出省略
只要加个show-overflow-tooltip就可以了,还自带tooltip效果
<el-table-column label="客户名称" prop="customerName" show-overflow-tooltip>
</el-table-column>
表头筛选
前端筛选
<el-table-column
align="center"
prop="state"
label="状态"
min-width="30"
:filters="[{ text: '未完成', value: 0 },{ text: '已完成', value: 1 }]"
:filter-method="filterState"
filter-placement="bottom-end"
>
<template slot-scope="scope">
<span v-html="getStatus(scope.row.state)" />
</template>
</el-table-column>
<script>
export default {
data(){
return{
}
},
methods:{
//表头状态筛选
filterState(value, row) {
return row.state === value
},
}
}
</script>
- 后端筛选需要在el-table加上filter-change事件
- 后端排序需要在el-table加上sort-change事件
<el-table
border
:data="tableData"
v-loading="listLoading"
@selection-change="handleSelectionChange"
:header-cell-style="{background:'#fafafa',color:'#252525','text-align':'center'}"
@sort-change="sortChange"
@filter-change="filterChange"
></el-table>
<script>
export default {
data(){
return{
queryItem:{}
}
},
methods:{
// column 的 key
filterChange(column){
this.pageData.pageNum = 1;
do...
this.getList();
},
// 监听创建时间排序的方法
sortChange(column) {
switch (column.prop) {
case 'createTime':
//ascending 升序 descending 降序
if (column.order === 'ascending') {
this.queryItem.createTimeDesc = 0;
}
if (column.order === 'descending') {
this.queryItem.createTimeDesc = 1;
}
this.getList();
break;
default:
break
}
},
}
}
</script>
表格数据formatter
<el-table-column
prop="workTypeCode"
label="身份证类型"
:formatter="idCardTypes"
align="center">
</el-table-column>
<el-table-column
prop="type"
label="类型"
:formatter="formatterType"
align="center">
</el-table-column>
<script>
export default {
data(){
return{
idCardTypeList:[]//身份证类型数据列表
}
},
methods:{
//有固定的数组列表
idCardTypes(row, column, cellValue, index) {
if (cellValue) {
for (var i = 0; i < this.idCardTypeList.length; i++) {
if (cellValue == this.idCardTypeList[i].code) {
return this.idCardTypeList[i].name;
}
}
} else {
return "--";
}
//单一值判断
formatterType(row, column, cellValue){
if (row.type == 1 ) {
return'离线'
}else{
return'--'
}
},
},
}
}
</script>
表格数据自定义dom
<el-table-column
align="center"
prop="state"
label="状态"
min-width="30"
>
<template slot-scope="scope">
<span v-html="getStatus(scope.row.state)" />
</template>
</el-table-column>
<script>
export default {
data(){
return{
}
},
methods:{
getStatus(status) {
if (status == 0) {
return `<div style="display: flex;align-items: center;justify-content: center">
<i class="c_" style="background: #EC7F30"></i>
<span>未发送</span>
</div>`
}
if (status == 1) {
return `<div style="display: flex;align-items: center;justify-content: center">
<i class="c_" style="background: #4D89FF"></i>
<span>发送中</span>
</div>`
}
if (status == 2) {
return `<div style="display: flex;align-items: center;justify-content: center">
<i class="c_" style="background: #67C23A"></i>
<span>接收成功</span>
</div>`
}
if (status == 3) {
return `<div style="display: flex;align-items: center;justify-content: center">
<i class="c_" style="background: #EB1A18"></i>
<span>接收失败</span>
</div>`
}
},
}
}
</script>
<style>
.c_ {
width: 7px;
height: 7px;
display: block;
margin-right: 5px;
margin-top: 3px;
border-radius: 50%;
}
</style>
表格单选多选
- 单选
<el-table
ref="singleTable"
:data="tableData"
highlight-current-row
@current-change="handleCurrentChange"
style="width: 100%">
<el-table-column
type="index"
width="50">
</el-table-column>
</el-table>
<script>
export default {
data(){
return{
currentRow:'',//单选数据
}
},
methods:{
handleCurrentChange(val) {
this.currentRow = val;
}
}
}
</script>
- 多选 @selection-change
- let ids = this.selectData.map((item) => item.id).toString();
<el-table
:data="tableData"
v-loading="listLoading"
@selection-change="selsChange"
highlight-current-row
style="width: 100%"
>
<el-table-column
type="index"
width="50">
</el-table-column>
</el-table>
<script>
export default {
data(){
return{
tableData:[],
selectData:[]//多选数据
}
},
methods:{
//选择表格列的时候会把数据放到一个数组里 多选
selsChange: function (val) {
this.selectData = val;
},
}
}
</script>
表尾合计行
- 若表格展示的是各类数字,可以在表尾显示各列的合计。
- 将show-summary设置为true就会在表格尾部展示合计行
- 使用summary-method并传入一个方法,返回一个数组,这个数组中的各项就会显示在合计行的各列中,
<el-table
:data="tableData"
border
show-summary
:summary-method="getSummaries"
style="width: 100%">
</table>
<script>
export default {
data(){
return{
tableData:[],
selectData:[]//多选数据
}
},
methods:{
getSummaries(param) {
const { columns, data } = param;
const sums = [];
columns.forEach((column, index) => {
const values = data.map(item => Number(item[column.property]));
if (!values.every(value => isNaN(value))) {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr);
if (!isNaN(value)) {
return prev + curr;
} else {
return prev;
}
}, 0);
sums[index] = sums[index].toFixed(2);
} else {
sums[index] = ' ';
}
if (index === 0) {
sums[index] = '总价';
return;
}
if (index === 6) {
sums[6] = (sums[5]/sums[2]*100).toFixed(2);
return;
}
if (index === 8) {
sums[8] =( sums[7]/sums[4]*100).toFixed(2);
return;
}
if (index === 10) {
sums[10] =( sums[9]/sums[5]*100).toFixed(2);
return;
}
if (index === 12) {
sums[12] =( sums[11]/sums[7]*100).toFixed(2);
return;
}
});
return sums;
},
}
}
</script>
添加自定义的暂无数据
<el-table :data="tableData">
<template slot="empty">
<div class="empty-table">
<img style="width: 160px;" class="data-pic" src="../../assets/images/noData.png" alt="" />
<div>暂无数据</div>
</div>
</template>
<el-table-column
type="index"
width="60"
align="center"
label="序号"
/>
</el-table>