vue购物车

<html>
<head>
<title>购物车</title>
<link rel="stylesheet" href="./css/购物车.css" type="text/css" />
</head>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

<body>
    <div id="test">
        <table id="mytable">
            <tr>
                <th><input type="checkBox"  @click="selectProduct()" v-bind:checked="isSelectAll" />全选</th>
                <th>书名</th>
                <th>数量</th>
                <th>单价</th>
                <th>总价</th>
                <th>操作</th>
            </tr>

            <tr v-for="(item,index) in productList">
                <td><input type="checkBox" v-bind:checked="item.isSelect" @click="item.isSelect=!item.isSelect"/></td>
                <td>{{item.proName}}</td>
                <td>
                    <span><a @click="item.proNum--">-</a></span>
                    {{item.proNum}}
                    <span><a @click="item.proNum++">+</a></span>
                </td>
                <td>{{item.proPrice}}</td>
                <td>{{item.proPrice*item.proNum}}</td>
                <td><a @click="deletePro(index)">删除</a></td>
            </tr>
        </table>

        <div class="checkPro">
            <div class="left">
                <span><a  @click="deleteProduct">删除所选产品</a></span>
            </div>
            <div class="right">
                <span>{{getTotal.totalNum}}本图书总计{{getTotal.totalPrice}}元</span>
            </div>
        </div>
    </div>
</body>


<script >
    new Vue({
        el : "#test",
        data : {
            productList:[
                {
                    'proName' :'操作系统',
                    'proNum' : 2,
                    'proPrice' :30,
                },
                {
                    'proName' :'组成原理',
                    'proNum' : 5,
                    'proPrice' :25,
                },
                {
                    'proName' :'编译原理',
                    'proNum' : 1,
                    'proPrice' :30,
                }
            ],
        },

        methods:{
            deletePro:function(index){
                this.productList.splice(index,1);
            },

            deleteProduct:function(){
                this.productList=this.productList.filter(function (item) {return !item.isSelect})
            },

            selectProduct:function(){
                for (var i=0;i<this.productList.length; i++) {
                    this.productList[i].isSelect=true;
                }
            },
        },

        mounted:function () {
            var _this=this;
            this.productList.map(function (item) {
                _this.$set(item, 'isSelect', true);
            })
        },

        computed:{
            getTotal:function(){
                var prolist=this.productList.filter(function (val) { return val.isSelect}),
                totalPri=0;
                for (var i=0,len=prolist.length; i<len; i++) {
                    totalPri+=prolist[i].proPrice*prolist[i].proNum;
                }
                return {totalNum:prolist.length,totalPrice:totalPri}
            },

            isSelectAll:function(){
                return this.productList.every(function (val) { return val.isSelect});
            },
        },

    
    })
</script>

</html>

table{width: 1200px;}
table th{width: 100px}
table td{width: 200px;text-align: center;}
a{text-decoration:none;color: black}
span{font-size: 20px;margin: 10px 10px}
strong{border: 1px solid;}
.checkPro{
padding-right:450px;
padding-left:50px;
}
.left{
float: left;

}
.right{
float: right;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,845评论 1 92
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 1,928评论 0 1
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,731评论 0 8
  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 3,216评论 0 11
  • 日子如流水般一天天的逝去,过去的日子赋予我什么样的生命思考,今晚就谈谈自己对生命的一点看法吧 永远记得96年第一次...
    小苇阅读 353评论 4 4