【前端】如何优雅的解决el-table分页index问题

el-table在分页时候,运用scope.$index去处理行数据,往往会出现index分页从0重新计数的问题。通常有两种方法:
1、每次运用scope.$index时,根据page和pageSize对index进行转化

<el-table                          
     :data='tableData.slice((page-1)*pageSize,page*pageSize)'
     >
     <el-table-column label='line1' prop='line1' >
		<template slot-scope='scope'>
             <el-button type='text' @click='handleFuc(trueIndex(scope.$index))'>  
             </el-button>
        </template>
     </el-table-column>
</el-table>

private handleFunc(trueIndex: any) {
	console.log(trueIndex);
}

private trueIndex(index: any) {
   return index + (this.page - 1) * this.pageSize;
}

缺点是每次运用scope.$index都需要调用trueIndex获取实际的index

2、运用el-table的row-class-name属性,对row的index进行统一处理,将trueIndex放入row属性中,每次需要用到行的index时候,直接用scope.row.index

<el-table                          
     :data='tableData.slice((page-1)*pageSize,page*pageSize)'
     :row-class-name="tableRowClassName"
     >
     <el-table-column label='line1' prop='line1' >
		<template slot-scope='scope'>
             <el-button type='text' @click='handleFuc(scope.row.index))'>  
             </el-button>
        </template>
     </el-table-column>
</el-table>

// 通过这一条将row的index在分页情况下进行校正
private tableRowClassName(row: any) {
     row.row.index = row.rowIndex + (this.page - 1) * this.pageSize;
 }
 
private handleFunc(trueIndex: any) {
	console.log(trueIndex);
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值