vue选项卡

描述:
后台返回的数据采用v-for渲染到<li>中,点击对应一级标签显示对应二级内容。
数据格式:

test: [
  { id: 1, 
	name: '1', 
	child: [
		{ id: 11, name: '11' }, 
		{ id: 12, name: '12' }, 
		{ id: 13, name: '13' }, 
		{ id: 14, name: '14' }
	] 
  }, 
  { id: 2, 
    name: '2', 
	child: [
		{ id: 21, name: '21' }, 
		{ id: 22, name: '22' }, 
		{ id: 23, name: '23' }, 
		{ id: 24, name: '24' }
	] 
  }
]
				

思路
遍历父级,给父级绑定点击事件,点击将id存入data;
判断data中所存id是否为父级列表id,是的话显示子级。

代码

<!doctype html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>test</title>
		<script type="text/javascript" src="vue.js"></script>
		<!-- :class="activeId==fartherItem.id ? 'activeStyle': 'commonStyle' " -->
		<style type="text/css">
			.activeStyle {
				background: #baf
			}
			.commonStyle {
				background: #bfa;
				borderBottom: 1px solid #eee;
			}
		</style>
	</head>
	<body>
		<div id="vue_app" style="line-height:  50px;">
			<ul style="float: left;width: 100px;position: absolute;">
				<li 
					v-for="fartherItem in test" 
					@click="toggle(fartherItem.id)"
					:class="activeId==fartherItem.id ? 'activeStyle' : ''">
					{{fartherItem.name}}
					<ul style="position: absolute; width: 100px; top: 0; right: -120px" v-show="activeId==fartherItem.id ">
						<li v-for="item in fartherItem.child">
							{{item.name}}
						</li>
					</ul>
				</li>
			</ul>
		</div>
	<script>
		app_all=new Vue({
			el: "#vue_app",
			data: {
				activeId: '11111',
				test: [
				{ id: 1, 
				  name: '1', 
				  child: [{ id: 11, name: '11' }, { id: 12, name: '12' }, { id: 13, name: '13' }, { id: 14, name: '14' }] 
				}, 
				{ id: 2, 
				  name: '2', 
				  child: [{ id: 21, name: '21' }, { id: 22, name: '22' }, { id: 23, name: '23' }, { id: 24, name: '24' }] 
				}
				]
			},
			methods: {
			    toggle(id) {
			      this.activeId = id
			      console.log(this.activeId)
			    }
			 }
		})
	</script>
	</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值