Update Error

This commit is contained in:
qimengjie
2018-12-19 01:48:59 +08:00
parent c5f7d27c17
commit 77b99c3f16

View File

@@ -1,3 +1,5 @@
/* 参考文档: https://github.com/IceApriler/miniprogram-picker */
Component({ Component({
/** /**
* 组件的属性列表 * 组件的属性列表
@@ -99,47 +101,52 @@ Component({
const multiArray = [] const multiArray = []
const { countError } = this.checkSourceData(newSourceData) const { countError } = this.checkSourceData(newSourceData)
if (countError > 0) { if (countError > 0) {
console.warn(`miniprogram-picker: 检测到源数组中${countError}个错误,为了方便排查修改已经为你做出了相关提示,请修改后再试,务必保证数据源的数据结构无误。`) console.warn(`miniprogram-picker: 初始化源数组时检测到有${countError}个错误,为了方便排查修改已经为你做出了相关提示,请修改后再试,务必保证数据源的数据结构无误。`)
} } else {
const defaultIndex = this.getDefaultIndex(newSourceData) const defaultIndex = this.getDefaultIndex(newSourceData)
const handle = (source = [], columnIndex = 0) => { const handle = (source = [], columnIndex = 0) => {
// 当前遍历Picker的第columnIndex列 // 当前遍历Picker的第columnIndex列
// 当columnIndex = 0时source表示sourceData其它则表示子集subset // 当columnIndex = 0时source表示sourceData其它则表示子集subset
const _multiArrayColumn0 = [] const _multiArrayColumn0 = []
source.forEach((item, index) => { source.forEach((item, index) => {
if (columnIndex === 0) { if (columnIndex === 0) {
// newSourceData的第0维要单独处理最后unshift到multiArray中 // newSourceData的第0维要单独处理最后unshift到multiArray中
_multiArrayColumn0.push(item[shownFieldName]) _multiArrayColumn0.push(item[shownFieldName])
} }
if (item[shownFieldName] && index === (defaultIndex[columnIndex] || 0)) { if (item[shownFieldName] && index === (defaultIndex[columnIndex] || 0)) {
// 选中的索引和值默认取每列的第0个 // 选中的索引和值默认取每列的第0个
multiIndex.push(index) multiIndex.push(index)
if (columnIndex < steps - 1) { if (columnIndex < steps - 1) {
if (item[subsetFieldName]) { if (item[subsetFieldName]) {
// 开始处理下一维的数据 // 开始处理下一维的数据
const _subsetArr = item[subsetFieldName].map(sub => sub[shownFieldName]) const _subsetArr = item[subsetFieldName].map(sub => sub[shownFieldName])
multiArray.push(_subsetArr) multiArray.push(_subsetArr)
handle(item[subsetFieldName], columnIndex + 1) handle(item[subsetFieldName], columnIndex + 1)
}
} }
} }
})
if (columnIndex === 0) {
multiArray.unshift(_multiArrayColumn0)
} }
})
if (columnIndex === 0) {
multiArray.unshift(_multiArrayColumn0)
} }
handle(newSourceData)
this.setData({
multiIndex,
multiArray
})
} }
handle(newSourceData)
this.setData({
multiIndex,
multiArray
})
}, },
/**
* 获取默认值index
* @param {Array} newSourceData 源数组
*/
getDefaultIndex(newSourceData) { getDefaultIndex(newSourceData) {
const { const {
defaultIndex, defaultIndex,
@@ -152,7 +159,7 @@ Component({
return defaultIndex return defaultIndex
} else if (defaultValue.length) { } else if (defaultValue.length) {
if (!defaultValueUniqueField) { if (!defaultValueUniqueField) {
console.error(new Error('你设置了"defaultValue"字段, 但是没有设置defaultValueUniqueField这将无法识别默认选项请补充后再试。')) this.consoleError(new Error('你设置了"defaultValue"字段, 但是没有设置defaultValueUniqueField这将无法识别默认选项请补充后再试。'))
} }
const _defaultIndex = [] const _defaultIndex = []
const handle = (source = [], columnIndex = 0) => { const handle = (source = [], columnIndex = 0) => {
@@ -163,12 +170,12 @@ Component({
source.forEach((item, index) => { source.forEach((item, index) => {
if (!item[defaultValueUniqueField]) { if (!item[defaultValueUniqueField]) {
Interrupt = true Interrupt = true
console.error(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${defaultValueUniqueField}"字段`)) this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${defaultValueUniqueField}"字段`))
} else { } else {
defaultValue.forEach((def, key) => { defaultValue.forEach((def, key) => {
if (!def[defaultValueUniqueField]) { if (!def[defaultValueUniqueField]) {
Interrupt = true Interrupt = true
console.error(def, new Error(`"defaultValue"中第${key}项(从0开始计算)的对象中缺少"${defaultValueUniqueField}"字段`)) this.consoleError(def, new Error(`"defaultValue"中第${key}项(从0开始计算)的对象中缺少"${defaultValueUniqueField}"字段`))
} }
if (!Interrupt && if (!Interrupt &&
(def[defaultValueUniqueField] === item[defaultValueUniqueField])) { (def[defaultValueUniqueField] === item[defaultValueUniqueField])) {
@@ -208,7 +215,7 @@ Component({
source.forEach((item) => { source.forEach((item) => {
if (!item[shownFieldName]) { if (!item[shownFieldName]) {
countError++ countError++
console.error(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${shownFieldName}"字段`)) this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${shownFieldName}"字段`))
} }
if (item[shownFieldName]) { if (item[shownFieldName]) {
@@ -219,7 +226,7 @@ Component({
handle(item[subsetFieldName], columnIndex + 1) handle(item[subsetFieldName], columnIndex + 1)
} else { } else {
countError++ countError++
console.error(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${subsetFieldName}"字段`)) this.consoleError(item, new Error(`源数组第${columnIndex}维(从0开始计算)的对象中缺少"${subsetFieldName}"字段`))
} }
} }
} }
@@ -237,7 +244,7 @@ Component({
* @param {Array} e.detail.value 用户选择的下标数组。 * @param {Array} e.detail.value 用户选择的下标数组。
*/ */
pickerChange(e) { pickerChange(e) {
console.log('picker发送选择改变携带值为', e.detail.value) // console.log('picker发送选择改变携带值为', e.detail.value)
this.setData({ this.setData({
multiIndex: e.detail.value multiIndex: e.detail.value
@@ -304,7 +311,7 @@ Component({
let { multiIndex } = this.data let { multiIndex } = this.data
const { column, value: changeIndex } = e.detail const { column, value: changeIndex } = e.detail
console.log(`修改了Picker的第${column}列(从0开始计算),选中了第${changeIndex}个值(从0开始计算)`) // console.log(`修改了Picker的第${column}列(从0开始计算),选中了第${changeIndex}个值(从0开始计算)`)
// multiIndex变化了所以也要同步更新multiArray // multiIndex变化了所以也要同步更新multiArray
multiIndex[column] = changeIndex multiIndex[column] = changeIndex
@@ -346,9 +353,21 @@ Component({
}) })
this.triggerEvent('columnchange', e) this.triggerEvent('columnchange', e)
}, },
/**
* 用户点击了取消触发
* @param {Object} e 事件对象
*/
pickerCancel(e) { pickerCancel(e) {
this.triggerEvent('cancel', e) this.triggerEvent('cancel', e)
}, },
/**
* 绑定console.error
* @param {...any} arg 打印参数
*/
consoleError(...arg) {
console.error(...arg)
console.warn('参考文档https://github.com/IceApriler/miniprogram-picker')
}
}, },
attached() { attached() {
if (this.data.autoSelect) { if (this.data.autoSelect) {