WeChat 미니 프로그램 몰 프로젝트에서 상품 속성 분류 구현

不言
풀어 주다: 2018-06-27 15:06:57
원래의
4844명이 탐색했습니다.

이 글은 주로 위챗 미니 프로그램 몰 프로젝트의 상품 속성 값 연계 선택을 자세히 소개하고 있으며, 관심 있는 친구들은 참고할 수 있습니다.

이전 글에 이어: 위챗 미니 프로그램 쇼핑의 덧셈과 뺄셈 Quantity

쇼핑 수량의 가감에 대해 말씀드렸으니 이제 상품속성값의 연계선택에 대해 말씀드리겠습니다.

학생들의 직관적인 이해를 돕기 위해 전자상거래 사이트에 접속하여 스크린샷을 찍었습니다. 빨간색 원으로 표시된 부분입니다


이제 이 작은 구성요소를 소개하겠습니다. 작은 프로그램에 사용하려면
아래 사진은 이 프로젝트의 사진입니다:


<view class="title">商品属性值联动选择</view> 
<!--options--> 
<view class="commodity_attr_list"> 
 <!--每组属性--> 
 <view class="attr_box" wx:for="{{attrValueList}}" wx:for-item="attrValueObj" wx:for-index="attrIndex"> 
 <!--属性名--> 
 <view class="attr_name">{{attrValueObj.attrKey}}</view> 
 <!--属性值--> 
 <view class="attr_value_box"> 
 <!--每个属性值--> 
 <view class="attr_value {{attrIndex==firstIndex || attrValueObj.attrValueStatus[valueIndex]?(value==attrValueObj.selectedValue?&#39;attr_value_active&#39;:&#39;&#39;):&#39;attr_value_disabled&#39;}}" bindtap="selectAttrValue" data-status="{{attrValueObj.attrValueStatus[valueIndex]}}" 
 data-value="{{value}}" data-key="{{attrValueObj.attrKey}}" data-index="{{attrIndex}}" data-selectedvalue="{{attrValueObj.selectedValue}}" wx:for="{{attrValueObj.attrValues}}" wx:for-item="value" wx:for-index="valueIndex">{{value}}</view> 
 </view> 
 </view> 
</view> 
<!--button--> 
<view class="weui-btn-area"> 
 <button class="weui-btn" type="primary" bindtap="submit">确定</button> 
</view>
로그인 후 복사


wxss:

.title { 
 padding: 10rpx 20rpx; 
 margin: 10rpx 0; 
 border-left: 4rpx solid #ccc; 
} 
 
/*全部属性的主盒子*/ 
.commodity_attr_list { 
 background: #fff; 
 padding: 0 20rpx; 
 font-size: 26rpx; 
 overflow: hidden; 
 width: 100%; 
} 
/*每组属性的主盒子*/ 
.attr_box { 
 width: 100%; 
 overflow: hidden; 
 border-bottom: 1rpx solid #ececec; 
} 
/*属性名*/ 
.attr_name { 
 width: 20%; 
 float: left; 
 padding: 15rpx 0; 
} 
/*属性值*/ 
.attr_value_box { 
 width: 80%; 
 float: left; 
 padding: 15rpx 0; 
 overflow: hidden; 
} 
/*每个属性值*/ 
.attr_value { 
 float: left; 
 padding: 0 10rpx; 
 margin: 0 10rpx; 
 border: 1rpx solid #ececec; 
} 
/*每个属性选中的当前样式*/ 
.attr_value_active { 
 background: #FFCC00; 
 border-radius: 10rpx; 
 color: #fff; 
 padding: 0 10rpx; 
} 
/*禁用属性*/ 
.attr_value_disabled { 
 color: #ccc; 
} 
 
/*button*/ 
.btn-area { 
 margin: 1.17647059em 15px 0.3em; 
} 
 
.btn { 
 margin-top: 15px; 
 background-color:#FFCC00; 
 color: #fff; 
} 
.btn:first-child { 
 margin-top: 0; 
}
로그인 후 복사


js:

데이터 부분은 일반적으로 인터페이스를 통해 데이터를 얻기 위해 여기서는 네트워크 액세스를 사용하지 않습니다. 데모를 단순화하기 위해 데이터 세트가 데이터 개체에 직접 배치됩니다.


Page({ 
 data: { 
 firstIndex: -1, 
 //准备数据 
 //数据结构:以一组一组来进行设定 
 commodityAttr: [ 
 { 
 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:&#39;型号&#39;, 
 attrValueList:[&#39;1&#39;,&#39;2&#39;,&#39;3&#39;] 
 } 
 */ 
 // 把数据对象的数据(视图使用),写到局部内 
 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(&#39;属性索引&#39;, 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(&#39;result&#39;, 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:&#39;型号&#39;, 
 attrValueList:[&#39;1&#39;,&#39;2&#39;,&#39;3&#39;], 
 selectedValue:&#39;1&#39;, 
 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); 
 } 
 
 } 
 }, 
 /* 选中 */ 
 selectValue: function (attrValueList, index, key, value, unselectStatus) { 
 // console.log(&#39;firstIndex&#39;, this.data.firstIndex); 
 var includeGroup = []; 
 if (index == this.data.firstIndex && !unselectStatus) { // 如果是第一个选中的属性值,则该属性所有值可选 
 var commodityAttr = this.data.commodityAttr; 
 // 其他选中的属性值全都置空 
 // console.log(&#39;其他选中的属性值全都置空&#39;, index, this.data.firstIndex, !unselectStatus); 
 for (var i = 0; i < attrValueList.length; i++) { 
 for (var j = 0; j < attrValueList[i].attrValues.length; j++) { 
 attrValueList[i].selectedValue = &#39;&#39;; 
 } 
 } 
 } else { 
 var commodityAttr = this.data.includeGroup; 
 } 
 
 // console.log(&#39;选中&#39;, commodityAttr, index, key, value); 
 for (var i = 0; i < commodityAttr.length; i++) { 
 for (var 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; 
 
 // 判断属性是否可选 
 for (var i = 0; i < attrValueList.length; i++) { 
 for (var j = 0; j < attrValueList[i].attrValues.length; j++) { 
 attrValueList[i].attrValueStatus[j] = false; 
 } 
 } 
 for (var k = 0; k < attrValueList.length; k++) { 
 for (var i = 0; i < includeGroup.length; i++) { 
 for (var j = 0; j < includeGroup[i].attrValueList.length; j++) { 
 if (attrValueList[k].attrKey == includeGroup[i].attrValueList[j].attrKey) { 
 for (var m = 0; m < attrValueList[k].attrValues.length; m++) { 
 if (attrValueList[k].attrValues[m] == includeGroup[i].attrValueList[j].attrValue) { 
 attrValueList[k].attrValueStatus[m] = true; 
 } 
 } 
 } 
 } 
 } 
 } 
 // console.log(&#39;结果&#39;, attrValueList); 
 this.setData({ 
 attrValueList: attrValueList, 
 includeGroup: includeGroup 
 }); 
 
 var count = 0; 
 for (var i = 0; i < attrValueList.length; i++) { 
 for (var j = 0; j < attrValueList[i].attrValues.length; j++) { 
 if (attrValueList[i].selectedValue) { 
 count++; 
 break; 
 } 
 } 
 } 
 if (count < 2) {// 第一次选中,同属性的值都可选 
 this.setData({ 
 firstIndex: index 
 }); 
 } else { 
 this.setData({ 
 firstIndex: -1 
 }); 
 } 
 }, 
 /* 取消选中 */ 
 disSelectValue: function (attrValueList, index, key, value) { 
 var commodityAttr = this.data.commodityAttr; 
 attrValueList[index].selectedValue = &#39;&#39;; 
 
 // 判断属性是否可选 
 for (var i = 0; i < attrValueList.length; i++) { 
 for (var j = 0; j < attrValueList[i].attrValues.length; j++) { 
 attrValueList[i].attrValueStatus[j] = true; 
 } 
 } 
 this.setData({ 
 includeGroup: commodityAttr, 
 attrValueList: attrValueList 
 }); 
 
 for (var i = 0; i < attrValueList.length; i++) { 
 if (attrValueList[i].selectedValue) { 
 this.selectValue(attrValueList, i, attrValueList[i].attrKey, attrValueList[i].selectedValue, true); 
 } 
 } 
 }, 
 /* 点击确定 */ 
 submit: function () { 
 var value = []; 
 for (var i = 0; i < this.data.attrValueList.length; i++) { 
 if (!this.data.attrValueList[i].selectedValue) { 
 break; 
 } 
 value.push(this.data.attrValueList[i].selectedValue); 
 } 
 if (i < this.data.attrValueList.length) { 
 wx.showToast({ 
 title: &#39;请完善属性&#39;, 
 icon: &#39;loading&#39;, 
 duration: 1000 
 }) 
 } else { 
 wx.showToast({ 
 title: &#39;选择的属性:&#39; + value.join(&#39;-&#39;), 
 icon: &#39;sucess&#39;, 
 duration: 1000 
 }) 
 } 
 } 
})
로그인 후 복사

작동 효과:

위 내용은 모두의 학습에 도움이 되기를 바랍니다.

관련 추천 :

WeChat Mini 프로그램 몰에서 사이드바 분류 효과 달성하기

WeChat Mini 프로그램 몰 프로젝트를 위한 쇼핑 수량 가감하기

WeChat Mini 프로그램 쇼핑몰 시스템 개발에 필요한 도구


위 내용은 WeChat 미니 프로그램 몰 프로젝트에서 상품 속성 분류 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!