2020-11-19購(gòu)物車(chē)案例--過(guò)濾器,更改購(gòu)買(mǎi)數(shù)量,移除按鈕,最終價(jià)

<!DOCTYPE?html>

<html?lang="zh-CN">

<head>

??<meta?charset="UTF-8">

??<meta?name="viewport"?content="width=device-width,?initial-scale=1.0">

??<meta?http-equiv="X-UA-Compatible"?content="ie=edge">

??<title>Document</title>

??<script?src="https://cdn.staticfile.org/vue/2.6.12/vue.min.js"></script>

??<style>

????table,tr,th,td{

??????border:?darkgrey?solid?2px;

??????border-collapse:?collapse;

????}

??</style>

</head>

<body>

??<div?id="app">

????<div?v-if='books.length'>

??????<table>

????????<thead>

??????????<tr>

????????????<th></th>

????????????<th>書(shū)籍名</th>

????????????<th>出版日期</th>

????????????<th>價(jià)格</th>

????????????<th>購(gòu)買(mǎi)數(shù)量</th>

????????????<th>操作</th>

??????????</tr>

????????</thead>?


????????<tbody>

??????????<tr?v-for='(item,index)?in?books'>

????????????<td>{{item.id}}</td>

????????????<td>{{item.name}}</td>

????????????<td>{{item.itime}}</td>

????????????<!--?<td>{{getFinalPrice(item.price)}}</td>?-->

????????????<td>{{item.price?|?showPrice}}</td>

????????????<td>

??????????????<!--?<button?@click='down(item.nub--)'>-</button>?-->

??????????????<button?@click='decrement(index)'?v-bind:disabled='item.nub?<=1'>-</button>


??????????????{{item.nub}}

??????????????<!--?<button?@click='up(item.nub++)'>+</button>?-->

??????????????<button?@click='increment(index)'>+</button>


????????????</td>

????????????<td>

??????????????<button?@click='remove(index)'>移除</button>

????????????</td>

??????????</tr>


????????</tbody>


???????</table>

???????<span>總價(jià):{{totalPrice?|?showPrice}}</span>

????</div>

????<h2?v-else>購(gòu)物車(chē)為空</h2>

??</div>


??<script>

????const?vm=new?Vue({

??????el:'#app',

??????data:{

????????books:?[

??????????{?id:?1,?name:?"1",?itime:?new?Date().toLocaleDateString(),?price:?"44"?,nub:?45?},

??????????{?id:?2,?name:?"2",?itime:?new?Date().toLocaleDateString(),?price:?"45"?,nub:?56?},

??????????{?id:?3,?name:?"3",?itime:?new?Date().toLocaleDateString(),?price:?"46"?,nub:?66?},

??????????{?id:?4,?name:?"4",?itime:?new?Date().toLocaleDateString(),?price:?"47"?,nub:?88?}

????????]

??????},?

??????methods:{

????????//?down?()?{},

????????//?up?()?{},

????????//?getFinalPrice(price)?{

????????//???return?'¥'+?Number(price).toFixed(2)

????????//?}

??????????increment(index)?{

????????????this.books[index].nub?=?this.books[index].nub?+?1

??????????},

??????????decrement(index)?{

????????????this.books[index].nub--

??????????},

??????????remove(index)?{

????????????this.books.splice(index,1)

??????????}

??????},

??????computed:?{

????????totalPrice()?{

??????????let?totalPrice?=?0;

??????????for(let?i?=?0;?i?<?this.books.length;?i++)?{

????????????totalPrice+=?this.books[i].price*this.books[i].nub

??????????}

??????????return?totalPrice;

????????}

??????},

??????filters:?{

????????showPrice(price)?{

??????????return?'¥'?+Number(price).toFixed(2)

????????}

??????}

????});

??</script>

</body>

</html>

B站 UP:ilovecoding 視屏課案例
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容