Advertisement

微信小程序之商品属性分类-——-微信小程序实战商城系列(4)

阅读量:

该文本描述了一个JavaScript对象的结构及其相关功能实现。主要包含以下内容:
对象包含priceId(商品ID)、price(价格)、stock(库存数量)以及attrValueList(属性值列表)。
attrValueList中包含多个型号、颜色、大小和尺寸组合项。
onShow函数用于处理数据展示逻辑,并对属性进行分类设置。
selectAttrValue函数用于处理用户点击选择属性值的行为,并对其他相关属性进行联动判断。
提交按钮的功能是收集所有选中的属性值,并显示提示信息以确认填写情况。
摘要涵盖了对象结构、各字段含义及主要功能实现逻辑。

priceId: 1,
price: 35.0,
“stock”: 8,
“attrValueList”: [
{
“attrKey”: “型号”,
“attrValue”: “2”
},
{
“attrKey”: “颜色”,
“attrValue”: “白色”
},
{
“attrKey”: “大小”,
“attrValue”: “小”
},
{
“attrKey”: “尺寸”,
“attrValue”: “S”
}
]
},
{
priceId: 2,
price: 35.1,
“stock”: 9,
“attrValueList”: [
{
“attrKey”: “型号”,
“attrValue”: “1”
},
{
“attrKey”: “颜色”,
“attrValue”: “黑色”
},
{
“attrKey”: “大小”,
“attrValue”: “小”
},
{
“attrKey”: “尺寸”,
“attrValue”: “M”
}
]
},
{
priceId: 3,
price: 35.2,
“stock”: 10,
“attrValueList”: [
{
“attrKey”: “型号”,
“attrValue”: “1”
},
{
“attrKey”: “颜色”,
“attrValue”: “绿色”
},
{
“attrKey”: “大小”,
“attrValue”: “大”
},
{
“attrKey”: “尺寸”,
“attrValue”: “L”
}
]
},
{
priceId: 4,
price: 35.2,
“stock”: 10,
“attrValueList”: [
{
“attrKey”: “型号”,
“attrValue”: “1”
},
{
“attrKey”: “颜色”,
“attrValue”: “绿色”
},
{
“attrKey”: “大小”,
“attrValue”: “大”
},
{
“attrKey”: “尺寸”,
“attrValue”: “L”
}
]
}
],
attrValueList: []
},
onShow: function () {
this.setData({
includeGroup: this.data.commodityAttr
});
this.distachAttrValue(this.data.commodityAttr);
// 只有一个属性组合的时候默认选中
// console.log(this.data.attrValueList);
if (this.data.commodityAttr.length == 1) {
for (var i = 0; i < this.data.commodityAttr[0].attrValueList.length; i++) {
this.data.attrValueList[i].selectedValue = this.data.commodityAttr[0].attrValueList[i].attrValue;
}
this.setData({
attrValueList: this.data.attrValueList
});
}
},
/* 获取数据 /
distachAttrValue: function (commodityAttr) {
/
*
将后台返回的数据组合成类似
{
attrKey:‘型号’,
attrValueList:[‘1’,‘2’,‘3’]
}
/
// 把数据对象的数据(视图使用),写到局部内
var attrValueList = this.data.attrValueList;
// 遍历获取的数据
for (var i = 0; i < commodityAttr.length; i++) {
for (var j = 0; j < commodityAttr[i].attrValueList.length; j++) {
var attrIndex = this.getAttrIndex(commodityAttr[i].attrValueList[j].attrKey, attrValueList);
// console.log(‘属性索引’, attrIndex);
// 如果还没有属性索引为-1,此时新增属性并设置属性值数组的第一个值;索引大于等于0,表示已存在的属性名的位置
if (attrIndex >= 0) {
// 如果属性值数组中没有该值,push新值;否则不处理
if (!this.isValueExist(commodityAttr[i].attrValueList[j].attrValue, attrValueList[attrIndex].attrValues)) {
attrValueList[attrIndex].attrValues.push(commodityAttr[i].attrValueList[j].attrValue);
}
} else {
attrValueList.push({
attrKey: commodityAttr[i].attrValueList[j].attrKey,
attrValues: [commodityAttr[i].attrValueList[j].attrValue]
});
}
}
}
// console.log(‘result’, attrValueList)
for (var i = 0; i < attrValueList.length; i++) {
for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
if (attrValueList[i].attrValueStatus) {
attrValueList[i].attrValueStatus[j] = true;
} else {
attrValueList[i].attrValueStatus = [];
attrValueList[i].attrValueStatus[j] = true;
}
}
}
this.setData({
attrValueList: attrValueList
});
},
getAttrIndex: function (attrName, attrValueList) {
// 判断数组中的attrKey是否有该属性值
for (var i = 0; i < attrValueList.length; i++) {
if (attrName == attrValueList[i].attrKey) {
break;
}
}
return i < attrValueList.length ? i : -1;
},
isValueExist: function (value, valueArr) {
// 判断是否已有属性值
for (var i = 0; i < valueArr.length; i++) {
if (valueArr[i] == value) {
break;
}
}
return i < valueArr.length;
},
/
选择属性值事件 /
selectAttrValue: function (e) {
/

点选属性值,联动判断其他属性值是否可选
{
attrKey:‘型号’,
attrValueList:[‘1’,‘2’,‘3’],
selectedValue:‘1’,
attrValueStatus:[true,true,true]
}
console.log(e.currentTarget.dataset);
*/
var attrValueList = this.data.attrValueList;
var index = e.currentTarget.dataset.index;//属性索引
var key = e.currentTarget.dataset.key;
var value = e.currentTarget.dataset.value;
if (e.currentTarget.dataset.status || index == this.data.firstIndex) {
if (e.currentTarget.dataset.selectedvalue == e.currentTarget.dataset.value) {
// 取消选中
this.disSelectValue(attrValueList, index, key, value);
} else {
// 选中
this.selectValue(attrValueList, index, key, value);
}

selectedValue: function(attrValueList, index, key, value, unselectStatus) {
// Log firstIndex value
const includeGroup = [];
if (index === this.data.firstIndex && !unselectStatus) { // 如果是第一个选中的属性值,则该属性所有值可选
const commodityAttr = this.data.commodityAttr;
// 其他选中的属性值全都置空
for (let i = 0; i < attrValueList.length; i++) {
for (let j = 0; j < attrValueList[i].attrValues.length; j++) {
attrValueList[i].selectedValue = '';
}
}
} else {
const commodityAttr = this.data.includeGroup;
}
}

// 记录已选属性项
for (let i = 0; i < commodityAttr.length; i++) {
// 遍历每个属性项
for (let j = 0; j < commodityAttr[i].attrValueList.length; j++) {
// 遍历每个属性值子项
if (
// 检查该子项是否匹配指定的键和值
commodityAttr[i].attrValueList[j].attrKey === key &&
commodityAttr[i].attrValueList[j].attrValue === value
) {
// 将该属性项加入已选组
includeGroup.push(commodityAttr[i]);
}
}
}
// 设置选定值为当前值
attrValueList[index].selectedValue = value;

初始化所有属性状态为不可选。
依次遍历每个属性值列表中的每个属性值,
并将其状态标记为不可选。
随后进行调试信息显示,
将修改后的数据传递给setData函数。
对于每一个属于includeGroup中的对象,
依次遍历其所有的子项,
并针对匹配到的子项进行相应的状态更新:
首先确定当前对象与子项的关键字相符,
随后更新对应的所有匹配项的状态。
整个过程通过多层嵌套循环实现对所有可能组合情况的覆盖。

初始化计数器count为0。
遍历整个属性值列表。
对于列表中的每一个元素:
再次遍历其所有的属性值。
如果当前元素已经被选中,则计数值加一并终止循环。
完成后执行以下操作:
当计数值小于2时:
将data对象的第一索引属性设置为当前索引位置。
否则:
将data对象的第一索引属性设置为-1以表示未被选中。
完成上述操作后继续执行主函数体内的代码。
为了实现取消选定的功能:
在disSelectValue函数中:
获取商品的主数据对象。
将指定位置的属性值清除为空字符串,
从而完成对该商品选定状态的撤销操作。

// 设置属性为可选状态
for (var i = 0, listLength = attrValueList.length; i < listLength; i++) {
for (var j = 0, valuesCount = attrValueList[i].attrValues.length; j < valuesCount; j++) {
attrValueList[i].attrValueStatus[j] := true;
}
}
this.setData({
includeGroup: commodityAttr,
attrValues: attrValueList
});

自我介绍一下:
小编拥有13年上海交大毕业的背景,在小公司有过短暂的工作经历,在华为和OPPO等大厂也有过深入接触。

意识到相当一部分初中级Android开发人员渴望提升个人技能时往往会面临两种主要路径:一种是通过自主学习稳步前进另一种是选择系统化的培训课程。然而面对动辄上万元的培训机构费用这让许多人感到力不从心。单纯靠零散时间自学不仅效率低下而且容易在技术瓶颈处停滞不前难以突破专业发展所需的更高要求。

为了更好地收集整理一份《2024年Android移动开发全套学习资料》,这份资源也是出于一个简单而真诚的目的——那就是希望能为那些希望自学提升却目前又迷茫于从何入手的朋友提供一份力所能及的帮助,并且也能够让大家不用为学习资源的获取而感到困扰。

img
img
img
img

从针对初学者提供的入门级学习材料到覆盖超过95%核心Android开发知识点的高级课程内容,为不同层次的学习者提供了全面且系统化的知识储备。

因为文件较大,在此仅作为参考展示各个节点的内容。具体来说,则不仅包含大厂面经与学习笔记等核心资料包,同时也包含源码讲义、实战项目以及讲解视频等多种资源。此外,在未来还会不断更新和完善这些内容资源库。

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

现在都说互联网寒冬势不可挡,实际上无非是你选择了错误的技术栈,并且技能水平不够(必要条件)。要是你选对了技术路线,自身技术能力过硬,公司换人只是水到渠成的事,怎么可能会被裁员呢?这些人都是被末位淘汰的业务Curd!目前市场上的初级程序员过剩有目共睹,这套教程专为1-6年Android开发工程师设计,旨在帮助那些正处于职业瓶颈期的职场新人,为希望在年后实现职业飞跃并加薪的人提供进阶指导,同时对中高级架构师来说更是得心应手!

为什么其他人总是能一直保持领先位置呢?因为他们本人就很出色,并且始终保持着不断进步的动力。

也许你要问的是:难道你还不感到些许不安?难道你就此满足?

Android 学习之路十分漫长,请各位耐心坚持。

《互联网巨头企业的面试真题解析与系统性的进阶开发技术学习笔记》点击传送门即可获取相关资源!

假如你在车上具备较强的技术实力(即你认为自己已经接近或超越了当前岗位所需的核心能力),那么由于公司可能需要裁员来优化人力成本(换人成本较高),导致你自然不会被裁员。这种岗位多为被淘汰的末线业务岗位(Cur)所代表。如今市场上的初级开发工程师明显过剩,在这一背景下,这套课程专为Android开发工程师提供从初级到中级阶段的学习资源。对于希望从事中高级开发及架构设计的人来说这套教程具有极大的价值

有些人为何总是能超越你?因为他不仅自身能力出众,并且始终保持着不懈的努力。难道你就 content with your current situation and secretly satisfied with your progress? 这种情况是否让你感到不安?

Android架构师之路很漫长,一起共勉吧!

《系统掌握互联网巨头公司面试真题及深入掌握前端开发技术的学习指南》点击传送门即可获取完整版!

全部评论 (0)

还没有任何评论哟~