//cart.wxml
<van-checkbox-group bindchange="onChange">
<view wx:for="{{carts}}" wx:key="_id" wx:for-item="item">
<van-checkbox value="{{item.checked}}" data-id="{{item._id}}">
<van-card thumb="{{item.imageSrc}}" title="{{item.title}}" desc="{{item.desc}}" price="{{ item.price }}">
<view slot="bottom">
<view class="goods-r-bottom">
<van-button bind:click="subCount" data-id="{{item._id}}" round="true" size="mini">-</van-button>
<view class="goods-r-bottom-text">{{item.num}}</view>
<van-button bind:click="addCount" data-id="{{item._id}}" round="true" size="mini">+</van-button>
</view>
</view>
</van-card>
</van-checkbox>
</view>
</van-checkbox-group>
<van-submit-bar price="{{totalPrice}}" button-text="提交订单" bind:submit="startpay">
<van-checkbox value="{{allChecked}}" clickable bind:change="onAllChange">全选</van-checkbox>
</van-submit-bar>
//cart.js
//处理购物车中商品勾选的变化
onChange(event) {
// 获取选中的复选框值
const checkedValues = event.detail.value;
console.log('event:', event);
// const { value: checkedValues } = event.detail;
// console.log('event:', event);
// 复制购物车数组
const carts = [...this.data.carts];
// 更新每个项目的checked属性
for (let i = 0; i < carts.length; i++) {
const index = carts[i]._id;
carts[i].checked = checkedValues.includes(index);
}
// 检查是否所有商品都已勾选
const allChecked = carts.every(item => item.checked);
// 计算总价
const totalPrice = carts.filter(item => item.checked).reduce((acc, cur) => acc + cur.price, 0);
// 更新数据
this.setData({
carts,
allChecked,
totalPrice
});
},
//勾选全选
onAllChange(event) {
const allChecked = event.detail.length > 0;
const carts = [...this.data.carts].map(item => ({
...item,
checked: allChecked
}));
const totalPrice = allChecked ? carts.reduce((acc, cur) => acc + cur.price, 0) : 0;
this.setData({
carts,
allChecked,
totalPrice
});
},
想要实现vant-checkbox-group组件勾选商品卡片后统计所勾选商品总价的功能,报错
WAServiceMainContext.js:2 TypeError: Cannot read property 'detail' of undefined
at To.onChange (cart.js:121)
at To.onShow (cart.js:26)
at To.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at To.i.__callPageLifeTime__ (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at Bt (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2
at qt (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at r.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at ge.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
at ge.emit (WAServiceMainContext.js:2)(env: Windows,mp,1.06.2303220; lib: 2.23.4)
121行是
const checkedValues = event.detail.value;
购物车内商品来自表carts,属性有_id,checked,price等,请问出了什么错,复选框无法勾选或取消勾选,全选按钮会取消全部选中但是checked属性没变,也无法勾选。