12 小程式 購物車

2021-10-07 04:04:16 字數 3490 閱讀 8857

資料庫連表查詢

json-server 連線資料中的表

localhost:3000/carts?_expand=good --> 當前購物車商品的詳情資訊

命名要有統一性:

商品資訊資料名:goods

購物車資料id:goodid

購物車介面:localhost:3000/carts?_expand=good

shopcar.wxml

src=

"./shopcar.wxs"

module

="shopcarmodule"

/>

wx:if

="}"

style

="text-align

: center;

">

購物車空空如也

view

>

wx:else

>

bindchange

="handleselectall"

>

value

="隨便寫乙個value"

checked

="}"

>

checkbox

>

全選 checkbox-group

>

wx:for

="}"

wx:key

="index"

class

="shopcar"

bindlongtap

="handelmodel"

data-id

="}"

>

}">

-->

bindtap

="handleboxchange"

data-id

="}"

checked

="}"

>

checkbox

>

src=

"}">

image

>

>

>

}view

>

style

="color

: red;

">

}view

>

class

="add-reduce"

>

bindtap

="handlereduce"

data-id

="}"

>

-text

>

value

="}"

disabled

>

input

>

bindtap

="handleadd"

data-id

="}"

>

+text

>

view

>

view

>

view

>

>

總金額:}

view

>

bindtap

="handlepay"

>

支付button

>

view

>

shopcar.js

page(,

onshow:

function()

`,success:

(res)

=>)}

})},

// 減

handlereduce

(e)return tmpobj

}return item

})this

.setdata()

,// 存到資料庫中

this

.updatecart

(tmpobj)},

// 加

handleadd

(e)return tmpobj

}return item

})this

.setdata()

,// 存到資料庫中

this

.updatecart

(tmpobj)},

// 同步資料庫操作

updatecart

(cartobj)

)// cartobj的物件結構

let= cartobj

wx.request(`

, method:

'put'

, data:

, success:()

=>})

},// 選中和未選中

handleboxchange

(e)}

)// 更新檢視

this

.setdata()

},// 長按

handelmodel

(e)else

if(res.cancel)}}

)},// 刪除

handledel

(id)

)// 運算元據庫

wx.request(`

, method:

'delete'

, success:

(res)

=>)}

})},

// 全選

handleselectall

(e)))}

)// 同步資料庫(json-server中沒有好的方法可以一次性put修改多個值,只能寫乙個遍歷,遍歷每乙個id進行更新)

// 真實工作中使用put進行更新

}else))

})// 同步資料庫}}

,// 支付

handlepay()

,fail

(res)})

}})

shopcar.wxss

.shopcar

/* 商品 */

.shopcar image

/* 加減數量 */

.add-reduce

.add-reduce input

shopcar.wxs

// wxs 不支援es6

// 計算總金額

vartotal

=function

(carlist)})

return sum

}// 全選/不全選/全不選

function

checked

(carlist)

var ischecked = carlist.

every

(function

(item)

)return ischecked

}// commonjs規範匯出

module.exports =

購物車小程式

coding utf 8 shp iphone 5000 offee 35 shoes 800 pric list e int input 請輸入賬戶金額 while true for i,p in enumerate shp print i,p 0 p 1 enther input 請輸入要購買的...

Python購物車小程式

問題描述 商店有一系列商品,給出購買序列,然後輸入你的金額,然後選擇要購買的商品序號或是退出,如果持續購買餘額不足則提醒使用者餘額不足,然後如果使用者退出則列印使用者購買的商品列表,並列印使用者餘額。購物車程式 product list mac 9000 kindle 8000 tesla 9000...

小程式購物車實現

class cartmain for wx key key wx for index index class cart box data index 核取方塊 class icon if type success color rgb 51,138,238 size 20 bindtap select...