欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)本地購物車功能

 更新時間:2018年12月05日 11:34:03   作者:Eclipextro  
這篇文章主要為大家詳細介紹了Vue實現(xiàn)本地購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Vue實現(xiàn)本地購物車功能的具體代碼,供大家參考,具體內容如下

功能分析 : v-for顯示商品名字,價格,數(shù)量和對商品進行操作,全選的功能
結構仍然分成 : index.html , index.js , style.css

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>購物車實例</title>
 <link rel="stylesheet" href="style.css" >
</head>
<body>
 <div id="app" v-cloak>
 <template v-if="list.length">
 <table>
 <thead>
 <tr>
 <th><input type="checkbox" @click='checkAll' :checked='allCheck'></th>
 <th>序號</th>
 <th>商品名稱</th>
 <th>商品單價</th> 
 <th>購買數(shù)量</th> 
 <th>操作</th> 
 </tr>
 </thead>
 <tbody>
 <tr v-for="(item,index) in list">
 <td><input type="checkbox" :checked='item.isChecked' @click="singleCheck(index)"></td>
 <td>{{index + 1}}</td>
 <td>{{item.name}}</td>
 <td>{{item.price}}</td>
 <td>
 <button @click="handleReduce(index)" :disable="item.count === 1 ">-</button>
 {{item.count}}
 <button @click="handleAdd(index)">+</button>
 </td>
 <td>
 <button @click="handleRemove">移除</button>
 </td>
 </tr>
 </tbody>
 </table>
 <div>總價 : ¥{{totalPrice}}</div>
 </template>
 <div v-else>購物車為空</div>

 </div>
</body>
<!-- 通過cdn引入-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="index.js"></script>
</html>

index.js

const app = new Vue({
 el : '#app',
 data : {
 allCheck:false,
 list : [
 {
 id: 1 ,
 name :'iPhone 8 ',
 price: 6188 ,
 count: 1 ,
 isChecked : false
 },
 {
 id: 2 ,
 name :'小米 8 ',
 price: 5888 ,
 count: 1 ,
 isChecked : false
 },
 {
 id: 3 ,
 name :'iPad Pro ',
 price: 11000 ,
 count: 1 ,
 isChecked : false
 },
 {
 id: 4 ,
 name :'雷神SE9',
 price: 6188 ,
 count: 10 ,
 isChecked : false
 },
 ]
 },
 computed : {
 //通過計算屬性獲取總價格
 totalPrice:function() {
 let total = 0;
 const newArr = this.list.filter(value => {
 return value.isChecked == true
 })
 for(var i = 0 ;i<newArr.length;i++) {
 total += this.list[i].price * this.list[i].count
 }
 //返回一個符合千分位格式的金額數(shù)
 //return total.toString().replace(/\B(?=(\d{3})+$)/g,',')
 return total
 }
 },
 methods : {
 //減法
 handleReduce:function(index) {
 if(this.list[index].count === 1) return ;
 this.list[index].count--;
 },
 //加法
 handleAdd:function(index) {
 this.list[index].count++
 },
 //移除
 handleRemove:function(index) {
 this.list.splice(index,1)
 },
 //全選
 checkAll() {
 this.allCheck = !this.allCheck
 this.list.forEach(value => {
 value.isChecked = this.allCheck
 })
 },
 //單選,當全部選中時,改變全選按鈕的狀態(tài)
 singleCheck(index) {
 this.list[index].isChecked = !this.list[index].isChecked
 const selectData = this.list.filter(value => {
 return value.isChecked == true
 })
 this.allCheck = selectData.length === this.list.length ? true : false
 }
 }
})

style.css

[v-cloak] {
 display: none;
}
table {
 border: 1px solid #e9e9e9;
 border-collapse: collapse;
 border-spacing: 0;
 empty-cells: show;
}
th,td {
 padding: 8px 16px;
 border: 1px solid #e9e9e9;
 text-align: left;
}
th {
 background: yellowgreen;
 color: #5c6b77;
 font-weight: 600;
 white-space: nowrap;
}

效果圖如下

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Vue?列表過濾與排序的實現(xiàn)

    Vue?列表過濾與排序的實現(xiàn)

    這篇文章主要介紹了Vue?列表過濾與排序的實現(xiàn),文章圍繞主題展開詳細的內容介紹,具有一定的參考價值需要的小伙伴可以參考一下
    2022-05-05
  • Vue修改mint-ui默認樣式的方法

    Vue修改mint-ui默認樣式的方法

    下面小編就為大家分享一篇Vue修改mint-ui默認樣式的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue 解決父組件跳轉子路由后當前導航active樣式消失問題

    Vue 解決父組件跳轉子路由后當前導航active樣式消失問題

    這篇文章主要介紹了Vue 解決父組件跳轉子路由后當前導航active樣式消失問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue中用H5實現(xiàn)文件上傳的方法實例代碼

    vue中用H5實現(xiàn)文件上傳的方法實例代碼

    本篇文章主要介紹了vue中用H5實現(xiàn)文件上傳的方法實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Vue之解決Echarts組件使用ID不能復用的問題

    Vue之解決Echarts組件使用ID不能復用的問題

    這篇文章主要介紹了Vue之解決Echarts組件使用ID不能復用的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 解決vue build打包之后首頁白屏的問題

    解決vue build打包之后首頁白屏的問題

    下面小編就為大家分享一篇解決vue build打包之后首頁白屏的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue操作數(shù)組的幾種常用方法小結

    Vue操作數(shù)組的幾種常用方法小結

    本文主要介紹了Vue操作數(shù)組的幾種常用方法小結,主要包括map、filter、forEach、find 和 findIndex 、some 和 every、includes、Array.from這幾種方法,感興趣的可以了解一下
    2023-09-09
  • 使用vue.js編寫藍色拼圖小游戲

    使用vue.js編寫藍色拼圖小游戲

    之前在網上看到《藍色拼圖》這款小游戲,作者是用jquery寫的。下面通過本文給大家分享基于vue.js編寫藍色拼圖小游戲,一起看看實現(xiàn)代碼吧
    2017-03-03
  • Vue自定義指令學習及應用詳解

    Vue自定義指令學習及應用詳解

    這篇文章主要為大家詳細介紹了Vue中自定義指令的學習以及如何利用Vue制作一個簡單的學生信息管理系統(tǒng),感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05
  • Vue 路由間跳轉和新開窗口的方式(query、params)

    Vue 路由間跳轉和新開窗口的方式(query、params)

    這篇文章主要介紹了Vue 路由間跳轉和新開窗口的方式,本文主要通過query方式和params方式介紹,需要的朋友可以參考下
    2019-12-12

最新評論