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

Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能

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

開(kāi)始更新前端框架Vue.JS的相關(guān)博客。

功能概述

學(xué)習(xí)了Vue.JS的一些基礎(chǔ)知識(shí),現(xiàn)在利用指令、數(shù)據(jù)綁定這些基礎(chǔ)知識(shí)開(kāi)發(fā)一個(gè)簡(jiǎn)單的購(gòu)物車(chē)功能。功能要點(diǎn)如下:
(1)展示商品的名稱(chēng)、單價(jià)和數(shù)量;
(2)商品的數(shù)量可以增加和減少;
(3)購(gòu)物車(chē)的總價(jià)要實(shí)時(shí)更新,即數(shù)量發(fā)生變動(dòng),總價(jià)也要相應(yīng)的改變;
(4)商品可以從購(gòu)物車(chē)中移除;
(5)具有選擇功能,只計(jì)算選中的商品的總價(jià)。

上一張截圖,如下:

代碼

代碼分成三部分,分別是HTML、JS、CSS。關(guān)鍵的是HTML和JS。

HTML

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>Vue 購(gòu)物車(chē)</title>
 <script src="../js/vue.min.js"></script>
 <link href="../css/cart.css" rel="stylesheet">
 </head>
 <body>
 <div id="app" v-cloak>
  <template v-if="list.length">
  <table>
   <thead>
   <tr>
    <th><input type="checkbox" v-on:click="swapCheck" v-model="checked"></th>
    <th>商品名稱(chēng)</th>
    <th>商品單價(jià)</th>
    <th>商品數(shù)量</th>
    <th>操作</th>
   </tr>   
   </thead>
   <tbody>
   <tr v-for="(item,index) in list">
    <td><input type="checkbox" v-model="selectList" :id="item.id" :value="index" name="selectList" ></td>
    <td>{{ item.name }}</td>
    <td>{{ item.price }}</td>
    <td>
    <button @click="handleReduce(index)" :disabled="item.count === 1">-</button>
    {{ item.count }}
    <button @click="handleAdd(index)">+</button>
    </td>
    <td><button @click="handleRemove(index)">移除</button></td>
   </tr>
   </tbody>
  </table>
  <div>總價(jià):¥ {{ totalPrice }}</div>
  </template>
  <div v-else>購(gòu)物車(chē)為空!</div>
 </div>

 <script src="../js/cart.js"></script>
 </body>
</html>

JS

var app = new Vue({
 el:'#app',
 data:{
 list:[
  {
  id:1,
  name:'iPhone 8',
  price:8888,
  count:1
  },
  {
  id:2,
  name:'Huwei Mate10',
  price:6666,
  count:1
  },
  {
  id:3,
  name:'Lenovo',
  price:6588,
  count:1
  }
 ],
 selectList:[],
 checked:false
 },
 computed:{
 totalPrice:function(){
  var total = 0;
  for(var i = 0,len = this.selectList.length;i < len;i++){
  var index = this.selectList[i];
  var item = this.list[index];
  if(item){
   total += item.price * item.count;
  }
  else{
   continue;
  }

  }
  return total.toString().replace(/\B(?=(\d{3})+$)/g,',');
 }
 },
 methods:{
 handleReduce:function(index){
  var item = this.list[index];
  if(item.count < 2){
  return;
  }
  item.count--;
 },
 handleAdd:function(index){
  var item = this.list[index];
  item.count++;
 },
 handleRemove:function(index){
  this.list.splice(index,1);
 },
 swapCheck:function(){

  var selectList = document.getElementsByName('selectList');
  var len = selectList.length;
  if(this.checked){
  for(var i = 0;i < len;i++){
   var item = selectList[i];
   item.checked = false;
  }
  this.checked = false;
  this.selectList = [];
  }
  else{
  for(i = 0;i < len;i++){
   item = selectList[i];
   if(item.checked === false){
   item.checked = true;
   this.selectList.push(selectList[i].value);
   }
  }
  this.checked = true;

  }
 }
 }
});

CSS

[v-cloak]{
 display: none;
}

table{
 border: 1px solid black;
 border-collapse: collapse;
 border-spacing: 0;
 empty-cells: show;
}

th,td{
 padding: 8px 16px;
 border:1px solid black;
 text-align: center;
}

th{
 background-color: gray;
}

關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

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

相關(guān)文章

  • vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解

    vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解

    這篇文章主要介紹了vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue的子父組件傳值之小白必看篇

    Vue的子父組件傳值之小白必看篇

    這篇文章主要介紹了Vue的子父組件傳值之小白必看篇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 淺析vue.js數(shù)組的變異方法

    淺析vue.js數(shù)組的變異方法

    本篇文章給大家分享了vue.js數(shù)組的變異方法的相關(guān)內(nèi)容,有興趣的朋友跟著學(xué)習(xí)參考下。
    2018-06-06
  • Vue.set() this.$set()引發(fā)的視圖更新思考及注意事項(xiàng)

    Vue.set() this.$set()引發(fā)的視圖更新思考及注意事項(xiàng)

    this.$set()和Vue.set()本質(zhì)方法一樣,前者可以用在methods中使用。這篇文章主要介紹了Vue.set() this.$set()引發(fā)的視圖更新思考及注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • 9種方法優(yōu)化jQuery代碼詳解

    9種方法優(yōu)化jQuery代碼詳解

    本文將詳細(xì)介紹jQuery代碼優(yōu)化的9種方法,需要的朋友可以參考下
    2020-02-02
  • vue3圖片剪裁插件vue-img-cutter使用小結(jié)

    vue3圖片剪裁插件vue-img-cutter使用小結(jié)

    Vue.js是一款流行的JavaScript前端框架,很受用戶(hù)喜愛(ài),這篇文章主要介紹了vue3圖片剪裁插件vue-img-cutter使用小結(jié),本文結(jié)合示例代碼講解的非常詳細(xì),感興趣的朋友一起看看吧
    2024-01-01
  • 如何測(cè)量vue應(yīng)用運(yùn)行時(shí)的性能

    如何測(cè)量vue應(yīng)用運(yùn)行時(shí)的性能

    這篇文章主要介紹了如何測(cè)量vue應(yīng)用運(yùn)行時(shí)的性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法

    Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法

    這篇文章主要介紹了Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Vue 路由 過(guò)渡動(dòng)效 數(shù)據(jù)獲取方法

    Vue 路由 過(guò)渡動(dòng)效 數(shù)據(jù)獲取方法

    這篇文章主要介紹了Vue 路由 過(guò)渡動(dòng)效 數(shù)據(jù)獲取方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • Vue組件的通信方式詳解

    Vue組件的通信方式詳解

    這篇文章主要介紹的是Vue組件間的通信方式,本文將系統(tǒng)的介紹了幾種不使用Vuex,比較實(shí)用的組件間的通信方式,希望能幫助到大家
    2023-04-04

最新評(píng)論