資料是模擬的,當點擊編輯的時候,我想刪除選取的元素,就是如果選取一個的情況下,就刪除一個,選取多個的情況下,就刪除多個,這樣
每個陣列裡的元素,預設有個checked
屬性
演示地址
html結構
<p class="t-root" style="margin-bottom: 1.6rem;">
<section class="shopping-cart-title" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
<p>全部商品</p>
<p>
<a href="javascript:;" @click="editorShopping">{{ showtext ? '编辑' : '完成' }}</a>
</p>
</section>
<!-- 购物车商品列表 -->
<section class="shopping-list">
<template v-if="shoppingList.length > 0">
<p v-for="(shopping,index) in shoppingList" class="item" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
<p class="shopping-checkbox">
<input :id="shopping.id" type="checkbox" name="" class="" v-model="shopping.checked">
<label :for="shopping.id"></label>
</p>
<p class="shopping-box" flex f-d="r" f-w="n" j-c="start" a-i="center">
<p><img :src="shopping.imgsrc" alt=""></p>
<p class="info">
<h3>{{ shopping.title }}</h3>
<p class="acount" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
<span class="money">¥{{ shopping.money }}</span>
<p class="count" flex f-d="r" f-w="n" j-c="center" a-i="center">
<span class="shopping-num" v-show="showtext">x{{ shopping.shoppingnum }}</span>
<p flex f-d="r" f-w="n" j-c="center" a-i="center" v-show="!showtext">
<span class="minus" :class="{active : shopping.shoppingnum > 1}" @click="reduce(index)">
<i class="icon iconfont icon-t-icon8"></i>
</span>
<input type="text" readonly disabled class="showNumber" v-model="shopping.shoppingnum">
<span class="add" :class="{active : shopping.shoppingnum < 100}" @click="add(index)">
<i class="icon iconfont icon-t-icon7"></i>
</span>
</p>
</p>
</p>
</p>
</p>
</p>
</template>
</section>
<section class="download-order-footer" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
<p class="shopping-checkall">
<input id="checkall" type="checkbox" class="" name="">
<label for="checkall">
<i>全选</i>
</label>
</p>
<p class="left">
总计:<b>¥684</b>
</p>
<p class="right">
<a href="javascript:;" flex f-d="r" f-w="n" j-c="center" a-i="center" v-show="showtext">去付款</a>
<a @click="removeShopping" href="javascript:;" flex f-d="r" f-w="n" j-c="center" a-i="center" v-show="!showtext">删除</a>
</p>
</section>
</p>
#js程式碼
var vm = new Vue({
el : ".t-root",
data : {
showtext : true,
shoppingList : [
{
id : 11,
title : '贡牌茶叶西湖春茶正宗雨前龙井茶礼盒装礼盒装160g礼盒装',
money : 48,
imgsrc : 'images/download-order-img-11@2x.png',
shoppingnum : 1,
checked : true
},
{
id : 22,
title : '贡牌茶叶西湖春茶正宗雨前龙井茶礼盒装礼盒装160g礼盒装',
money : 98,
imgsrc : 'images/download-order-img-11@2x.png',
shoppingnum : 1,
checked : true
},
{
id : 33,
title : '贡牌茶叶西湖春茶正宗雨前龙井茶礼盒装礼盒装160g礼盒装',
money : 148,
imgsrc : 'images/download-order-img-11@2x.png',
shoppingnum : 1,
checked : true
}
]
},
computed : {
},
methods : {
editorShopping : function(){
this.showtext = !this.showtext;
},
removeShopping : function(){
var that = this;
that.shoppingList.forEach(function(value,index){
//只有为true时才删除
if (value.checked) {
that.shoppingList.splice(index,1);
// console.log(index);
}
});
},
add : function(index){
var shopping = this.shoppingList[index];
if (shopping.shoppingnum == 100) {
return false;
}else {
shopping.shoppingnum ++;
}
},
reduce : function(index){
var shopping = this.shoppingList[index];
if (shopping.shoppingnum == 1){
return false;
}else {
shopping.shoppingnum --;
}
}
}
});
JS中刪除批量刪除數組元素時應該倒序刪除(意思是先刪除索引大的元素,再刪除索引小的元素),
因為在刪除的過程中數組的索引會變化,如果先刪除了小的元素,後面元素的索引都會改變.
我寫了個簡單的demo:
換個思路,別刪除,過濾更簡單直覺。
已經解決了!
參考位址
用逆向循環