### 商品规格的实现
> 规格页面主要封装为组件的形式,将得到的数据传入到子组件当中,很多时候也得根据后端的数据来
> 子组件需要接收的
```
props: {
goodsAttr: {
type: Array,
default: () => [],
},
goods: {
default: function() {
return {}
}
},
item: {
default: function() {
return {}
}
},
},
```
> watch监听
```
watch: {
goods: {
handler(newValue, oldValue) {
this.goodsStockNumber = this.goods.store_nums;
this.goodsPrice = this.goods.sell_price;
let fields = {};
for (let i in this.goods) {
if (this.$utils.in_array(i, this.fields)) {
fields[i] = this.goods[i];
}
}
Object.assign(fields, {
id: this.goods.id,
num: this.number,
isSubmit: true,
selectedSku: {
id: "",
specSelected: ""
}
});
this.selectedGoodsInfo = fields;
},
deep: true
},
/**
* 对于规格的处理
*/
goodsAttr: {
handler(newValue, oldValue) {
// console.log(2, 'goodsAttr');
if (this.goodsAttr.length <= 0) {
return;
}
let arr = [];
this.selectedSku = [];
for (let obj in this.goodsAttr) {
for (let index in this.goodsAttr[obj]['list']) {
if (this.goodsAttr[obj]['list'][index]['selected'] && !this.goodsAttr[obj]['list'][index][
'disable'
]) {
this.selectedSku.push(this.goodsAttr[obj]['list'][index]);
arr.push({
name: this.goodsAttr[obj].name,
value: this.goodsAttr[obj]['list'][index].value
});
}
}
}
let selectedIndex = [];
for (let obj in this.selectedSku) {
selectedIndex.push(this.selectedSku[obj].pid + "_" + this.selectedSku[obj].id);
}
if (this.goodsAttr.length == selectedIndex.length && this.item[selectedIndex.join("_")] != undefined) {
let g = this.item[selectedIndex.join("_")];
this.goodsPrice = g.sell_price;
this.goodsStockNumber = g.store_nums;
if (this.number >= g.store_nums) {
this.number = g.store_nums;
}
this.selectedGoodsInfo.selectedSku.id = g.product_id;
this.selectedGoodsInfo.isSubmit = true;
} else {
this.selectedGoodsInfo.isSubmit = false;
}
this.specSelected = '';
let s = [];
for (let i in arr) {
s.push(arr[i].name + ":" + arr[i].value);
}
if (s.length > 0) {
this.selectedGoodsInfo.selectedSku.specSelected = s.join(",");
this.specSelected = "己选择:" + s.join(",");
}
// console.log(5, this.selectedGoodsInfo);
},
deep: true
},
},
```
> 规格点击
```
/**
* 属性选择点击
*/
onAttrSize(id, value, index, idx, pid) {
let specArray = [];
for (let i in this.goodsAttr) {
specArray[i] = "[A-Za-z0-9_\\:\\,]+";
if (id == this.goodsAttr[i]['id']) {
for (let j in this.goodsAttr[i]['list']) {
let value = this.goodsAttr[i]['list'][j];
if (id == value['pid'] && pid == value['id']) {
let flag = !value.selected;
this.$set(this.goodsAttr[i]['list'][j], 'selected', flag);
if (flag == true) {
specArray[i] = value.pid + ":" + value.id;
}
} else {
this.$set(this.goodsAttr[i]['list'][j], 'selected', false);
}
}
}
}
for (let i in this.goodsAttr) {
for (let j in this.goodsAttr[i]['list']) {
let value = this.goodsAttr[i]['list'][j];
let temp = specArray.slice();
temp[i] = value.pid + ":" + value.id;
let flag = true;
for (let j in this.item) {
let reg = new RegExp(temp.join(","));
if (reg.test(this.item[j].key) && this.item[j].store_nums > 0) {
flag = false;
}
}
this.$set(this.goodsAttr[i]['list'][j], 'disable', flag);
}
}
},
```
> 样式实现
```
<view class="size-list">
<div class="list" v-for="(value, idx) in item.list"
:class="{'action': value.selected && value.disable == false}"
@click.stop="onAttrSize(item.id,value,index,idx,value.id)" :key="idx">
<text>{{value.value}}</text>
</div>
</view>
```
#### 实现思想
正在构思中
- 前端指南
- 基础
- HTML、HTTP、web综合问题
- css部分
- 学习指南
- 开发指南
- css指南
- JavaScript
- 视图、文件
- canvas
- 二维码的生成
- 64码及图片
- weui
- Promise
- 第三方js
- 网络请求
- 字符串,数组,时间
- 时间类
- Css
- 布局封装
- 媒体布局
- 九宫格图片自适应
- 两行显示,且省略
- uni-app
- uniapp踩坑指南
- 表单类
- 商品规格
- 页面操作
- H5端返回按钮不显示
- H5解决浏览器跨域问题
- uView——Waterfall 瀑布流
- uniapp中使用复制功能(复制文本到粘贴板)
- 动态导航栏的实现
- React
- React基础
- 微信小程序
- 上传多图
- uni-app 微信小程序生成小程序码二维码带参数
- 小程序分享图片给好友,到朋友圈,保存到本地
- 缓存封装
- Vue
- 深度作用选择器deep
- 使用js实现列表无限循环滚动(横向)
- js 无限循环垂直滚动列表
- 可视化
- AntV
- 玫瑰图