国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、案例效果

1、將通過數(shù)據(jù)重構(gòu)頁面

  • 查詢數(shù)據(jù), 渲染頁面
    【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

2、全選

  1. 選中全選按鈕后, 根據(jù)全選按鈕的選中狀態(tài), 修改所有商品的選中狀態(tài)
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

3、清空購物車

  1. 清空商品數(shù)據(jù)
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

4、結(jié)算

  1. 找到所有選中的商品
  2. 計(jì)算所有選中商品各自的總價(jià)
  3. 計(jì)算所有選中商品的總價(jià)之和

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

5、刪除已選中

  1. 在原數(shù)組中, 找到選中的商品, 然后刪除
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

6、商品數(shù)量調(diào)整

  1. 找到對(duì)應(yīng)的商品, 修改收藏?cái)?shù)量
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

7、選中商品

  1. 找到對(duì)應(yīng)的商品, 修改選中狀態(tài)
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

8、刪除某一項(xiàng)

  1. 找到對(duì)應(yīng)商品, 將刪除
  2. 重新渲染視圖

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)文章來源地址http://www.zghlxwxcb.cn/news/detail-479236.html

9、數(shù)據(jù)持久化 (瀏覽器關(guān)閉, 數(shù)據(jù)能保存)

  • 本地存儲(chǔ)
    【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)

二、案例分析

1. 數(shù)組數(shù)據(jù)分析

  1. id: 數(shù)據(jù)的唯一值
  2. status: true代表該商品被選中, false則為沒被選中
  3. pic: 圖片地址
  4. name: 商品名
  5. price: 價(jià)格
  6. number: 商品收藏?cái)?shù)量
  7. total: 庫存

2. 數(shù)據(jù)驅(qū)動(dòng)視圖

  • 查: 查詢數(shù)據(jù), 渲染到頁面
  • 增刪改: 找到源數(shù)據(jù), 然后對(duì)源數(shù)據(jù)做修改, 修改完成, 重新渲染頁面

3. 邏輯思維

  1. 準(zhǔn)備一個(gè)渲染函數(shù)
  2. 首次打開頁面時(shí) 調(diào)用
  3. 在各種事件觸發(fā)之后, 重新調(diào)用

三、html代碼

		<div class="header">頁面頂部</div>
		<!-- 動(dòng)態(tài)生成數(shù)據(jù) -->
		<div class="content"></div>
		<div class="footer">頁面底部</div>
		<script src="./index.js"></script>

四、css代碼

		* {
		    margin: 0;
		    padding: 0;
		}
		
		ul,ol,li {
		    list-style: none;
		}
		
		.header,.footer {
		    width: 1200px;
		    height: 100px;
		    background-color: skyblue;
		    color: #fff;
		    font-size: 50px;
		    display: flex;
		    justify-content: center;
		    align-items: center;
		    margin: 0 auto;
		}
		
		.footer {
		    height: 400px;
		}
		
		.content {
		    width: 1200px;
		    margin: 0 auto;
		    padding: 10px 0;
		}
		
		.content > .top,.content > .bottom {
		    height: 50px;
		    background-color: pink;
		    display: flex;
		    align-items: center;
		}
		
		.content > .bottom {
		    justify-content: space-between;
		    box-sizing: border-box;
		    padding: 0 10px;
		}
		
		.content > .bottom > .totalPrice > span {
		    font-size: 20px;
		    color: red;
		}
		
		.content > .bottom > .btns > button {
		    font-size: 18px;
		    padding: 5px 10px;
		    cursor: pointer;
		}
		
		.content > .top > input {
		    width: 30px;
		    height: 30px;
		    margin: 0 15px 0 50px;
		}
		
		.content > ul {
		    padding-top: 10px;
		}
		
		.content > ul > li {
		    width: 100%;
		    border: 1px solid #333;
		    box-sizing: border-box;
		    height: 100px;
		    margin-bottom: 10px;
		
		    display: flex;
		}
		
		.content > ul > li > div {
		    display: flex;
		    justify-content: center;
		    align-items: center;
		    border-right: 1px solid #333;
		}
		
		.content > ul > li > div:last-child {
		    border: none;
		}
		
		.content > ul > li > .show,
		.content > ul > li > .status {
		    width: 100px;
		}
		
		.content > ul > li > .status > input {
		    width: 30px;
		    height: 30px;
		}
		
		.content > ul > li > .show > img {
		    width: 100%;
		    height: 100%;
		    display: block;
		}
		
		.content > ul > li > .price,
		.content > ul > li > .sub {
		    width: 200px;
		    color: red;
		    font-size: 20px;
		}
		
		.content > ul > li > .title {
		    width: 300px;
		    align-items: flex-start;
		    justify-content: flex-start;
		    box-sizing: border-box;
		    padding: 5px;
		}
		
		.content > ul > li > .number {
		    width: 230px;
		}
		
		.content > ul > li > .number > input {
		    width: 50px;
		    height: 30px;
		    text-align: center;
		    margin: 0 5px;
		    border: none;
		    outline: none;
		    font-size: 18px;
		}
		
		.content > ul > li > .number > button {
		    width: 30px;
		    height: 30px;
		    cursor: pointer;
		}
		
		.content > ul > li > .destroy {
		    flex: 1;
		}
		
		.content > ul > li > .destroy > button {
		    padding: 5px;
		    font-size: 18px;
		    cursor: pointer;
		}

五、js代碼的實(shí)現(xiàn)

// 獲取localStorage的數(shù)據(jù)時(shí), 為了避免首次進(jìn)入頁面沒有數(shù)據(jù), 所以通過邏輯或給一個(gè)本地?cái)?shù)據(jù)兜底
var cartList = JSON.parse(window.localStorage.getItem("cartList")) || [
    // 每一個(gè)對(duì)象就是一個(gè)購物車內(nèi)容的數(shù)據(jù)
    {
        id: 111234,
        status: true,
        pic: "https://img1.baidu.com/it/u=2511310783,721605137&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=332",
        name: "我是一個(gè)手機(jī), 不知道是啥",
        price: 100,
        number: 3,
        total: 16,
    },
    {
        id: 123456,
        status: false,
        pic: "https://img1.baidu.com/it/u=1537709578,2453227648&fm=253&fmt=auto&app=120&f=JPEG?w=809&h=500",
        name: "我是一個(gè)電腦, 不知道是啥",
        price: 98.72,
        number: 1,
        total: 7,
    },
    {
        id: 965874,
        status: true,
        pic: "https://img2.baidu.com/it/u=3561506717,735421650&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500",
        name: "我是一個(gè)手紙, 不知道是啥",
        price: 356.21,
        number: 2,
        total: 22,
    },
];


// 1. 獲取標(biāo)簽對(duì)稱
const oContent = document.querySelector('.content');

// 2. 封裝函數(shù) 渲染頁面
function myPage() {
    // 定義變量 存儲(chǔ)數(shù)據(jù)
    let selectItem = 0;       //存儲(chǔ)選中商品的數(shù)量
    let selectTotalNum = 0;   //存儲(chǔ)選中商品的總數(shù)量
    let totalPrice = 0;       //存儲(chǔ)選中商品的總價(jià)格

    // 找到選中的商品
    cartList.forEach(function (item) {
        if (item.status == true) {
            selectItem++;
            selectTotalNum += item.number;
            totalPrice += item.price * item.number;
        }
    })

    // 查找數(shù)據(jù) 渲染頁面
    // 選中的商品數(shù)量 如果代表商品總量 代表所有商品被選中
    var str = `
        <div class="top">
			<input type="checkbox" class="checkAll" ${selectItem === cartList.length ? "checked" : ""}> 全選
		</div>
    `;

    cartList.forEach(function (item) {
        str += `
        <ul>
            <li>
                <div class="status">
                    <input type="checkbox" class="checkOther" data-id="${item.id}"  ${item.status ? "checked" : ""}>
                </div>
                <div class="show">
                    <img src="${item.pic}" alt="">
                </div>
                <div class="title">${item.name}</div>
                <div class="price">¥ ${item.price.toFixed(2)}</div>
                <div class="number">
                    <button class="reduce-btn"  data-id="${item.id}">-</button>
                    <input type="text" value="${item.number}">
                    <button class="increase-btn" data-id="${item.id}">+</button>
                </div>
                <div class="sub">¥ ${(item.number * item.price).toFixed(2)}</div>
                <div class="destroy">
                    <button class="del" data-id="${item.id}">刪除</button>
                </div>
            </li>
        </ul>
    `;
    })

    str += `
		<div class="bottom">
        <div class="totalNum">總件數(shù) : ${selectTotalNum}</div>
        <div class="btns">
            <button class="clearShopCart">清空購物車</button>
            <button class="payment" data-totalPrice="${totalPrice}"">去結(jié)算</button>
            <button class="delCheck">刪除所有已選中</button>
        </div>
        <div class="totalPrice">
            總價(jià)格 : ¥ <span>${totalPrice.toFixed(2)}</span>
        </div>
    </div>
    `;
    oContent.innerHTML = str;

    // 將數(shù)據(jù)存儲(chǔ)到localStorage中 便于下次進(jìn)入可以獲取上一次的數(shù)據(jù)
    window.localStorage.cartList = JSON.stringify(cartList);
    // window.localStorage.setItem("cartList", JSON.stringify(cartList));

}

// 調(diào)用函數(shù) 渲染頁面(頁面首次打開頁面)
myPage()

// 3. 購物車的功能實(shí)現(xiàn)

// 利用事件冒泡 把事件委托給統(tǒng)一的父級(jí)
oContent.addEventListener('click', function (e) {
    // 全選效果
    if (e.target.className === 'checkAll') {

        cartList.forEach(function (item) {
            console.log(e.target.checked);
            item.status = e.target.checked;
        })

        // 渲染頁面
        myPage()
    }


    // 清空購物車
    if (e.target.className === 'clearShopCart') {
        var warn = confirm('您確定要清空購物車嗎')
        if (warn) {
            //購物車的數(shù)據(jù)為空
            cartList = [];
            // 渲染頁面
            myPage()
        }
    }

    // 結(jié)算(將選擇的商品總價(jià)計(jì)算打印到控制臺(tái))
    if (e.target.className === 'payment') {
        // console.log(e.target.dataset.totalprice);
        var price = e.target.dataset.totalprice;
        price = Math.round(price)
        confirm(`總價(jià)格為:${price}`)
    }

    // 刪除所有已選中(沒有選中時(shí) 禁止執(zhí)行)
    if (e.target.className === 'delCheck') {
        var warn = confirm('您確定要?jiǎng)h除當(dāng)前選擇的嗎')
        if (!warn) return;
        // 過濾數(shù)組 只留下選中狀態(tài)為false
        cartList = cartList.filter(function (item) {
            return !item.status
        })

        // 渲染頁面
        myPage()
    }

    // 減少商品的數(shù)量
    if (e.target.className === 'reduce-btn') {
        // 減少商品數(shù)量 不能為0
        cartList.forEach(function (item) {
            // 通過商品的ID找到點(diǎn)擊的是哪一個(gè)商品 累減操作
            if (item.id == e.target.dataset.id && item.number >= 2) item.number--;
        })

        // 渲染頁面
        myPage();
    }

    // 增加商品的數(shù)量
    if (e.target.className === 'increase-btn') {
        cartList.forEach(function (item) {
            // 通過商品的ID找到點(diǎn)擊的是哪一個(gè)商品 累加操作
            if (item.id == e.target.dataset.id && item.number < item.total) item.number++;
        })

        // 渲染頁面
        myPage();
    }

    // 選中商品
    if (e.target.className === 'checkOther') {
        // 遍歷數(shù)組找到要修改的商品
        cartList.forEach(function (item) {
            if (item.id == e.target.dataset.id) {
                // 修改商品的選中狀態(tài)
                item.status = !item.status;
            }
        })
        // 渲染頁面
        myPage()
    }

    // 刪除某一項(xiàng)商品
    if (e.target.className === 'del') {
        var warn = confirm('您確定要?jiǎng)h除當(dāng)前的商品嗎');
        if(!warn) return;
        // 遍歷數(shù)組 找到需要?jiǎng)h除的項(xiàng) 將其過濾
        cartList = cartList.filter(function(item){
            return item.id != e.target.dataset.id;
        })
        // 渲染頁面
        myPage()
    }
})

到了這里,關(guān)于【學(xué)習(xí)筆記46】JavaScript購物車的實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Vue實(shí)戰(zhàn)】使用vue實(shí)現(xiàn)購物車案例

    【Vue實(shí)戰(zhàn)】使用vue實(shí)現(xiàn)購物車案例

    1. 實(shí)現(xiàn)步驟 【備注】這里的接口地址目前(2022年6月)還是可以用的哦~ 2. 實(shí)現(xiàn) 2.1 代碼結(jié)構(gòu) 使用的樣式是Boostrap,需要npm i bootstrap,然后在main.js中引入bootstrap。 2.2 Header頭部的代碼 2.3 Goods商品組件代碼 代碼: 2.4 Footer組件代碼 2.5 Counter組件代碼 2.6 App.vue根組件代碼 2.7 event

    2024年02月08日
    瀏覽(21)
  • 使用JavaScript實(shí)現(xiàn)動(dòng)態(tài)生成并管理購物車的深入解析

    在當(dāng)前的互聯(lián)網(wǎng)時(shí)代,電子商務(wù)已成為我們?nèi)粘I畹闹匾M成部分。購物車作為電子商務(wù)網(wǎng)站的核心功能之一,其實(shí)現(xiàn)方式對(duì)于用戶體驗(yàn)至關(guān)重要。本文將深入探討如何使用JavaScript實(shí)現(xiàn)一個(gè)動(dòng)態(tài)生成并管理購物車的功能,并詳細(xì)介紹其實(shí)現(xiàn)細(xì)節(jié),同時(shí)附上相關(guān)代碼。 購物

    2024年01月24日
    瀏覽(28)
  • itheima蒼穹外賣項(xiàng)目學(xué)習(xí)筆記--Day7:緩存商品 / 購物車

    通過Redis來緩存菜品數(shù)據(jù),減少數(shù)據(jù)庫查詢操作。 緩存邏輯分析: 每個(gè)分類下的菜品保存一份緩存數(shù)據(jù) 數(shù)據(jù)庫中菜品數(shù)據(jù)有變更時(shí)清理緩存數(shù)據(jù) 修改用戶端接口 DishController 的 list 方法,加入緩存處理邏輯 修改管理端接口 DishController 的相關(guān)方法,加入清理緩存的邏輯,需要

    2024年02月16日
    瀏覽(23)
  • 使用JavaScript和Vue.js框架開發(fā)的電子商務(wù)網(wǎng)站,實(shí)現(xiàn)商品展示和購物車功能

    引言: 隨著互聯(lián)網(wǎng)的快速發(fā)展和智能手機(jī)的普及,電子商務(wù)行業(yè)正迎來一個(gè)全新的時(shí)代。越來越多的消費(fèi)者選擇網(wǎng)上購物,而不再局限于傳統(tǒng)的實(shí)體店。這種趨勢(shì)不僅僅是改變了消費(fèi)者的習(xí)慣購物,也給企業(yè)帶來了巨大的商機(jī)。為了不斷滿足消費(fèi)者的需求,電子商務(wù)網(wǎng)站需要

    2024年02月15日
    瀏覽(26)
  • 14-案例:購物車

    需求說明: ????????1. 渲染功能 ? ? ? ? ? ? ? ? v-if/v-else v-for :class ? ? ? ? 2. 刪除功能 ? ? ? ? ? ? ? ? 點(diǎn)擊傳參 filter 過濾覆蓋原數(shù)組 ? ? ? ? 3. 修改個(gè)數(shù) ? ? ? ? ? ? ? ? 點(diǎn)擊傳參 find 找對(duì)象 ? ? ? ? 4. 全選反選 ? ? ? ? ? ? ? ? 計(jì)算屬性 computed 完整寫法 get/

    2024年02月12日
    瀏覽(15)
  • [gtp]購物車案例參考

    [gtp]購物車案例參考

    react hooks,購物車案例. 在列表上點(diǎn)擊+或者-更新數(shù)量,調(diào)用接口更新單個(gè)價(jià)格. 點(diǎn)擊table的checkbox勾選后,計(jì)算總價(jià)? 對(duì)于React Hooks和購物車案例,您可以使用useState來管理列表中商品的數(shù)量和總價(jià)。當(dāng)點(diǎn)擊\\\"+“或”-\\\"按鈕時(shí),可以更新相應(yīng)商品的數(shù)量,并通過調(diào)用接口來更新單個(gè)商品

    2024年02月13日
    瀏覽(20)
  • 基于jQuery------購物車案例

    基于jQuery------購物車案例

    目錄 基于jQuery------購物車案例 案例:購物車案例模塊-增減商品數(shù)量分析 案例:購物車案例模塊-修改商品小計(jì)分析? 案例:購物車案例模塊-計(jì)算總計(jì)和總額 案例:購物車案例模塊-刪除商品模塊 ?案例:購物車案例模塊-選中商品添加背景 html ?js css? 效果? ① 核心思路:首

    2024年02月02日
    瀏覽(18)
  • vue購物車案例(源碼)

    vue購物車案例(源碼)

    全部效果的代碼: 最終實(shí)現(xiàn)的效果 ?購物車功能實(shí)現(xiàn) (1)實(shí)現(xiàn)全選,選中購物車中所有內(nèi)容 (2)點(diǎn)擊增加和減少按鈕,每個(gè)商品的數(shù)量發(fā)生變化,對(duì)應(yīng)的金額會(huì)變化 (3)動(dòng)態(tài)計(jì)算總價(jià),商品總數(shù) (4)點(diǎn)擊操作中的刪除,或者下面的刪除所選商品按鈕可以刪除商品 (5)

    2024年02月08日
    瀏覽(33)
  • HTML+CSS+JavaScript:渲染電商站購物車頁面

    HTML+CSS+JavaScript:渲染電商站購物車頁面

    ?根據(jù)下圖渲染購物車頁面 以下是缺失JS部分的代碼,感興趣的小伙伴可以先自己試著寫一寫

    2024年02月14日
    瀏覽(24)
  • JavaWeb(10)——前端綜合案例4(購物車示例)

    JavaWeb(10)——前端綜合案例4(購物車示例)

    ???????購物車需要展示一個(gè)已加入購物車的商品列表,包含商品名稱、商品單價(jià)、購買數(shù)量和操作 等信息,還需要實(shí)時(shí)顯示購買的總價(jià)。其中購買數(shù)量可以增加或減少,每類商品還可以從購物車中移除。最終實(shí)現(xiàn)的效果大致如圖所示。 基礎(chǔ)版 plus版? ? ? ????????先在

    2024年02月05日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包