为了让同学们有个直观的了解,到电商网截了一个图片,就是红圈所示的部分
现在就为大家介绍这个小组件,在小程序中,该如何去写
下图为本项目的图:
wxml:
-
<view class="title">商品属性值联动选择</view>
-
-
<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?'attr_value_active':''):'attr_value_disabled'}}" 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>
-
-
<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;
-
}
-
-
-
.btn-area {
-
margin: 1.17647059em 15px 0.3em;
-
}
-
-
.btn {
-
margin-top: 15px;
-
background-color:#FFCC00;
-
color: #fff;
-
}
-
.btn:first-child {
-
margin-top: 0;
-
}
js:
数据部分,一般情况都是访问接口获取数据的,这里并没有使用网络访问,为了简化demo,直接把一组数据放在data对象中。
-
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);
-
-
-
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) {
-
-
-
-
-
-
-
-
-
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);
-
-
-
if (attrIndex >= 0) {
-
-
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]
-
});
-
}
-
}
-
}
-
-
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) {
-
-
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) {
-
-
-
-
-
-
-
-
-
-
-
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) {
-
-
var includeGroup = [];
-
if (index == this.data.firstIndex && !unselectStatus) {
-
var commodityAttr = this.data.commodityAttr;
-
-
-
for (var i = 0; i < attrValueList.length; i++) {
-
for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
-
attrValueList[i].selectedValue = '';
-
}
-
}
-
} else {
-
var commodityAttr = this.data.includeGroup;
-
}
-
-
-
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;
-
}
-
}
-
}
-
}
-
}
-
}
-
-
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 = '';
-
-
-
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: '请完善属性',
-
icon: 'loading',
-
duration: 1000
-
})
-
} else {
-
wx.showToast({
-
title: '选择的属性:' + value.join('-'),
-
icon: 'sucess',
-
duration: 1000
-
})
-
}
-
}
-
})
运行效果: