|
|
@@ -1 +1,70 @@
|
|
|
-Page({})
|
|
|
+Page({
|
|
|
+ data:{
|
|
|
+ cartList: [
|
|
|
+ { id: 1, name: "商品A", price: 100, checked: false, count: 1 },
|
|
|
+ { id: 2, name: "商品B", price: 200, checked: false, count: 2 },
|
|
|
+ { id: 3, name: "商品C", price: 300, checked: false, count: 1 }
|
|
|
+ ],
|
|
|
+ isAllChecked: false, // 全选状态
|
|
|
+ totalPrice:0,
|
|
|
+ checkedCount:0
|
|
|
+ },
|
|
|
+ // 复选框变化事件
|
|
|
+ handleCheckboxChange(e) {
|
|
|
+ const checkedIds = e.detail.value; // 选中的商品id数组(如 ['1', '2'])
|
|
|
+ const cartList = this.data.cartList.map(item => {
|
|
|
+ item.checked = checkedIds.includes(item.id.toString());
|
|
|
+ return item;
|
|
|
+ });
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ cartList,
|
|
|
+ isAllChecked: checkedIds.length === this.data.cartList.length
|
|
|
+ });
|
|
|
+ this.calculateTotal(); // 计算总价
|
|
|
+ },
|
|
|
+ // 全选/取消全选
|
|
|
+ toggleAll() {
|
|
|
+ const isAllChecked = !this.data.isAllChecked;
|
|
|
+ const cartList = this.data.cartList.map(item => {
|
|
|
+ item.checked = isAllChecked;
|
|
|
+ return item;
|
|
|
+ });
|
|
|
+ this.setData({ cartList, isAllChecked });
|
|
|
+ this.calculateTotal();
|
|
|
+ },
|
|
|
+ // 增加数量
|
|
|
+ increaseCount(e) {
|
|
|
+ const id = e.currentTarget.dataset.id;
|
|
|
+ const cartList = this.data.cartList.map(item => {
|
|
|
+ if (item.id == id) item.count++;
|
|
|
+ return item;
|
|
|
+ });
|
|
|
+ this.setData({ cartList });
|
|
|
+ this.calculateTotal();
|
|
|
+ },
|
|
|
+
|
|
|
+ // 减少数量
|
|
|
+ decreaseCount(e) {
|
|
|
+ const id = e.currentTarget.dataset.id;
|
|
|
+ const cartList = this.data.cartList.map(item => {
|
|
|
+ if (item.id == id && item.count > 1) item.count--;
|
|
|
+ return item;
|
|
|
+ });
|
|
|
+ this.setData({ cartList });
|
|
|
+ this.calculateTotal();
|
|
|
+ },
|
|
|
+ // 计算总价和选中数量
|
|
|
+ calculateTotal() {
|
|
|
+ let totalPrice = 0;
|
|
|
+ let checkedCount = 0;
|
|
|
+ this.data.cartList.forEach(item => {
|
|
|
+ if (item.checked) {
|
|
|
+ totalPrice += item.price * item.count;
|
|
|
+ checkedCount++;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.setData({ totalPrice, checkedCount });
|
|
|
+ }
|
|
|
+
|
|
|
+})
|