alphabetBrandList.js
7.25 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// pages/goodsList/alphabetBrandList.js
let app = getApp();
const screenHeight = app.globalData.systemInfo.screenHeight;
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const scale = windowWidth / 375;
Page({
data: {
windowWidth,
windowHeight,
screenHeight,
scale,
alphabetList: [],
alpha: '',
windowHeight: '',
filterData: {},
filterList: [],
filterIndex: 0,
storageKey: '',
currentFilter: {},
multiSelect: false,
selectItemsIds: '',
selectItemsName: '',
selectedItems: [],
allSelect: false,
},
onLoad: function (options) {
let key = options.storageKey;
let start = options.index;
let that = this;
wx.getStorage({
key: key,
success: function (res) {
if (res.data.length > 0) {
let filterData = res.data;
let currentFilter = filterData.slice(start, parseInt(start) + 1)[0];
let filterList = currentFilter.allItemList;
if (!filterList) {
return;
}
that.setData({
filterData: filterData,
filterIndex: start,
storageKey: key,
currentFilter: currentFilter,
filterList: filterList,
multiSelect: currentFilter.multiSelect,
selectItemsIds: currentFilter.selectItemsIds ? currentFilter.selectItemsIds : '',
selectItemsName: currentFilter.selectItemsName ? currentFilter.selectItemsName : '',
selectedItems: currentFilter.selectedItems ? currentFilter.selectedItems : [],
});
that.parseData();
}
},
})
},
handlerAlphaTap(e) {
// console.log(e);
let { ap } = e.target.dataset;
this.setData({ alpha: ap });
// console.log( e.target);
},
// 选择品牌
selectBrand: function (event) {
let cell = event.currentTarget.dataset.item;
if (this.data.multiSelect) {
this.multiSelectBrand(cell);
return;
}
let selectedItems = this.data.selectedItems;
let alphabetList = this.data.alphabetList;
selectedItems = [];
if (!cell.isSelected) {
cell.isSelected = true;
selectedItems.push(cell);
}
alphabetList.map((items) => {
items.brandInfo.map((item) => {
if (cell.itemId == item.itemId) {
item.isSelected = !item.isSelected
} else {
item.isSelected = false;
}
});
});
this.setData({
alphabetList,
selectedItems,
allSelect: selectedItems.length == 0
});
this.submitBrand();
},
//多选
multiSelectBrand: function (currentItem) {
let selectedItems = this.data.selectedItems;
let cell = currentItem;
let alphabetList = this.data.alphabetList;
for (let i = 0; i < alphabetList.length; i++) {
let items = alphabetList[i];
if (cell.alif == items.initial) {
for (let j = 0; j < items.brandInfo.length; j++) {
let item = items.brandInfo[j];
if (cell.itemId == item.itemId) {
item.isSelected = !item.isSelected;
if (item.isSelected) {
cell.isSelected = true;
selectedItems.push(cell); //添加
} else {
for (let k = 0; k < selectedItems.length; k++) {
let current = selectedItems[k];
if (cell.itemId == current.itemId) {
selectedItems.splice(k, 1); //删除
break
}
}
}
break;
}
}
break;
};
}
this.setData({
alphabetList,
selectedItems,
allSelect: selectedItems.length == 0
});
},
// 移除选中的品牌
deleteBrand: function (event) {
let index = event.currentTarget.dataset.index;
if (index == -1) {
return;
}
// 1. 从当前选中的数组中移除
let selectedItems = this.data.selectedItems;
selectedItems.splice(index, 1);
// 2. 更换数组的选中状态
let cell = event.currentTarget.dataset.item;
let alphabetList = this.data.alphabetList;
for (let i = 0; i < alphabetList.length; i++) {
let items = alphabetList[i];
if (cell.alif == items.initial) {
for (let j = 0; j < items.brandInfo.length; j++) {
let item = items.brandInfo[j];
if (cell.itemId == item.itemId) {
item.isSelected = false;
break;
}
}
break;
};
}
this.setData({
alphabetList: alphabetList,
selectedItems: selectedItems,
allSelect: selectedItems.length == 0
});
},
// 清空选中的品牌
clearBrand: function (event) {
let alphabetList = this.data.alphabetList;
alphabetList.map((items) => {
items.brandInfo.map((item) => {
item.isSelected = false;
});
});
this.setData({
alphabetList,
selectedItems: [],
allSelect: true
});
},
//返回上一页
submitBrand: function () {
var selectItemsName = '';
var selectItemsIds = '';
let selectedItems = this.data.selectedItems;
selectedItems && selectedItems.map((item, index) => {
selectItemsName = selectItemsName + item.itemName + ',';
selectItemsIds = selectItemsIds + item.itemId + ',';
});
selectItemsName = selectItemsName.slice(0, selectItemsName.length - 1);
selectItemsIds = selectItemsIds.slice(0, selectItemsIds.length - 1);
let currentFilter = this.data.currentFilter;
currentFilter.selectItemsName = selectItemsName;
currentFilter.selectItemsIds = selectItemsIds;
currentFilter.selectedItems = selectedItems;
var filterData = this.data.filterData;
filterData.splice(this.data.filterIndex, 1, currentFilter);
this.setData({
filterData,
selectItemsIds: selectItemsIds,
selectItemsName: selectItemsName,
})
wx.setStorage({
key: this.data.storageKey,
data: filterData
})
wx.navigateBack({
delta: 1
})
},
//解析数据
parseData: function () {
//检索的首字母
let letter = []; //存储检索字母
let alif = /^[a-zA-Z].*/g; //匹配规则
let allItemList = this.data.filterList;
allItemList.map((item) => {
item.isSelected = false;
if (item.alif.match(alif)) {
if (letter.indexOf(item.alif) < 0) {
letter.push(item.alif);
}
} else {
item.alif = 'Top';
if (letter.indexOf('Top') < 0) {
letter.push('Top');
}
}
});
let alphabetList = [];
letter.map((initial) => {
let brandInfo = [];
let alphabet = {};
alphabet.initial = initial;
allItemList.map((item) => {
if (initial == item.alif) {
brandInfo.push(item);
}
})
alphabet.brandInfo = brandInfo;
alphabetList.push(alphabet);
});
let selectedItems = this.data.selectedItems;
alphabetList.map((items) => {
items.brandInfo.map((item) => {
selectedItems && selectedItems.map((selectId) => {
if (selectId.itemId == item.itemId) {
item.isSelected = true;
}
});
});
});
this.setData({
alphabetList,
selectedItems,
windowHeight: windowHeight,
allSelect: selectedItems.length == 0
});
}
})