購(gòu)物車加減效果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class='container'>
<table class='table table-bordered text-center'>
<thead>
<tr>
<th>編號(hào)</th>
<th>品名</th>
<th>單價(jià)</th>
<th>數(shù)量</th>
<th>小計(jì)</th>
</tr>
</thead>
<tbody>
<tr v-for="(value,index) in list">
<td>{{index+1}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>
<button @click='add(index)'>+</button>
<span>{{value.count}}</span>
<button @click='redu(index)'>-</button>
</td>
<td>{{value.sub}}</td>
</tr>
<tr>
<td colspan="5">總價(jià):¥{{sum}}</td>
</tr>
</tbody>
</table>
</div>
<script src="../../js/vue.js"></script>
<script type="text/javascript">
new Vue({
el:'.container',
data:{
list:[
{pname:'apple',price:3,count:2,sub:6},
{pname:'pear',price:4,count:3,sub:12},
{pname:'banana',price:5,count:4,sub:20}
],
sum:0
},
methods:{
add:function(ind){
//數(shù)量
this.list[ind].count++;
//改變小計(jì)
this.list[ind].sub=this.list[ind].count*this.list[ind].price;
this.total();
},
redu:function(ind){
//數(shù)量
if(this.list[ind].count>1){
this.list[ind].count--;
}
//小計(jì)
this.list[ind].sub=this.list[ind].count*this.list[ind].price;
this.total();
},
total:function(){
for(var i=0,tota=0;i<this.list.length;i++){
tota+=this.list[i].sub
}
this.sum=tota
}
}
})
</script>
</body>
</html>
選項(xiàng)卡效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='itany'>
<ul>
<li v-for='(value,index) in card' @click='chg(index)'>{{value.title}}</li>
</ul>
<ul>
<li v-for='(value,index) in card' v-show='value.flag'>{{value.content}}</li>
</ul>
</div>
<script src="../../js/vue.js"></script>
<script>
new Vue({
el:"#itany",
data:{
card:[
{title:'選項(xiàng)卡1',content:'11111111111111111',flag:true},
{title:'選項(xiàng)卡2',content:'22222222222222222',flag:false},
{title:'選項(xiàng)卡3',content:'33333333333333333',flag:false}
]
},
methods:{
chg:function(ind){
for(var i=0;i<this.card.length;i++){
this.card[i].flag=false;
}
this.card[ind].flag=true;
}
}
})
</script>
</body>
</html>
添加用戶效果:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="js/vue.js"></script>
<div id="box">
<p>姓名</p>
<input type="text" v-model="arrs.name"/>
<p>年齡</p>
<input type="text" v-model="arrs.name1"/>
<p>郵箱</p>
<input type="text" v-model="arrs.name2"/>
<div>
<button v-on:click="add">添加</button>
<button v-on:click="app(index)">刪除</button>
</div>
<table border="1" cellspacing="0" cellpadding="" style="width: 500px;">
<tr>
<th>姓名</th>
<th>年齡</th>
<th>郵箱</th>
</tr>
<tr v-for="(value,index) in arr">
<td>{{value.name}}</td>
<td>{{value.name1}}</td>
<td>{{value.name2}}</td>
</tr>
</table>
</div>
<script>
new Vue({
el:'#box',
data:{
arr:[
{name:'張',name1:'18',name2:'1054890634@qq.com'}
],
arrs:{}
},
methods:{
add:function(){
this.arr.push(this.arrs)
},
app:function(ind){
this.arr.splice(ind,1)
}
}
})
</script>
</body>
</html>