Vue中多层数组嵌套,修改内部数组对象值的方法

在这里插入图片描述

查看本专栏目录


关于作者

还是大剑师兰特:曾是美国某知名大学计算机专业研究生,现为航空航海领域高级前端工程师;CSDN知名博主,GIS领域优质创作者,深耕openlayers、leaflet、mapbox、cesium,canvas,webgl,echarts等技术开发,欢迎加底部微信,一起交流。

热门推荐内容链接
1openlayers 从基础到精通,300+代码示例
2leaflet 热门分解学习教程,150+图文示例
3cesium 从0到1学习指南,200+代码示例
4 mapboxGL 从入门到实战,150+图文示例
5canvas 示例应用100+,揭密底层细节
6javascript从基础到高级,示例展示200+
7vue2 实战指南,100+个细节深度剖析

在这里插入图片描述

工程需求

在项目中遇到一个问题,就是数组套数组,然后在内部数组中补充一个属性值onview,原始时候设定为true或者false,通过v-for的形式,展现数据。 这里面的核心要点是,能够通过点击切换来修改onview的值,从而显示不同的状态,请参考效果图。

效果展示图

在这里插入图片描述

示例源代码

/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2022-06-12
*/

<template>
	<div class="box">
		<div class="one1" v-for="(item,pindex) in myArray" :key="pindex">
			<div class="oneTop1">
				<div class="cc1" >
					<span>名称:{{item.name}}</span>
				</div>
		    </div>
			<div v-for="(subitem,cindex) in item.options" :key='cindex'  class="two1" >
				<div class="cc1" >
					<div  class="fl1" >名字:{{subitem.cname}}</div>
					<el-button class="fr1"  type="danger" size='small' @click="changeTrue(pindex,cindex)" v-if="subitem.onview ">关闭</el-button>
					<el-button class="fr1"  type="success" size='small' @click="changeFalse(pindex,cindex)" v-else>显示</el-button>
				</div>
			</div>
		</div>
		
	</div>
</template>

<script>
	export default {
		data() {
			return {
				arr : [{
						name: "第一组数据",
						options: [{
								cname: '大剑师兰特'
							},
							{
								cname: '还是大剑师兰'
							}
						]
					},
					{
						name: "第二组数据",
						options: [{
								cname: 'gis-dajianshi'
							},
							{
								cname: '还是大剑师兰'
							}
						]
					},
				],
				myArray:[],
				
			}
		},

		methods: {
             changeArr(){
				 let temp=this.arr
				 for( let j=0;j<temp.length;j++){
					 temp[j].options=temp[j].options.map((item)=>{
							 return {
								 ...item,
								 onview:false,
							 }
					 })
				 }
				 this.myArray=temp;
			 },
			 
			changeTrue(pindex,cindex){	
				this.$set(this.myArray[pindex].options[cindex],'onview',false)
			},
			changeFalse(pindex,cindex){
				this.$set(this.myArray[pindex].options[cindex],'onview',true)
			},

		},
		mounted() {
            this.changeArr()
		}
	}
</script>
<style scoped>
 .box{ margin:30px auto; width:600px;}
 .one1{ background-color: aliceblue;margin-bottom: 30px;}
 .oneTop1{ background-color: aquamarine;}
 .two1{ background-color:#eee;}
 .cc1{ line-height: 50px; height:50px; padding:5px 10px }
 .fr1{float: right; cursor: pointer;}
 .fl1{float: left; cursor: pointer;}
</style>

核心代码

changeTrue(pindex,cindex){
this.$set(this.myArray[pindex].options[cindex],‘onview’,false)
},

changeFalse(pindex,cindex){
this.$set(this.myArray[pindex].options[cindex],‘onview’,true)
},

专栏目标

在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。

提供vue2的一些基本操作:安装、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,updated, beforeDestroy,destroyed,activated,deactivated,errorCaptured,components,)、 $root , $parent , $children , $slots , $refs , props, $emit , eventbus ,provide / inject, Vue.observable, $listeners, $attrs, $nextTick , v-for, v-if, v-else,v-else-if,v-on,v-pre,v-cloak,v-once,v-model, v-html, v-text, keep-alive,slot-scope, filters, v-bind,.stop, .native, directives,mixin,render,国际化,Vue Router等

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是大剑师兰特

打赏一杯可口可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值