Commit 6a744556 by Lin

feat: 销售选择器选中回显

1 parent 9e64325b
......@@ -17,7 +17,11 @@ Component({
// 当前选中的值(整个对象)
selectedValue: {
type: Object,
value: null
value: {
staffPhone: "13132112120",
text: "晨星1号",
value: "173690cbda6d4e8bbccdb3ab485858f8"
}
}
},
......@@ -36,7 +40,11 @@ Component({
*/
lifetimes: {
attached: function() {
// 组件初始化
// 组件初始化时,如果弹框已显示且有数据,进行初始化
const { show, options, selectedValue } = this.properties
if (show && options && options.length > 0) {
this.initPopup()
}
}
},
......@@ -66,24 +74,56 @@ Component({
*/
initPopup: function() {
const { options, selectedValue } = this.properties
// 兼容处理:如果 selectedValue 是字符串,转换为对象;如果是对象,直接使用
let selectedValueObj = selectedValue
if (typeof selectedValue === 'string' && selectedValue) {
// 从 options 中查找对应的对象
const foundOption = options.find(opt => opt.value === selectedValue)
selectedValueObj = foundOption || null
// 兼容处理:支持多种数据格式
// 1. 字符串格式:直接是 value
// 2. 对象格式1:{ staffPhone, text, value } - 组件期望的格式
// 3. 对象格式2:{ staffName, staffId, staffPhone } - 从 filters 传递过来的格式
let selectedValueObj = null
let selectedValueId = null
if (selectedValue) {
if (typeof selectedValue === 'string') {
// 字符串格式:直接是 value
selectedValueId = selectedValue
const foundOption = options.find(opt => opt.value === selectedValue)
if (foundOption) {
selectedValueObj = foundOption
}
} else if (typeof selectedValue === 'object') {
// 对象格式
if (selectedValue.value) {
// 格式1:{ staffPhone, text, value }
selectedValueId = selectedValue.value
selectedValueObj = selectedValue
} else if (selectedValue.staffId) {
// 格式2:{ staffName, staffId, staffPhone }
selectedValueId = selectedValue.staffId
// 从 options 中查找对应的对象
const foundOption = options.find(opt => opt.value === selectedValue.staffId)
if (foundOption) {
selectedValueObj = {
staffPhone: selectedValue.staffPhone || foundOption.staffPhone,
text: selectedValue.staffName || foundOption.text,
value: selectedValue.staffId || foundOption.value
}
}
}
}
}
// 处理选项列表,标记选中状态
let processedOptions = options.map(option => ({
...option,
checked: selectedValueObj && option.value === selectedValueObj.value
checked: selectedValueId && option.value === selectedValueId
}))
// 设置选中的选项ID数组(用于单选模式)
let selectedOptions = []
if (selectedValueObj && selectedValueObj.value) {
const selectedOption = processedOptions.find(opt => opt.value === selectedValueObj.value)
if (selectedValueId) {
const selectedOption = processedOptions.find(opt => opt.value === selectedValueId)
if (selectedOption) {
selectedOptions = [selectedValueObj.value]
selectedOptions = [selectedValueId]
}
}
......@@ -223,8 +263,9 @@ Component({
* 属性观察器
*/
observers: {
// 监听弹框显示、选项列表和选中值的变化
'show, options, selectedValue': function(show, options, selectedValue) {
// 当弹框显示时,初始化数据
// 当弹框显示且有选项数据时,初始化数据(包括选中回显)
if (show && options && options.length > 0) {
this.initPopup()
}
......
......@@ -82,7 +82,7 @@ Page({
async showStaffPopup() {
if(!this.data.staffAbled) return
// 如果员工列表为空,先加载数据
// if (!this.data.staffOptions || this.data.staffOptions.length === 0) {
if (!this.data.staffOptions || this.data.staffOptions.length === 0) {
wx.showLoading({ title: '加载中...', mask: true })
try {
await this.getEmployeeAllList()
......@@ -94,11 +94,11 @@ Page({
wx.hideLoading()
wx.showToast({ title: '加载失败', icon: 'none' })
}
// } else {
// this.setData({
// showStaffPopup: true
// })
// }
} else {
this.setData({
showStaffPopup: true
})
}
},
/**
......
......@@ -101,7 +101,7 @@
<staffSelector
show="{{ showStaffPopup }}"
options="{{ staffOptions }}"
selected-value="{{ filters.staffId }}"
selected-value="{{ filters }}"
bind:close="onCloseStaffPopup"
bind:confirm="onStaffSelectConfirm"
></staffSelector>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!