vue實(shí)現(xiàn)購(gòu)物車列表
本文實(shí)例為大家分享了vue實(shí)現(xiàn)購(gòu)物車列表的具體代碼,供大家參考,具體內(nèi)容如下
功能:
- 刪除
- 單選 全選
- 增加數(shù)量 減少數(shù)量
- 計(jì)算總價(jià) 計(jì)算數(shù)量
- 搜索
代碼:
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
篩選:<input type="text" v-model="key">
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th>
<input type="checkbox" v-model="all" @change="checkAll()" >
</th>
<th>id</th>
<th>書籍名稱</th>
<th>出版日期</th>
<th>購(gòu)買價(jià)格</th>
<th>數(shù)量</th>
<th>操作</th>
</tr>
<tr v-for="(item,index) in flist" :key="item.id">
<td style="text-align: center;"><input type="checkbox" v-model="item.sel" ></td>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.time}}</td>
<td>{{item.price|prices}}</td>
<td><button @click="item.num--" :disabled="item.num==1">-</button>{{item.num}}<button @click="item.num++">+</button></td>
<td><button @click="delItem(item.id)">移除</button></td>
</tr>
<tr><td colspan="7">總價(jià)格:{{total.price|prices}} 選擇數(shù)量:{{total.num}}</td></tr>
</table>
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
key:"",
all:true,
list:[
{id:1,name:"小紅書",time:"2018-8",price:188.99,num:1,sel:true},
{id:2,name:"小爛熟",time:"2019-8",price:88.9,num:1,sel:true},
{id:3,name:"小綠樹",time:"2017-5",price:133.00,num:1,sel:true},
{id:4,name:"發(fā)生的樹",time:"2020-1",price:68.80,num:1,sel:true},
{id:5,name:"奧古",time:"2015-4",price:555.50,num:1,sel:true },
]
},
methods:{
delItem(item){
var falg=window.confirm("確定要?jiǎng)h除嗎?");
if(falg){
this.list.splice(item-1,1)
}
},
checkAll(){
this.list.forEach(item=>item.sel=this.all)
}
},
watch:{
list:{
handler:function(){
this.all=this.list.every(item=>item.sel)
},
deep:true
}
},
computed:{
total:function(){
var price=0;
var num=0;
this.list.forEach(item=>{
if(item.sel){
price+=item.num*item.price
num+=item.num*1
}
})
return ({price,num})
},
flist:function(){
if(this.key===''){return this.list}
return this.list.filter(item=>item.name.includes(this.key))
}
},
filters:{
prices:function(val,fix=2){
val=val.toFixed(fix)
val=""+val
return "¥"+val
}
},
})
</script>
</body>
</html>

關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+vant實(shí)現(xiàn)購(gòu)物車全選和反選功能
- Vue實(shí)現(xiàn)購(gòu)物車基本功能
- Vuex實(shí)現(xiàn)購(gòu)物車小功能
- vuex實(shí)現(xiàn)購(gòu)物車的增加減少移除
- vuex實(shí)現(xiàn)購(gòu)物車功能
- vuejs手把手教你寫一個(gè)完整的購(gòu)物車實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
相關(guān)文章
Vue響應(yīng)式原理與虛擬DOM實(shí)現(xiàn)步驟詳細(xì)講解
在Vue中最重要、最核心的概念之一就是響應(yīng)式系統(tǒng)。這個(gè)系統(tǒng)使得Vue能夠自動(dòng)追蹤數(shù)據(jù)變化,并在數(shù)據(jù)發(fā)生變化時(shí)自動(dòng)更新相關(guān)的DOM元素。本文將會(huì)探討Vue響應(yīng)式系統(tǒng)的實(shí)現(xiàn)原理及其底層實(shí)現(xiàn)2023-03-03
Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問(wèn)題
這篇文章主要介紹了Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue3中的shallowRef?和shallowReactive對(duì)比分析
這篇文章主要介紹了Vue3中的shallowRef?和shallowReactive,通過(guò)示例代碼逐一對(duì)他們的使用做的詳細(xì)介紹,文末補(bǔ)充介紹了vue3的shallowRef()、shallowReactive()和shallowReadonly()的使用,需要的朋友可以參考下2023-01-01
vue中設(shè)置echarts寬度自適應(yīng)的代碼步驟
這篇文章主要介紹了vue中設(shè)置echarts寬度自適應(yīng)的問(wèn)題及解決方案,常常需要做到echarts圖表的自適應(yīng),一般是根據(jù)頁(yè)面的寬度做對(duì)應(yīng)的適應(yīng),本文記錄一下設(shè)置echarts圖表的自適應(yīng)的步驟,需要的朋友可以參考下2022-09-09
vue使用rules實(shí)現(xiàn)表單字段驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue使用rules實(shí)現(xiàn)表單字段驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

