javascript - vuejs+elementui 購物車價格計算,點擊加減號修改數量總價都不會改變,但是計算執行了
巴扎黑
巴扎黑 2017-07-05 10:58:23
0
2
1177

一個商品下單的功能
查出所有商品,點擊下單,然後把下單的商品加入購物車,購物車是不用保存的
商品可以修改數量,計算總價,現在我method裡面的計算執行了,但是頁面點擊加減號的時候,數量和總價就是不會變,查詢出來的數據列表裡面,沒有數量和總價這兩個字段,這裡是我自己定義的

##
    
count: undefined, totalPrice: 0,
//下单商品列表 getCartData() { this.$axios({ url: this.path + 'product/getByList', method: 'get', params: this.cartListQuery, transformRequest: [function(params) { let ret = '' for(let it in params) { ret += encodeURIComponent(it) + '=' + encodeURIComponent(params[it]) + '&' } return ret }], headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then((res) => { this.cartNum //下单商品的长度 this.cartListData = res.data; this.cartListData.forEach(function(data,index){ data.count = 1; //默认一个商品 data.totalPrice= (data.price * parseInt(data.count)).toFixed(2); //商品总价 }); }) .catch((err) => { console.log(err); }) }, //加减号 changeQuantity(row, type){ if( type > 0 ){ row.count++; }else{ row.count > 1 ? row.count--: row.count = 1; } this.changeCount(row); }, //数量文本框值改变 changeCount (row) { if(null == row.count || row.count == ""){ row.count=1; } row.totalPrice = (row.count * row.price).toFixed(2);//保留两位小数 console.log(row.totalPrice+" = "+ row.count +" * "+ row.price) //增加商品数量也需要重新计算商品总价 },
巴扎黑
巴扎黑

全部回覆 (2)
漂亮男人

第一個問題,你的問題代碼截圖不完整

第二個問題,你的總價的v-model是cartListData的totalPrice,而你的method裡面設定的是row的totalPrice,你確定你的cartListData的totalPrice,你確定你的cartListData

與row是同一個物件?
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!