亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue實(shí)現(xiàn)購(gòu)物車結(jié)算功能

 更新時(shí)間:2021年09月07日 10:52:09   作者:周小姐  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車結(jié)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用vue做的購(gòu)物車結(jié)算的功能,供大家參考,具體內(nèi)容如下

代碼:

<!-- 占位 -->
<template>
 <div>
 <div class="product_table">
 <div class="product_info">商品信息</div>
 <div class="product_info">商品金額</div>
 <div class="product_info">商品數(shù)量</div>
 <div class="product_info">總金額</div>
 <div class="product_info">編輯</div>
 </div>
 <div class="product_table" v-for="(item,index) in getProductList" :key="index">
 <div style="width:20px;height:20px;border-radius:10px;border:1px solid black;" @click="checkSingle(item)" :class="{checked:item.makeChoose}"></div>
 <div class="product_info">{{item.productName}}</div>
 <div class="product_info">{{item.productPrice}}</div>
 <span @click="changeNumber(item,1)">+</span>
 <input type="text" v-model="item.prductQty" style="width: 30px;">
 <span @click="changeNumber(item,-1)">-</span>
 <div class="product_info">{{item.productPrice*item.prductQty}}</div>
 <div class="product_info" @click="deleteProduct(index)">刪除</div>
 </div>
 <div style="width:20px;height:20px;border-radius:10px;border:1px solid black;margin-top:10px" @click="checkAll()" :class="{checked:checkAllItem}"></div>
 <div>總價(jià)格:{{totalPrice}}</div>
 </div>
</template>
<script>
 import Vue from 'vue'
 export default {
 name: 'side-bar-placeholder',
 data () {
 return {
 getProductList:[
 {
 productName:'西瓜',
 productPrice:100,
 prductQty:3
 },
 {
 productName:'南瓜',
 productPrice:50,
 prductQty:2
 },
 {
 productName:'蘋果',
 productPrice:300,
 prductQty:3
 },
 ],
 totalPrice:0, //總金額
 checkAllItem:false, //全部選中
 checkedList:[] //選中的數(shù)
 }
 },
 methods:{
 //刪除某一項(xiàng)
 deleteProduct:function (index) {
 this.getProductList.splice(index,1) 
 this.calcTotalPrice() //這里要注意,當(dāng)某一項(xiàng)刪除時(shí),如果你選中了,這里也是要做計(jì)算總價(jià)格的
 },
 //修改數(shù)量
 changeNumber:function (number,add) {
 if(add<0){
 number.prductQty--;
 if(number.prductQty<'1'){ //因?yàn)閿?shù)量最低是1
 number.prductQty=1
 }
 }else{
 number.prductQty++;
 } 
 this.calcTotalPrice() 
 },
 //選中單個(gè)的
 checkSingle:function (item){
 if(typeof item.makeChoose=='undefined'){ //這里要注意,因?yàn)閏hecked字段根本不在this.getProductList里面,所以你要自己賦值進(jìn)去
 Vue.set(item, 'makeChoose',true) //這里應(yīng)該設(shè)為true
 }else{
 item.makeChoose=!item.makeChoose
 } 
 this.calcTotalPrice() 
 },
 //選中所有的
 checkAll:function (){ 
 this.checkAllItem=!this.checkAllItem
 var _this=this
 if(this.checkAllItem){
 this.getProductList.forEach(element => {
 if(typeof element.makeChoose=='undefined'){
  Vue.set(element, 'makeChoose',_this.checkAllItem) //讓每一小項(xiàng)跟隨checkall來(lái)變化
 }else{
 element.makeChoose=_this.checkAllItem
 }
 });
 }else{
 this.getProductList.forEach(element => {
 if(typeof element.makeChoose=='undefined'){
  Vue.set(element, 'makeChoose',_this.checkAllItem)
 }else{
 element.makeChoose=_this.checkAllItem
 }
 });
 } 
 this.calcTotalPrice() 
 },
 //計(jì)算總金額
 calcTotalPrice:function () {
 var _this=this
 this.totalPrice=0
 this.getProductList.forEach((element,index) => {
 if(element.makeChoose){
 _this.totalPrice+=element.productPrice*element.prductQty //這里是一個(gè)累加的過(guò)程,所以要用+=
 }
 }); 
 },
 //讓頁(yè)面一進(jìn)來(lái)就處于選中的狀態(tài)
 makeAllChecked:function () {
 this.getProductList.forEach((item)=>{
 if(typeof item.makeChoose=='undefined'){
 Vue.set(item, 'makeChoose',true)
 }
 }) 
 }
 } ,
 watch:{
 //如果全部選中,那么全部選中的按鈕應(yīng)該變綠,如果一項(xiàng)不是,應(yīng)該變空
 getProductList:{
 handler:function (item) {
 this.checkedList=this.getProductList.filter((element)=>{
 return element.makeChoose==true;
 }) 
 //選中數(shù)<總數(shù)據(jù)
 if(this.checkedList.length<this.getProductList.length){
 this.checkAllItem=false
 }else{
 this.checkAllItem=true
 } 
 },
 deep:true //這個(gè)deep:true一定要寫,不然肯定不會(huì)時(shí)時(shí)變化的
 }
 } ,
 created:function (){
 this.makeAllChecked()
 }
 }
</script>
<style lang="less" scoped>
.product_table{
 display: flex;
 width: 100%;
}
.product_info{
 flex:1; 
}
.checked{
 background-color:green;
}
</style>

這個(gè)代碼實(shí)現(xiàn)了什么?

1.在點(diǎn)擊加減時(shí)每個(gè)產(chǎn)品的總價(jià)變化,所有產(chǎn)品的總價(jià)變化
2.選中時(shí)才會(huì)結(jié)算
3.如果全部選中了每個(gè)子項(xiàng),全部選中按鈕會(huì)變綠,如果有一項(xiàng)不選中,那么會(huì)變白
4.一般的購(gòu)物車,我希望他一進(jìn)來(lái)就是checked的狀態(tài),提高購(gòu)買性
5.當(dāng)我刪除某一項(xiàng)時(shí),如果這一項(xiàng)是已經(jīng)checked了的,也要讓他在計(jì)算總價(jià)時(shí)重新計(jì)算.

ps:最后一行的按鈕是全部選中哦,或者是全部取消,忘記寫了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實(shí)現(xiàn)輸入一位數(shù)字轉(zhuǎn)漢字功能

    vue實(shí)現(xiàn)輸入一位數(shù)字轉(zhuǎn)漢字功能

    這篇文章主要介紹了vue實(shí)現(xiàn)輸入一位數(shù)字轉(zhuǎn)漢字功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 關(guān)于nuxt?store中保存localstorage的問(wèn)題

    關(guān)于nuxt?store中保存localstorage的問(wèn)題

    這篇文章主要介紹了關(guān)于nuxt?store中保存localstorage的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • element vue Array數(shù)組和Map對(duì)象的添加與刪除操作

    element vue Array數(shù)組和Map對(duì)象的添加與刪除操作

    這篇文章主要介紹了element vue Array數(shù)組和Map對(duì)象的添加與刪除功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • vue項(xiàng)目history模式刷新404問(wèn)題解決辦法

    vue項(xiàng)目history模式刷新404問(wèn)題解決辦法

    這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目history模式刷新404問(wèn)題的解決辦法,需要的朋友可以參考下
    2023-11-11
  • 解決vite項(xiàng)目Uncaught Syntaxerror:Unexpected token>vue項(xiàng)目上線白屏問(wèn)題

    解決vite項(xiàng)目Uncaught Syntaxerror:Unexpected token>vue項(xiàng)

    這篇文章主要介紹了解決vite項(xiàng)目Uncaught Syntaxerror:Unexpected token>vue項(xiàng)目上線白屏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽

    vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽

    今天小編就為大家分享一篇vue 實(shí)現(xiàn)移動(dòng)端鍵盤搜索事件監(jiān)聽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue清空數(shù)組的幾個(gè)方式(小結(jié))

    vue清空數(shù)組的幾個(gè)方式(小結(jié))

    本文主要介紹了vue清空數(shù)組的幾個(gè)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Vue?3需要避免的錯(cuò)誤

    Vue?3需要避免的錯(cuò)誤

    Vue3已經(jīng)穩(wěn)定了相當(dāng)長(zhǎng)一段時(shí)間了。許多代碼庫(kù)都在生產(chǎn)環(huán)境中使用它,其他人最終都將不得不遷移到Vue3。我現(xiàn)在有機(jī)會(huì)使用它并記錄了我的錯(cuò)誤,下面這些錯(cuò)誤你可能想要避免
    2023-03-03
  • vue中?$forceUpdate的使用解析

    vue中?$forceUpdate的使用解析

    這篇文章主要介紹了vue中?$forceUpdate的使用解析,該方案是比較好的一種方式,比如說(shuō)我們嘗試直接給某個(gè)??object??增加一個(gè)屬性,發(fā)現(xiàn)頁(yè)面上沒有效果;直接將length變成0來(lái)清空數(shù)組,下文詳細(xì)資料需要的小伙伴可以參考一下
    2022-04-04
  • 解決vuex改變了state的值,但是頁(yè)面沒有更新的問(wèn)題

    解決vuex改變了state的值,但是頁(yè)面沒有更新的問(wèn)題

    這篇文章主要介紹了解決vuex改變了state的值,但是頁(yè)面沒有更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11

最新評(píng)論