您好,登录后才能下订单哦!
怎么在vue中实现多次循环操作?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。
需求:有以下一个数组,想将其对象里的信息展示出来,而且还要把对象下面的数组的具体信息也展示出来。v-for可以实现循环,但是能否再下一级进行渲染呢?答案是可以的。
list: [{ "scheme_id": "1", "scheme_sn": "2018031416442200001", "scheme_name": "测试支付宝", "type": "", "field_id": "0", "user_id": "5", "mother_id": "0", "content": "测试支付宝", "program_id": "1,2", "status": "1", "range": "1", "obj": "5", "add_time": "2018-03-14 16:44:22", "is_handsel": "2", "atten_del": "", "up_time": null, "obj_phone": "13521121232", "programs": [{ "program_id": "1", "reserve": "99701", "program_name": "测试支付宝1", "price": "0.0100", "sale_num": "200" }, { "program_id": "2", "reserve": "4895", "program_name": "阿萨打算", "price": "0.0200", "sale_num": null }] }, { "scheme_id": "2", "scheme_sn": "2018031416512800002", "scheme_name": "阿散发撒", "type": "", "field_id": "0", "user_id": "5", "mother_id": "0", "content": "阿斯达", "program_id": "2,3", "status": "2", "range": "1", "obj": "5", "add_time": "2018-03-14 16:51:28", "is_handsel": "2", "atten_del": "", "up_time": null, "obj_phone": "13521121232", "programs": [{ "program_id": "2", "reserve": "4895", "program_name": "阿萨打算", "price": "0.0200", "sale_num": null }, { "program_id": "3", "reserve": "10", "program_name": "测试多个程序方案赠送1", "price": "0.0000", "sale_num": null }] }]
HTML部分:
<div id="app"> <div v-for ="item in list"> <p>{{item.scheme_sn}}</p> <p v-for="items in item.programs">程序id {{items.program_id}}</p> <p v-for="items in item.programs">程序名称 {{items.program_name}}</p> </div> </div>
先循环大的list 然后再用items in item.programs
去循环对象里面的数组
完整测试示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.jb51.net vue多次循环</title> <style> *{ margin: 0; padding: 0; list-style: none; } </style> </head> <body> <div id="app"> <div v-for ="item in list"> <p>{{item.scheme_sn}}</p> <p v-for="items in item.programs">程序id {{items.program_id}}</p> <p v-for="items in item.programs">程序名称 {{items.program_name}}</p> </div> </div> </body> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script> const app=new Vue({ el:"#app", data:{ list: [{ "scheme_id": "1", "scheme_sn": "2018031416442200001", "scheme_name": "测试支付宝", "type": "", "field_id": "0", "user_id": "5", "mother_id": "0", "content": "测试支付宝", "program_id": "1,2", "status": "1", "range": "1", "obj": "5", "add_time": "2018-03-14 16:44:22", "is_handsel": "2", "atten_del": "", "up_time": null, "obj_phone": "13521121232", "programs": [{ "program_id": "1", "reserve": "99701", "program_name": "测试支付宝1", "price": "0.0100", "sale_num": "200" }, { "program_id": "2", "reserve": "4895", "program_name": "阿萨打算", "price": "0.0200", "sale_num": null }] }, { "scheme_id": "2", "scheme_sn": "2018031416512800002", "scheme_name": "阿散发撒", "type": "", "field_id": "0", "user_id": "5", "mother_id": "0", "content": "阿斯达", "program_id": "2,3", "status": "2", "range": "1", "obj": "5", "add_time": "2018-03-14 16:51:28", "is_handsel": "2", "atten_del": "", "up_time": null, "obj_phone": "13521121232", "programs": [{ "program_id": "2", "reserve": "4895", "program_name": "阿萨打算", "price": "0.0200", "sale_num": null }, { "program_id": "3", "reserve": "10", "program_name": "测试多个程序方案赠送1", "price": "0.0000", "sale_num": null }] }] } }) </script> </html>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试上述代码,可得如下运行效果:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。