国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Vue.js框架實(shí)現購物車(chē)功能

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:涉水的小菜嘰 欄目: JavaScript 歡迎投稿:712375056

本文實(shí)例為大家分享了Vue.js框架實(shí)現購物車(chē)的具體代碼,供大家參考,具體內容如下

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">

    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="../lib/vue.min.js"></script>
    </head>

    <body>

        <div id="app" style="position: relative;left: 30%">
            <table cellpadding="10">
                <thead>
                    <th><input type="checkbox" v-model="cb" v-on:click="allSelect">全選</th>
                    <th>名稱(chēng)</th>
                    <th>單價(jià)</th>
                    <th>數量</th>
                    <th>金額</th>
                    <th>操作</th>
                </thead>

                <tbody>
                    <tr v-for="x in info">
                        <td><input type="checkbox" v-model="x.bol" v-on:click="sign()"></td>
                        <td>{{x.name}}</td>
                        <td>{{x.price}}</td>
                        <td><input type="number" v-model="x.num" min="0" style="width: 50px;text-align: center" v-on:click="count(x)" v-on:change="count(x)"></td>
                        <td>{{x.total}}</td>
                        <td><button v-on:click="del(x)">刪除</button></td>
                    </tr>
                </tbody>
            </table>

            <Br>
            <p>總金額:{{all}}</p>
        </div>

        <script>
            var vm = new Vue({
                el: "#app",
                data: {
                    all: 0,
                    cb: false,
                    info: [{
                        bol: false,
                        name: "iphone7",
                        price: 6000,
                        num: 1,
                        total: 6000
                    }, {
                        bol: false,
                        name: "榮耀6x",
                        price: 1200,
                        num: 1,
                        total: 1200
                    }, {
                        bol: false,
                        name: "dell筆記本",
                        price: 4000,
                        num: 1,
                        total: 4000
                    }]
                },
                methods: {
                    //計算單價(jià)
                    count: function(obj) {
                        for(var i = 0; i < this.info.length; i++) {
                            //
                            if(this.info[i] == obj) {
                                this.info[i].total = obj.price * obj.num;
                            }
                            //如果被選中了,就計算總價(jià)格
                            if(obj.bol) {
                                this.allSelect();
                            }
                        }
                    },
                    //刪除
                    del: function(obj) {
                        this.info.splice(this.info.indexOf(obj), 1)
                        this.allCount();
                    },

                    //單選
                    sign: function() {
                        this.allCount();
                    },
                    //全選
                    allSelect: function() {

                        for(var i = 0; i < this.info.length; i++) {
                            this.info[i].bol = this.cb;
                        }
                        this.allCount();
                    },
                    //計算總價(jià)
                    allCount: function() {
                        //每次計算總價(jià)都要清空
                        this.all = 0;
                        for(var i = 0; i < this.info.length; i++) {
                            //計算被選中的商品
                            if(this.info[i].bol) {
                                this.all += this.info[i].total;
                            }
                        }
                    }

                }
            })
        </script>
    </body>

</html>

效果圖:

更多文章可以點(diǎn)擊《》學(xué)習閱讀。

關(guān)于vue.js組件的教程,請大家點(diǎn)擊專(zhuān)題進(jìn)行學(xué)習。

更多vue學(xué)習教程請閱讀專(zhuān)題

以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

夜夜爽夜夜叫夜夜高潮漏水| 天天曰天天躁天天摸孕妇| 俺也来俺也去俺也射| 国产SUV精二区| 激情综合一区二区迷情校园| 曰本女人牲交全视频免费播放|