提交 778b0edd 编写于 作者: D dolymood

fix issue #163 , array findIndex

上级 e07b7345
function findIndex(ary, fn) {
if (ary.findIndex) {
return ary.findIndex(fn)
}
let index = -1
ary.some(function (item, i, ary) {
const ret = fn.call(this, item, i, ary)
if (ret) {
index = i
return ret
}
})
return index
}
function deepAssign(to, from) { function deepAssign(to, from) {
for (let key in from) { for (let key in from) {
if (!to[key] || typeof to[key] !== 'object') { if (!to[key] || typeof to[key] !== 'object') {
...@@ -65,4 +80,4 @@ function resetTypeValue(obj, key, defVal) { ...@@ -65,4 +80,4 @@ function resetTypeValue(obj, key, defVal) {
} }
} }
export { deepAssign, createAddAPI, toLocaleDateString, resetTypeValue } export { findIndex, deepAssign, createAddAPI, toLocaleDateString, resetTypeValue }
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<script> <script>
import apiMixin from '../../common/mixins/api' import apiMixin from '../../common/mixins/api'
import pickerMixin from '../../common/mixins/picker' import pickerMixin from '../../common/mixins/picker'
import { deepAssign } from '../../common/helpers/util' import { deepAssign, findIndex } from '../../common/helpers/util'
import { computeNatureMaxDay, formatType } from '../../common/lang/date' import { computeNatureMaxDay, formatType } from '../../common/lang/date'
const COMPONENT_NAME = 'cube-date-picker' const COMPONENT_NAME = 'cube-date-picker'
...@@ -145,13 +145,13 @@ ...@@ -145,13 +145,13 @@
selectedIndex() { selectedIndex() {
let selectedIndex = [] let selectedIndex = []
let data = this.data let data = this.data
let findIndex let index
for (let i = 0; i < this.columnCount && i < 6 - this.startIndex; i++) { for (let i = 0; i < this.columnCount && i < 6 - this.startIndex; i++) {
findIndex = data.findIndex((item) => { index = findIndex(data, (item) => {
return this.valueArray[i] && item.value === this.valueArray[i] return this.valueArray[i] && item.value === this.valueArray[i]
}) })
selectedIndex[i] = findIndex !== -1 ? findIndex : 0 selectedIndex[i] = index !== -1 ? index : 0
data = data[selectedIndex[i]].children data = data[selectedIndex[i]].children
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
</template> </template>
<script> <script>
import { findIndex } from '../../common/helpers/util'
const COMPONENT_NAME = 'cube-select' const COMPONENT_NAME = 'cube-select'
const EVENT_CHANGE = 'change' const EVENT_CHANGE = 'change'
...@@ -67,23 +68,23 @@ ...@@ -67,23 +68,23 @@
return item return item
})] })]
}, },
findIndex() { valueIndex() {
const findIndex = this.adaptOptions[0].findIndex((item) => { const index = findIndex(this.adaptOptions[0], (item) => {
return item.value === this.value return item.value === this.value
}) })
this.picker && this.picker.setData(this.adaptOptions, findIndex !== -1 ? [findIndex] : [0]) this.picker && this.picker.setData(this.adaptOptions, index !== -1 ? [index] : [0])
return findIndex return index
}, },
selectedText() { selectedText() {
return this.findIndex !== -1 ? this.adaptOptions[0][this.findIndex].text : '' return this.valueIndex !== -1 ? this.adaptOptions[0][this.valueIndex].text : ''
} }
}, },
created() { created() {
this.picker = this.$createPicker({ this.picker = this.$createPicker({
title: this.title, title: this.title,
data: this.adaptOptions, data: this.adaptOptions,
selectedIndex: this.findIndex !== -1 ? [this.findIndex] : [0], selectedIndex: this.valueIndex !== -1 ? [this.valueIndex] : [0],
cancelTxt: this.cancelTxt, cancelTxt: this.cancelTxt,
confirmTxt: this.confirmTxt, confirmTxt: this.confirmTxt,
onSelect: this.hided, onSelect: this.hided,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册