網(wǎng)站首頁 Vue 正文
vue實現(xiàn)購物車商品單選、全選及商品數(shù)量和總價計算,供大家參考,具體內(nèi)容如下
效果展示
HTML
<template> ? <div class="buyCar"> ? ? <header-bar ? ? ? title="購物車商品" ? ? ? :show-line="true" ? ? /> ? ? <div class="content"> ? ? ? <table> ? ? ? ? <thead> ? ? ? ? ? <tr> ? ? ? ? ? ? <th>商品總數(shù): {{ total }}</th> ? ? ? ? ? ? <th>商品總價: {{ totalPrice }}</th> ? ? ? ? ? ? <th> ? ? ? ? ? ? ? <input ? ? ? ? ? ? ? ? v-model="AllChecked" ? ? ? ? ? ? ? ? type="checkbox" ? ? ? ? ? ? ? ? @click="checkAll" ? ? ? ? ? ? ? > ? ? ? ? ? ? ? 全選 ? ? ? ? ? ? </th> ? ? ? ? ? </tr> ? ? ? ? </thead> ? ? ? ? <tbody> ? ? ? ? ? <tr ? ? ? ? ? ? v-for="(item, index) in list" ? ? ? ? ? ? :key="index" ? ? ? ? ? > ? ? ? ? ? ? <td>{{ item.name }}</td> ? ? ? ? ? ? <td>單價: {{ item.price }}</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? <input ? ? ? ? ? ? ? ? v-model="item.isChecked" ? ? ? ? ? ? ? ? type="checkbox" ? ? ? ? ? ? ? ? @click="check(item)" ? ? ? ? ? ? ? > ? ? ? ? ? ? </td> ? ? ? ? ? </tr> ? ? ? ? </tbody> ? ? ? </table> ? ? </div> ? </div> </template>
JS
<script> import HeaderBar from '../components/header/index.vue'; export default { ? name: 'BuyCar', ? components: { ? ? HeaderBar, ? }, ? data() { ? ? return { ? ? ? list: [ ? ? ? ? { ? ? ? ? ? name: '商品1', ? ? ? ? ? price: 1111, ? ? ? ? ? isChecked: false, ? ? ? ? }, ? ? ? ? { ? ? ? ? ? name: '商品2', ? ? ? ? ? price: 2222, ? ? ? ? ? isChecked: false, ? ? ? ? }, ? ? ? ? { ? ? ? ? ? name: '商品3', ? ? ? ? ? price: 3333, ? ? ? ? ? isChecked: false, ? ? ? ? }, ? ? ? ? { ? ? ? ? ? name: '商品4', ? ? ? ? ? price: 4444, ? ? ? ? ? isChecked: false, ? ? ? ? }, ? ? ? ? { ? ? ? ? ? name: '商品5', ? ? ? ? ? price: 5555, ? ? ? ? ? isChecked: false, ? ? ? ? }, ? ? ? ], ? ? ? total: 0, ? ? ? // 是否已全選 ? ? ? AllChecked: false, ? ? }; ? }, ? computed: { ? ? totalPrice() { ? ? ? let prices = 0; ? ? ? this.list.forEach(item => { ? ? ? ? if (item.isChecked) { ? ? ? ? ? prices += item.price; ? ? ? ? } ? ? ? }); ? ? ? return prices; ? ? }, ? }, ? methods: { ? ? // 單選 ? ? check(item) { ? ? ? if (!item.isChecked) { ? ? ? ? this.total++; ? ? ? } else { ? ? ? ? this.total--; ? ? ? } ? ? ? this.AllChecked = this.total === this.list.length; ? ? }, ? ? // 全選和反選 ? ? checkAll() { ? ? ? const AllChecked = this.AllChecked; ? ? ? this.list.forEach(item => { ? ? ? ? item.isChecked = !AllChecked; ? ? ? }); ? ? ? this.AllChecked = !this.AllChecked; ? ? ? this.total = this.AllChecked ? this.list.length : 0; ? ? }, ? }, }; </script>
CSS
<style lang="less" scoped> .buyCar { ? height: 100%; ? display: flex; ? flex-direction: column; ? .content { ? ? height: calc(100vh); ? ? padding: 45px 15px 15px; ? ? table { ? ? ? text-align: center; ? ? ? border-collapse: collapse; ? ? ? border-spacing: 0; ? ? } ? ? td, ? ? th { ? ? ? width: 200px; ? ? ? height: 20px; ? ? ? border: 1px solid #000; ? ? } ? ? input { ? ? ? width: 20px; ? ? ? height: 20px; ? ? } ? } } </style>
原文鏈接:https://blog.csdn.net/qq_42760405/article/details/84934181
相關推薦
- 2023-08-28 React綁定antd輸入框,點擊清空或者確定按鈕實現(xiàn)清空輸入框內(nèi)容
- 2022-07-04 如何用python實現(xiàn)結(jié)構(gòu)體數(shù)組_python
- 2022-09-14 R語言ggplot2圖例標簽、標題、順序修改和刪除操作實例_R語言
- 2022-05-31 如何使用yolov5輸出檢測到的目標坐標信息_python
- 2022-10-15 C++中?Sort函數(shù)詳細解析_C 語言
- 2022-02-11 idea package合在一起,利用Compact Middle Packages解決 &
- 2023-05-24 golang?recover函數(shù)使用中的一些坑解析_Golang
- 2022-07-30 一文掌握Python正則表達式_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支