feat(picker): add prop default-value

#255
This commit is contained in:
xuxiaoyan 2018-12-21 12:11:13 +08:00
parent 96572867f7
commit b9d99e3f2c
4 changed files with 34 additions and 13 deletions

View File

@ -25,6 +25,7 @@ Vue.component(Picker.name, Picker)
|data|data source|Array<{value, lable, ...}>[]|`[]`|-|
|cols|number of columns|Number|`1`|-|
|default-index|indexes of initially selected items in each column|Array|`[]`|-|
|default-value|values of initially selected items in each column|Array|`[]`|Available key `text/lable/value`|
|invalid-index|indexes of disabled items in each column|Array|`[]`|array of multiple disabled items, such as `[[1,2], 2]`|
|is-view|inline display in page, otherwise it shows as `Popup`|Boolean|`false`|-|
|is-cascade|data in each column is cascaded or not|Boolean|`false`|see #Appendix for the format of cascaded data|

View File

@ -25,6 +25,7 @@ Vue.component(Picker.name, Picker)
|data|数据源|Array<{value, lable, ...}>[]|`[]`|-|
|cols|数据列数|Number|`1`|-|
|default-index|选择器各列初始选中项索引|Array|`[]`|-|
|default-value|选择器各列初始选中项值|Array|`[]`|可用字段`text/lable/value`|
|invalid-index|选择器各列不可用选项索引|Array|`[]`|某列多个不可用项使用数组,单个使用数字, 如`[[1,2], 2]`|
|is-view|是否内嵌在页面内展示,否则以弹层形式|Boolean|`false`|-|
|is-cascade|各列数据是否级联|Boolean|`false`|级联数据格式见附录|

View File

@ -5,6 +5,22 @@ const defaultOptions = {
maxLevel: 0,
values: [],
defaultIndex: [],
defaultValue: [],
}
function getDefaultIndex(data, defaultIndex, defaultValue) {
let activeIndex = 0
if (defaultIndex !== undefined) {
return defaultIndex
} else if (defaultValue !== undefined) {
data.some((item, index) => {
if (item.text === defaultValue || item.label === defaultValue || item.value === defaultValue) {
activeIndex = index
return true
}
})
}
return activeIndex
}
/**
@ -26,9 +42,9 @@ export default function(picker, options = {}, fn) {
/* istanbul ignore next */
for (let i = options.currentLevel + 1; i < options.maxLevel; i++) {
const activeIndex = options.defaultIndex[i] || 0
const columnValues = values.children || []
const columnValues = (!i ? values[i] : values.children) || []
picker.setColumnValues(i, columnValues)
const activeIndex = getDefaultIndex(columnValues, options.defaultIndex[i], options.defaultValue[i])
values = columnValues[activeIndex] || []
}

View File

@ -88,14 +88,15 @@ export default {
defaultIndex: {
type: Array,
default() {
if (this.cols < 1) {
return []
}
const arr = new Array(this.cols)
for (let i = 0, len = arr.length; i < len; i++) {
arr[i] = 0
}
return arr
// if (this.cols < 1) {
// return []
// }
// const arr = new Array(this.cols)
// for (let i = 0, len = arr.length; i < len; i++) {
// arr[i] = 0
// }
// return arr
return []
},
},
invalidIndex: {
@ -221,13 +222,15 @@ export default {
}
const defaultIndex = this.$_getDefaultIndex()
const defaultIndexOfFirstColumn = defaultIndex[0] || 0
const defaultValue = this.$_getDefaultValue()
// const defaultIndexOfFirstColumn = defaultIndex[0] || 0
this.$nextTick(() => {
cascadePicker(this.column, {
currentLevel: 0,
currentLevel: -1,
maxLevel: this.cols,
values: this.data[0] ? this.data[0][defaultIndexOfFirstColumn] || [] : [],
values: this.data || [],
defaultIndex,
defaultValue,
})
})
},