数组参数赋值

前提:产品列表,可以多选,可以添加数量,点击完成把选择的数据传到上一个页面。再次点击进入产品页面,之前选中的产品不变,

后台获取的参数

this.chosen 新数据, 参数存本地,uni.getStorageSync('chosen')

复制代码
: {id: 78, title: "鼻舒康", image: "", desc: ""}
desc: ""
id: 78
image: ""
title: "鼻舒康"

that.carts:后台返回来的数据

复制代码
0: {id: 78, number: 1, title: "鼻舒康"}
id: 78
number: 1
title: "鼻舒康"
1: {id: 77, number: 1, title: "BTX"}
id: 77
number: 1
title: "BTX"

用本地存储的chosen修改列表参数

复制代码
if (res.data.code == 1) {
						res.data.data.data.map((item, index) => {
							that.$set(item, 'selected', false)
							that.$set(item, 'number', 1)
						})
						that.carts = [...that.carts, ...res.data.data.data]
						that.selectAll = false
						that.chosen = uni.getStorageSync('chosen') || [];
						that.chosen.forEach(chosenItem => {
						  const { id: chosenId, number: chosenNumber } = chosenItem;
						  const targetCartItem = that.carts.find(cartItem => cartItem.id === chosenId);
						  if (targetCartItem) {
						    that.$set(targetCartItem, 'number', chosenNumber);
						    that.$set(targetCartItem, 'selected', true);
						  }
						});
						// that.carts = res.data.data.data
						
						that.total = res.data.data.total
						console.log(that.carts, '12');
						that.islogin = true
						that.loading = false
					} else {
						uni.showToast({
							title: res.data.msg,
							duration: 1500,
							icon: 'none'
						})
					}

重要数据

复制代码
that.chosen = uni.getStorageSync('chosen') || [];
						that.chosen.forEach(chosenItem => {
						  const { id: chosenId, number: chosenNumber } = chosenItem;
						  const targetCartItem = that.carts.find(cartItem => cartItem.id === chosenId);
						  if (targetCartItem) {
						    that.$set(targetCartItem, 'number', chosenNumber);
						    that.$set(targetCartItem, 'selected', true);
						  }
						});

根据id判断 是否选中,选中的数据 selected = true

点击选中需要存本地的参数,

复制代码
that.carts.map((item, index1) => {
				  if (index == index1) {
				    item.selected = !item.selected;
				    if (item.selected == true) {
				      let res = {
				        id: item.id, 
				        number: item.number || 1,
				        title: item.title
				      };
				      const isExist = that.chosen.some(item => item.id === res.id);
				      if (!isExist) {
				        that.chosen.push(res);
				      }
				      uni.setStorageSync('chosen', that.chosen);
				      console.log(that.chosen, 'push');
				    } else {
				      const targetIndex = that.chosen.findIndex(chosenItem => chosenItem.id === item.id);
				      if (targetIndex !== -1) {
				        that.chosen.splice(targetIndex, 1);
				      }
				      uni.setStorageSync('chosen', that.chosen);
				      console.log(that.chosen, 'push22');
				    }
				  }
				});

修改数据里面的number 并且更新到本地存储,

复制代码
/**
			 * 修改商品数量
			 */
			changeNum: function(index, id, number, type) {
				console.log(index, id, number, type);
				var that = this;
				var carts = that.carts;
				var number = parseInt(carts[index].number);
				if (type == 0) {
					number = number - 1;
					if (number <= 1) {
						number = 1;
					}
				} else {
					number = number + 1;
				}

				carts[index].number = number;
				that.goods_numId = number
				that.cartId = [id]
				const targetChosenItem = that.chosen.find(item => item.id === id);
				if (targetChosenItem) {
				  targetChosenItem.number = number; // 同步更新数量
				  // 可选:如果之前有将chosen存入本地存储,这里同步更新本地存储,保证数据一致性
				  uni.setStorageSync('chosen', that.chosen);
				}
				
			},
相关推荐
qiuiuiu4132 小时前
正点原子RK3568学习日志21-实验1-字符设备点亮led
linux·学习
fai厅的秃头姐!2 小时前
01-python基础-day01Linux基础
linux
web小白成长日记2 小时前
从零起步,用TypeScript写一个Todo App:踩坑与收获分享
前端·javascript·typescript
无小道2 小时前
OS中的线程
linux·线程·进程·os·线程库·用户级线程库·线程使用
Q16849645152 小时前
红帽Linux-文件权限管理
linux·运维·服务器
这儿有一堆花2 小时前
Linux 内网环境构建与配置深度解析
linux·数据库·php
fiveym3 小时前
CI/CD 核心原则 + 制品管理全解析:落地要求 + 存储方案
linux·运维·ci/cd
VekiSon3 小时前
ARM架构——从嵌入式系统底层到指令执行解析
linux·arm开发·架构
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [driver][base]container
linux·笔记·学习
圣心3 小时前
Gemini 模型 介绍
前端