coupon-select.js
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Component({
properties: {
show: {
type: Boolean,
value: false,
observer: '_show'
},
coupons: {
type: Array,
value: [],
},
selectedCode: {
type: String,
value: '',
},
chosenIdx: {
type: String,
value: ''
},
},
data: {
// chosenIdx: ''
},
methods: {
confirm: function () {
let item = this.data.coupons[this.data.chosenIdx];
let codeType = item && item.coupon_type|| '';
let code = item && item.coupon_code || '';
let amount = item && item.coupon_value_str || '';
this.triggerEvent('confirmselect', { code, amount, codeType})
},
cancel: function () {
this.triggerEvent('cancel')
},
check: function (e) {
let idx = e.currentTarget.dataset.idx;
let that = this;
this.data.coupons.forEach((item, index) => {
let listItem = item;
if (index !== idx && item.isChosen) {
this.setData({
[`coupons[${index}].isChosen`]: false,
[`coupons[${idx}].selected`]: 'N'
});
}
if (index === idx) {
this.setData({
[`coupons[${idx}].isChosen`]: true,
[`coupons[${idx}].selected`]: 'Y'
});
}
});
this.data.chosenIdx = idx;
},
uncheck: function (e) {
let idx = e.currentTarget.dataset.idx;
this.setData({
[`coupons[${idx}].isChosen`]: false,
[`coupons[${idx}].selected`]: 'N'
});
this.data.chosenIdx = '';
},
_show: function (show) {
if (show) {
if (this.data.selectedCode) {
this.data.coupons.forEach((item, index) => {
if (item.coupon_code === this.data.selectedCode) {
this.setData({
[`coupons[${index}].isChosen`]: true
});
this.data.chosenIdx = index;
} else {
this.setData({
[`coupons[${index}].isChosen`]: false
});
}
});
} else {
this.data.coupons.forEach((item, index) => {
this.setData({
[`coupons[${index}].isChosen`]: false
});
});
this.data.chosenIdx = '';
}
}
console.log(this.data.coupons)
}
}
});