|
|
@ -12,26 +12,13 @@ |
|
|
|
<u-popup :blur="blur" :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="popupValue" length="auto" :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex"> |
|
|
|
<view class="u-select"> |
|
|
|
<view class="u-select__header" @touchmove.stop.prevent=""> |
|
|
|
<view |
|
|
|
class="u-select__header__cancel u-select__header__btn" |
|
|
|
:style="{ color: cancelColor }" |
|
|
|
hover-class="u-hover-class" |
|
|
|
:hover-stay-time="150" |
|
|
|
@tap.stop="getResult('cancel')" |
|
|
|
> |
|
|
|
<view class="u-select__header__cancel u-select__header__btn" :style="{ color: cancelColor }" hover-class="u-hover-class" :hover-stay-time="150" @tap.stop="getResult('cancel')"> |
|
|
|
{{ cancelText }} |
|
|
|
</view> |
|
|
|
<view class="u-select__header__title"> |
|
|
|
{{ title }} |
|
|
|
</view> |
|
|
|
<view |
|
|
|
class="u-select__header__confirm u-select__header__btn" |
|
|
|
:style="{ color: moving ? cancelColor : confirmColor }" |
|
|
|
hover-class="u-hover-class" |
|
|
|
:hover-stay-time="150" |
|
|
|
@touchmove.stop="" |
|
|
|
@tap.stop="getResult('confirm')" |
|
|
|
> |
|
|
|
<view class="u-select__header__confirm u-select__header__btn" :style="{ color: moving ? cancelColor : confirmColor }" hover-class="u-hover-class" :hover-stay-time="150" @touchmove.stop="" @tap.stop="getResult('confirm')"> |
|
|
|
{{ confirmText }} |
|
|
|
</view> |
|
|
|
</view> |
|
|
@ -73,7 +60,7 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
export default { |
|
|
|
emits: ["update:modelValue", "input", "confirm","cancel"], |
|
|
|
emits: ['update:modelValue', 'input', 'confirm', 'cancel'], |
|
|
|
props: { |
|
|
|
// 通过双向绑定控制组件的弹出与收起 |
|
|
|
value: { |
|
|
@ -88,7 +75,7 @@ export default { |
|
|
|
list: { |
|
|
|
type: Array, |
|
|
|
default() { |
|
|
|
return []; |
|
|
|
return [] |
|
|
|
} |
|
|
|
}, |
|
|
|
// 是否显示边框 |
|
|
@ -124,7 +111,7 @@ export default { |
|
|
|
defaultValue: { |
|
|
|
type: Array, |
|
|
|
default() { |
|
|
|
return [0]; |
|
|
|
return [0] |
|
|
|
} |
|
|
|
}, |
|
|
|
// 模式选择,single-column-单列,mutil-column-多列,mutil-column-auto-多列联动 |
|
|
@ -166,7 +153,7 @@ export default { |
|
|
|
blur: { |
|
|
|
type: [Number, String], |
|
|
|
default: 0 |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
data() { |
|
|
|
return { |
|
|
@ -183,197 +170,195 @@ export default { |
|
|
|
columnNum: 0, |
|
|
|
// 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确 |
|
|
|
moving: false |
|
|
|
}; |
|
|
|
} |
|
|
|
}, |
|
|
|
watch: { |
|
|
|
// 在select弹起的时候,重新初始化所有数据 |
|
|
|
value: { |
|
|
|
immediate: true, |
|
|
|
handler(val) { |
|
|
|
if(val) setTimeout(() => this.init(), 10); |
|
|
|
this.popupValue = val; |
|
|
|
if (val) setTimeout(() => this.init(), 10) |
|
|
|
this.popupValue = val |
|
|
|
} |
|
|
|
}, |
|
|
|
modelValue: { |
|
|
|
immediate: true, |
|
|
|
handler(val) { |
|
|
|
if(val) setTimeout(() => this.init(), 10); |
|
|
|
this.popupValue = val; |
|
|
|
if (val) setTimeout(() => this.init(), 10) |
|
|
|
this.popupValue = val |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
computed: { |
|
|
|
uZIndex() { |
|
|
|
// 如果用户有传递z-index值,优先使用 |
|
|
|
return this.zIndex ? this.zIndex : this.$u.zIndex.popup; |
|
|
|
}, |
|
|
|
return this.zIndex ? this.zIndex : this.$u.zIndex.popup |
|
|
|
} |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
// 标识滑动开始,只有微信小程序才有这样的事件 |
|
|
|
pickstart() { |
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
this.moving = true; |
|
|
|
this.moving = true |
|
|
|
// #endif |
|
|
|
}, |
|
|
|
// 标识滑动结束 |
|
|
|
pickend() { |
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
this.moving = false; |
|
|
|
this.moving = false |
|
|
|
// #endif |
|
|
|
}, |
|
|
|
init() { |
|
|
|
this.setColumnNum(); |
|
|
|
this.setDefaultSelector(); |
|
|
|
this.setColumnData(); |
|
|
|
this.setSelectValue(); |
|
|
|
this.setColumnNum() |
|
|
|
this.setDefaultSelector() |
|
|
|
this.setColumnData() |
|
|
|
this.setSelectValue() |
|
|
|
}, |
|
|
|
// 获取默认选中列下标 |
|
|
|
setDefaultSelector() { |
|
|
|
// 如果没有传入默认选中的值,生成长度为columnNum,用0填充的数组 |
|
|
|
this.defaultSelector = this.defaultValue.length == this.columnNum ? this.defaultValue : Array(this.columnNum).fill(0); |
|
|
|
this.lastSelectIndex = this.$u.deepClone(this.defaultSelector); |
|
|
|
this.defaultSelector = this.defaultValue.length == this.columnNum ? this.defaultValue : Array(this.columnNum).fill(0) |
|
|
|
this.lastSelectIndex = this.$u.deepClone(this.defaultSelector) |
|
|
|
}, |
|
|
|
// 计算列数 |
|
|
|
setColumnNum() { |
|
|
|
// 单列的列数为1 |
|
|
|
if(this.mode == 'single-column') this.columnNum = 1; |
|
|
|
if (this.mode == 'single-column') this.columnNum = 1 |
|
|
|
// 多列时,this.list数组长度就是列数 |
|
|
|
else if(this.mode == 'mutil-column') this.columnNum = this.list.length; |
|
|
|
else if (this.mode == 'mutil-column') this.columnNum = this.list.length |
|
|
|
// 多列联动时,通过历遍this.list的第一个元素,得出有多少列 |
|
|
|
else if (this.mode == 'mutil-column-auto') { |
|
|
|
let num = 1; |
|
|
|
let column = this.list; |
|
|
|
let num = 1 |
|
|
|
let column = this.list |
|
|
|
// 只要有元素并且第一个元素有children属性,继续历遍 |
|
|
|
while (column[0][this.childName]) { |
|
|
|
column = column[0] ? column[0][this.childName] : {}; |
|
|
|
num ++; |
|
|
|
column = column[0] ? column[0][this.childName] : {} |
|
|
|
num++ |
|
|
|
} |
|
|
|
this.columnNum = num; |
|
|
|
this.columnNum = num |
|
|
|
} |
|
|
|
}, |
|
|
|
// 获取需要展示在picker中的列数据 |
|
|
|
setColumnData() { |
|
|
|
let data = []; |
|
|
|
this.selectValue = []; |
|
|
|
let data = [] |
|
|
|
this.selectValue = [] |
|
|
|
if (this.mode == 'mutil-column-auto') { |
|
|
|
// 获得所有数据中的第一个元素 |
|
|
|
let column = this.list[this.defaultSelector.length ? this.defaultSelector[0] : 0]; |
|
|
|
let column = this.list[this.defaultSelector.length ? this.defaultSelector[0] : 0] |
|
|
|
// 通过循环所有的列数,再根据设定列的数组,得出当前需要渲染的整个列数组 |
|
|
|
for (let i = 0; i < this.columnNum; i++) { |
|
|
|
// 第一列默认为整个list数组 |
|
|
|
if (i == 0) { |
|
|
|
data[i] = this.list; |
|
|
|
column = column[this.childName]; |
|
|
|
data[i] = this.list |
|
|
|
column = column[this.childName] |
|
|
|
} else { |
|
|
|
// 大于第一列时,判断是否有默认选中的,如果没有就用该列的第一项 |
|
|
|
data[i] = column; |
|
|
|
column = column[this.defaultSelector[i]][this.childName]; |
|
|
|
data[i] = column |
|
|
|
column = column[this.defaultSelector[i]][this.childName] |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (this.mode == 'single-column') { |
|
|
|
data[0] = this.list; |
|
|
|
data[0] = this.list |
|
|
|
} else { |
|
|
|
data = this.list; |
|
|
|
data = this.list |
|
|
|
} |
|
|
|
this.columnData = data; |
|
|
|
this.columnData = data |
|
|
|
}, |
|
|
|
// 获取默认选中的值,如果没有设置defaultValue,就默认选中每列的第一个 |
|
|
|
setSelectValue() { |
|
|
|
let tmp = null; |
|
|
|
let tmp = null |
|
|
|
for (let i = 0; i < this.columnNum; i++) { |
|
|
|
tmp = this.columnData[i][this.defaultSelector[i]]; |
|
|
|
let data = { |
|
|
|
tmp = this.columnData[i][this.defaultSelector[i]] |
|
|
|
const data = { |
|
|
|
value: tmp ? tmp[this.valueName] : null, |
|
|
|
label: tmp ? tmp[this.labelName] : null |
|
|
|
}; |
|
|
|
} |
|
|
|
// 判断是否存在额外的参数,如果存在,就返回 |
|
|
|
if(tmp && tmp.extra) data.extra = tmp.extra; |
|
|
|
if (tmp && tmp.extra) data.extra = tmp.extra |
|
|
|
this.selectValue.push(data) |
|
|
|
} |
|
|
|
}, |
|
|
|
// 列选项 |
|
|
|
columnChange(e) { |
|
|
|
let index = null; |
|
|
|
let columnIndex = e.detail.value; |
|
|
|
let index = null |
|
|
|
const columnIndex = e.detail.value |
|
|
|
// 由于后面是需要push进数组的,所以需要先清空数组 |
|
|
|
this.selectValue = []; |
|
|
|
this.selectValue = [] |
|
|
|
if (this.mode == 'mutil-column-auto') { |
|
|
|
// 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化 |
|
|
|
this.lastSelectIndex.map((val, idx) => { |
|
|
|
if (val != columnIndex[idx]) index = idx; |
|
|
|
}); |
|
|
|
this.defaultSelector = columnIndex; |
|
|
|
if (val != columnIndex[idx]) index = idx |
|
|
|
}) |
|
|
|
this.defaultSelector = columnIndex |
|
|
|
for (let i = index + 1; i < this.columnNum; i++) { |
|
|
|
// 当前变化列的下一列的数据,需要获取上一列的数据,同时需要指定是上一列的第几个的children,再往后的 |
|
|
|
// 默认是队列的第一个为默认选项 |
|
|
|
this.columnData[i] = this.columnData[i - 1][i - 1 == index ? columnIndex[index] : 0][this.childName]; |
|
|
|
this.columnData[i] = this.columnData[i - 1][i - 1 == index ? columnIndex[index] : 0][this.childName] |
|
|
|
// 改变的列之后的所有列,默认选中第一个 |
|
|
|
this.defaultSelector[i] = 0; |
|
|
|
this.defaultSelector[i] = 0 |
|
|
|
} |
|
|
|
// 在历遍的过程中,可能由于上一步修改this.columnData,导致产生连锁反应,程序触发columnChange,会有多次调用 |
|
|
|
// 只有在最后一次数据稳定后的结果是正确的,此前的历遍中,可能会产生undefined,故需要判断 |
|
|
|
columnIndex.map((item, index) => { |
|
|
|
let data = this.columnData[index][columnIndex[index]]; |
|
|
|
let tmp = { |
|
|
|
const data = this.columnData[index][columnIndex[index]] |
|
|
|
const tmp = { |
|
|
|
value: data ? data[this.valueName] : null, |
|
|
|
label: data ? data[this.labelName] : null, |
|
|
|
}; |
|
|
|
label: data ? data[this.labelName] : null |
|
|
|
} |
|
|
|
// 判断是否有需要额外携带的参数 |
|
|
|
if(data && data.extra !== undefined) tmp.extra = data.extra; |
|
|
|
this.selectValue.push(tmp); |
|
|
|
|
|
|
|
if (data && data.extra !== undefined) tmp.extra = data.extra |
|
|
|
this.selectValue.push(tmp) |
|
|
|
}) |
|
|
|
// 保存这一次的结果,用于下次列发生变化时作比较 |
|
|
|
this.lastSelectIndex = columnIndex; |
|
|
|
this.lastSelectIndex = columnIndex |
|
|
|
} else if (this.mode == 'single-column') { |
|
|
|
let data = this.columnData[0][columnIndex[0]]; |
|
|
|
const data = this.columnData[0][columnIndex[0]] |
|
|
|
// 初始默认选中值 |
|
|
|
let tmp = { |
|
|
|
const tmp = { |
|
|
|
value: data ? data[this.valueName] : null, |
|
|
|
label: data ? data[this.labelName] : null, |
|
|
|
}; |
|
|
|
label: data ? data[this.labelName] : null |
|
|
|
} |
|
|
|
// 判断是否有需要额外携带的参数 |
|
|
|
if(data && data.extra !== undefined) tmp.extra = data.extra; |
|
|
|
this.selectValue.push(tmp); |
|
|
|
if (data && data.extra !== undefined) tmp.extra = data.extra |
|
|
|
this.selectValue.push(tmp) |
|
|
|
} else if (this.mode == 'mutil-column') { |
|
|
|
// 初始默认选中值 |
|
|
|
columnIndex.map((item, index) => { |
|
|
|
let data = this.columnData[index][columnIndex[index]]; |
|
|
|
const data = this.columnData[index][columnIndex[index]] |
|
|
|
// 初始默认选中值 |
|
|
|
let tmp = { |
|
|
|
const tmp = { |
|
|
|
value: data ? data[this.valueName] : null, |
|
|
|
label: data ? data[this.labelName] : null, |
|
|
|
}; |
|
|
|
label: data ? data[this.labelName] : null |
|
|
|
} |
|
|
|
// 判断是否有需要额外携带的参数 |
|
|
|
if(data && data.extra !== undefined) tmp.extra = data.extra; |
|
|
|
this.selectValue.push(tmp); |
|
|
|
if (data && data.extra !== undefined) tmp.extra = data.extra |
|
|
|
this.selectValue.push(tmp) |
|
|
|
}) |
|
|
|
} |
|
|
|
}, |
|
|
|
close() { |
|
|
|
this.$emit('input', false); |
|
|
|
this.$emit("update:modelValue", false); |
|
|
|
this.$emit('input', false) |
|
|
|
this.$emit('update:modelValue', false) |
|
|
|
}, |
|
|
|
// 点击确定或者取消 |
|
|
|
getResult(event = null) { |
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
if (this.moving) return; |
|
|
|
if (this.moving) return |
|
|
|
// #endif |
|
|
|
if (event) this.$emit(event, this.selectValue); |
|
|
|
this.close(); |
|
|
|
if (event) this.$emit(event, this.selectValue) |
|
|
|
this.close() |
|
|
|
}, |
|
|
|
selectHandler() { |
|
|
|
this.$emit('click'); |
|
|
|
this.$emit('click') |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
</script> |
|
|
|
|
|
|
|
<style scoped lang="scss"> |
|
|
|
@import "../../libs/css/style.components.scss"; |
|
|
|
@import '../../libs/css/style.components.scss'; |
|
|
|
|
|
|
|
.u-select { |
|
|
|
|
|
|
|
&__action { |
|
|
|
position: relative; |
|
|
|
line-height: $u-form-item-height; |
|
|
@ -383,7 +368,7 @@ export default { |
|
|
|
position: absolute; |
|
|
|
right: 20rpx; |
|
|
|
top: 50%; |
|
|
|
transition: transform .4s; |
|
|
|
transition: transform 0.4s; |
|
|
|
transform: translateY(-50%); |
|
|
|
z-index: 1; |
|
|
|
|
|
|
@ -415,7 +400,7 @@ export default { |
|
|
|
|
|
|
|
&__body { |
|
|
|
width: 100%; |
|
|
|
height: 300rpx; |
|
|
|
height: 500rpx; |
|
|
|
overflow: hidden; |
|
|
|
background-color: #fff; |
|
|
|
|
|
|
|