请教:Vue如何删除选中的数组元素(有多个选中的情况下)?

数据是模拟的,当点击编辑的时候,我想删除选中的元素,就是如果选中一个的情况下,就删除一个,选中多个的情况下,就删除多个,这样
每个数组里的元素,默认有个 checked 属性

html结构

    <div 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">
            <div>全部商品</div>
            <div>
                <a href="javascript:;" @click="editorShopping">{{ showtext ? '编辑' : '完成' }}</a>
            </div>
        </section>

        <!-- 购物车商品列表 -->
        <section class="shopping-list">
         <template v-if="shoppingList.length > 0">
            <div v-for="(shopping,index) in shoppingList" class="item" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
                <div class="shopping-checkbox">
                    <input :id="shopping.id" type="checkbox" name="" class="" v-model="shopping.checked">
                    <label :for="shopping.id"></label>
                </div>
                <div class="shopping-box" flex f-d="r" f-w="n" j-c="start" a-i="center">
                    <div><img :src="shopping.imgsrc" alt=""></div>
                    <div class="info">
                        <h3>{{ shopping.title }}</h3>
                        <div class="acount" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
                            <span class="money">¥{{ shopping.money }}</span>
                            <div 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>
                                <div 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>
                                 </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
         </template>

        </section>


        <section class="download-order-footer" flex f-d="r" f-w="n" j-c="s-b" a-i="center">
            <div class="shopping-checkall">
                <input id="checkall" type="checkbox" class="" name="">
                <label for="checkall">
                    <i>全选</i>
                </label>
            </div>
            <div class="left">
                总计:<b>¥684</b>
            </div>
            <div 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>
            </div>
        </section>

    </div>

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 --;
                }
            }
        }
    });
阅读 9.1k
3 个回答

JS中删除批量删除数组元素时应该倒序删除(意思是先删除索引大的元素,再删除索引小的元素),
因为在删除的过程中数组的索引会变化,如果先删除了小的元素,后面元素的索引都会变化.
我写了个简单的demo:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="app">
            <p>
                <button @click="removeAll">删除全部</button>
            </p>
            <ul v-for="value of arr">
                <li>
                    {{value}}
                </li>
            </ul>
        </div>
    </body>
    <script src="./vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        new Vue({
            el:'#app',
            data:{
                arr:['a','b','c']
            },
            methods:{
                removeAll:function() {
                    var length = this.arr.length;
                    for(let i =length ;i>=0 ;i--){
                        this.arr.splice(i,1);
                    }
                }
            }
        })
    </script>
</html>

已经解决了!
参考地址
用逆向循环


            removeShopping : function(){
                 var that = this;
                 for (var i = that.shoppingList.length - 1;i >= 0;i--) {
                     var index = that.shoppingList[i];
                     if (index.checked) {
                         that.shoppingList.splice(i,1);
                     }
                 }
            }

换个思路,别删除,过滤更简单直观。

// 直接过滤更简单,直观
var arr = [{a:1,c:true},{a:2,c:false},{a:3,c:true}]
arr = arr.filter(function(i){ return !i.c })
console.log(arr) // {a:2,c:false}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏