在做vue项目的时候难免会用到循环,可是但我们后台数据返回多条记录而且是多层嵌套关系的时候,我们需要获取当前第几次循环此时就会出现问题。
下面给大家介绍两种方式,第一种是基于数学公式:第一次循环*(第二次循环总长度)+1+第二次循环 可以获取当前第几次循环
第二种方法:是在方法中进行计算返回当前下标。废话不多说先看一下效果吧
具体代码如下:
测试数据json字符串:
parentList: [{ childList: [{ index: 1, childName: "第一个节点" }, { index: 2, childName: "第一个节点" }, { index: 3, childName: "第一个节点" }, { index: 4, childName: "第一个节点" }, { index: 5, childName: "第一个节点" }] }, { childList: [{ index: 6, childName: "第二个节点" }, { index: 7, childName: "第二个节点" }, { index: 8, childName: "第二个节点" }, { index: 9, childName: "第二个节点" }, { index: 10, childName: "第一个节点" }] }, { childList: [{ index: 11, childName: "第二个节点" }, { index: 12, childName: "第二个节点" }, { index: 13, childName: "第一个节点" }, { index: 14, childName: "第一个节点" }, { index: 15, childName: "第一个节点" }] }]
页面HTML 具体代码:
<template> <div class="hello"> <h1>获取多层循环的总行数</h1> <table border="1" width="50%" align="center"> <tr> <td>父循环第几次</td> <td>子循环第几次</td> <td>第一种办法</td> <td>第二种办法</td> <td>json字符串中的行数</td> <td>数值</td> </tr> <tbody v-for="parent,index in parentList" :key="index"> <tr v-for="child,cindex in parent.childList" :key="child.index"> <td>{{index}}</td> <td>{{cindex}}</td> <td olor="red"> <font size="3" color="red">{{index*(parent.childList.length)+1+cindex}}</font></td> <td><font size="3" color="red">{{getIndex()}}</font></td> <td>{{child.index}}</td> <td>{{child.childName}}</td> </tr> </tbody> </table> </div> </template>
第二种获取下标的方法:
methods:{ getIndex(){ if (!this.index){ this.index=1 }else{ this.index++ } return this.index } }
这样我们就轻松的获取到当前循环第几行啦。
以上这篇基于vue v-for 多层循环嵌套获取行数的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 群星.1991-不一样的感觉REMIX【宝丽金】【WAV+CUE】
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】