|
1 | 1 | /* 参考文档: https://github.com/IceApriler/miniprogram-picker */ |
2 | 2 |
|
| 3 | +function isExist(field) { |
| 4 | + return field !== null && field !== undefined |
| 5 | +} |
| 6 | + |
3 | 7 | Component({ |
4 | 8 | /** |
5 | 9 | * 组件的属性列表 |
@@ -99,53 +103,52 @@ Component({ |
99 | 103 | // 源数组更新,则需要更新multiIndex、multiArray |
100 | 104 | const multiIndex = [] |
101 | 105 | const multiArray = [] |
102 | | - const { countError } = this.checkSourceData(newSourceData) |
103 | | - if (countError > 0) { |
104 | | - console.warn(`miniprogram-picker: 初始化源数组时检测到有${countError}个错误,为了方便排查修改已经为你做出了相关提示,请修改后再试,务必保证数据源的数据结构无误。`) |
105 | | - } else { |
106 | | - const defaultIndex = this.getDefaultIndex(newSourceData) |
107 | | - const handle = (source = [], columnIndex = 0) => { |
108 | | - // 当前遍历Picker的第columnIndex列, |
109 | | - // 当columnIndex = 0时,source表示sourceData,其它则表示子集subset |
110 | | - const _multiArrayColumn0 = [] |
| 106 | + newSourceData = this.checkSourceData(newSourceData) |
111 | 107 |
|
112 | | - source.forEach((item, index) => { |
113 | | - if (columnIndex === 0) { |
114 | | - // newSourceData的第0维要单独处理,最后unshift到multiArray中 |
115 | | - _multiArrayColumn0.push(item[shownFieldName]) |
116 | | - } |
| 108 | + console.warn(newSourceData) |
117 | 109 |
|
118 | | - if (item[shownFieldName] && index === (defaultIndex[columnIndex] || 0)) { |
119 | | - // 选中的索引和值,默认取每列的第0个 |
120 | | - multiIndex.push(index) |
121 | | - |
122 | | - if (columnIndex < steps - 1) { |
123 | | - if (item[subsetFieldName]) { |
124 | | - // 开始处理下一维的数据 |
125 | | - const _subsetArr = item[subsetFieldName].map(sub => sub[shownFieldName]) |
126 | | - multiArray.push(_subsetArr) |
127 | | - handle(item[subsetFieldName], columnIndex + 1) |
128 | | - } |
129 | | - } |
130 | | - } |
131 | | - }) |
| 110 | + const defaultIndex = this.getDefaultIndex(newSourceData) |
| 111 | + const handle = (source = [], columnIndex = 0) => { |
| 112 | + // 当前遍历Picker的第columnIndex列, |
| 113 | + // 当columnIndex = 0时,source表示sourceData,其它则表示子集subset |
| 114 | + const _multiArrayColumn0 = [] |
132 | 115 |
|
| 116 | + source.forEach((item, index) => { |
133 | 117 | if (columnIndex === 0) { |
134 | | - multiArray.unshift(_multiArrayColumn0) |
| 118 | + // newSourceData的第0维要单独处理,最后unshift到multiArray中 |
| 119 | + _multiArrayColumn0.push(item[shownFieldName]) |
135 | 120 | } |
136 | | - } |
137 | 121 |
|
138 | | - handle(newSourceData) |
| 122 | + if (item[shownFieldName] && index === (defaultIndex[columnIndex] || 0)) { |
| 123 | + // 选中的索引和值,默认取每列的第0个 |
| 124 | + multiIndex.push(index) |
139 | 125 |
|
140 | | - this.setData({ |
141 | | - multiIndex, |
142 | | - multiArray |
| 126 | + if (columnIndex < steps - 1) { |
| 127 | + if (item[subsetFieldName]) { |
| 128 | + // 开始处理下一维的数据 |
| 129 | + const _subsetArr = item[subsetFieldName].map(sub => sub[shownFieldName]) |
| 130 | + multiArray.push(_subsetArr) |
| 131 | + handle(item[subsetFieldName], columnIndex + 1) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
143 | 135 | }) |
144 | 136 |
|
145 | | - if (this.data.autoSelect) { |
146 | | - this.processData() |
| 137 | + if (columnIndex === 0) { |
| 138 | + multiArray.unshift(_multiArrayColumn0) |
147 | 139 | } |
148 | 140 | } |
| 141 | + |
| 142 | + handle(newSourceData) |
| 143 | + |
| 144 | + this.setData({ |
| 145 | + multiIndex, |
| 146 | + multiArray |
| 147 | + }) |
| 148 | + |
| 149 | + if (this.data.autoSelect) { |
| 150 | + this.processData() |
| 151 | + } |
149 | 152 | }, |
150 | 153 | /** |
151 | 154 | * 获取默认值index |
@@ -213,34 +216,33 @@ Component({ |
213 | 216 | */ |
214 | 217 | checkSourceData(sourceData) { |
215 | 218 | const { shownFieldName, subsetFieldName, steps } = this.data |
216 | | - let countError = 0 |
217 | 219 | const handle = (source = [], columnIndex = 0) => { |
218 | 220 | // 当前遍历Picker的第columnIndex列, |
219 | 221 | // 当columnIndex = 0时,source表示sourceData,其它则表示子集subset |
220 | | - |
221 | | - source.forEach((item) => { |
222 | | - if (!item[shownFieldName]) { |
223 | | - countError++ |
| 222 | + if (!source.length) { |
| 223 | + const temp = {} |
| 224 | + temp[shownFieldName] = '' |
| 225 | + temp[subsetFieldName] = [] |
| 226 | + source.push(temp) |
| 227 | + } |
| 228 | + return source.map((item) => { |
| 229 | + if (!isExist(item[shownFieldName])) { |
224 | 230 | this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${shownFieldName}"字段`)) |
| 231 | + item[shownFieldName] = '' |
225 | 232 | } |
226 | 233 |
|
227 | | - if (item[shownFieldName]) { |
228 | | - // 有shownFieldName字段才会去遍历subsetFieldName字段 |
229 | | - if (columnIndex < steps - 1) { |
230 | | - if (item[subsetFieldName]) { |
231 | | - // 开始处理下一维的数据 |
232 | | - handle(item[subsetFieldName], columnIndex + 1) |
233 | | - } else { |
234 | | - countError++ |
235 | | - this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${subsetFieldName}"字段`)) |
236 | | - } |
| 234 | + // 有shownFieldName字段才会去遍历subsetFieldName字段 |
| 235 | + if (columnIndex < steps - 1) { |
| 236 | + if (!isExist(item[subsetFieldName])) { |
| 237 | + this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${subsetFieldName}"字段`)) |
237 | 238 | } |
| 239 | + // 开始处理下一维的数据 |
| 240 | + item[subsetFieldName] = handle(item[subsetFieldName], columnIndex + 1) |
238 | 241 | } |
| 242 | + return item |
239 | 243 | }) |
240 | 244 | } |
241 | | - |
242 | | - handle(sourceData) |
243 | | - return { countError } |
| 245 | + return handle(sourceData) |
244 | 246 | }, |
245 | 247 |
|
246 | 248 | /** |
|
0 commit comments