转移中下拉框不随选择框滚动的问题

本文提供了解决CSS浮动布局问题的方法,在main.css文件末尾添加指定代码即可实现修复。适用于div.plr20元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原来的select.js不用动,请在main.css文件的末尾加入下面一句即可:

 

/*add by ruanzhen, fix the bug of floating*/

div.plr20{position: relative;}


<!-- filter-picker.wxml --> <view class="filter-picker"> <!-- 输入部分 --> <view class="input-container" bindtap="toggleDropdown"> <input class="picker-input" placeholder="{{placeholder}}" value="{{searchText}}" bindinput="onInput" bindfocus="onFocus" bindblur="onBlur" disabled="{{disabled}}" /> </view> <!-- 下选项部分 --> <view class="dropdown-container" wx:if="{{dropdownVisible}}"> <scroll-view scroll-y class="dropdown-list" style="max-height: {{maxHeight}}px"> <block wx:for="{{filteredOptions}}" wx:key="key"> <view class="dropdown-item {{item.value === value ? 'selected' : ''}}" bindtap="selectItem" data-value="{{item.value}}" data-item="{{item}}" > {{item.label}} </view> </block> <view wx:if="{{filteredOptions.length === 0}}" class="no-data"> 无匹配数据 </view> </scroll-view> </view> </view> // components/filter-picker/filter-picker.js Component({ properties: { options: Array, value: String, placeholder: String, disabled: Boolean, maxHeight: { type: Number, value: 300 } }, data: { dropdownVisible: false, searchText: ‘’, filteredOptions: [], displayText: ‘’, isActive: false // 新增激活状态 }, methods: { /** * 安全更新筛选后的选项列表 * 1. 确保options是数组 * 2. 过滤掉非对象元素 * 3. 处理label属性缺失的情况 */ updateFilteredOptions() { let { options, searchText } = this.data; const emptyOption = { label: this.properties.placeholder, value: ‘’ } // 确保options是数组,如果是则重置为空数组 if (!Array.isArray(options)) { options = []; this.setData({ options }); } // 过滤掉非对象元素 const validOptions = options.filter(item => item && typeof item === 'object' && 'label' in item ); // 如果没有搜索文本,显示所有有效选项 if (!searchText) { this.setData({ filteredOptions: [emptyOption,...validOptions] }); return; } // 安全过滤:确保item有label属性 const filtered = validOptions.filter(item => { const label = item.label || ''; return label.toString().toLowerCase().includes(searchText.toLowerCase())&&(item.value&&item.value!=''); }); this.setData({ filteredOptions: [emptyOption,...filtered] }); }, /** * 安全更新显示文本 * 1. 处理无效值 * 2. 处理找到匹配项的情况 */ updateDisplayText() { const { value, options } = this.data; // 确保options是有效数组 if (!Array.isArray(options) || options.length === 0) { this.setData({ displayText: '' }); return; } // 处理空值 if (!value) { this.setData({ displayText: '' }); return; } // 查找匹配项,忽略非对象元素 const selected = options.find(item => item && typeof item === 'object' && item.value === value ); if (selected) { this.setData({ displayText: selected.label || '', searchText: selected.label || '' }); } else { this.setData({ displayText: '' }); } }, /** * 处理输入事件 * @param {Object} e - 事件对象 * @param {string} e.detail.value - 输入的值 */ onInput(e) { const searchText = e.detail.value; this.setData({ searchText }, () => { this.updateFilteredOptions(); }); }, /** * 当输入获得焦点时的处理函数 * - 如果组件被禁用则直接返回 * - 设置下拉框可见状态、搜索文本和激活状态 * - 更新过滤后的选项列表 */ onFocus() { if (this.data.disabled) return; this.updateFilteredOptions(); }, /** * 处理输入失去焦点事件 * 延迟200毫秒后隐藏下拉框并取消激活状态 */ onBlur() { }, /** * 切换下拉框的显示/隐藏状态 * 如果组件被禁用则做任何操作 * 当下拉框显示时会触发更新过滤选项的操作 */ toggleDropdown() { if (this.data.disabled) return; this.setData({ dropdownVisible: !this.data.dropdownVisible, isActive: !this.data.dropdownVisible }, () => { if (this.data.dropdownVisible) { this.updateFilteredOptions(); } }); }, /** * 处理选择项事件 * @param {Object} e - 事件对象 * @param {Object} e.currentTarget.dataset - 事件目标数据 * @param {string} e.currentTarget.dataset.value - 选中项的值 * @param {Object} e.currentTarget.dataset.item - 选中项对象 * @param {string} e.currentTarget.dataset.item.label - 选中项的显示文本 * @fires change - 触发change事件,传递选中项的值和对象 */ selectItem(e) { const { value, item } = e.currentTarget.dataset; if(!value){ this.setData({ value, displayText: '', searchText: "", dropdownVisible: false, isActive: false }); }else{ this.setData({ value, displayText: item.label, searchText: item.label, dropdownVisible: false, isActive: false }); } this.triggerEvent('change', { value, item }); } }, // 添加值观察器 observers: { ‘value’: function(newVal) { this.updateDisplayText(); } }, ready() { this.updateFilteredOptions(); this.updateDisplayText(); } }); 再用户滚动下拉框选项时,阻止输入失去焦点,点击下拉框和输入以外得 地方会关闭下拉框
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值