【项目实战】select+input实现下拉框左右选择+模糊查询功能

本文介绍了一个项目实战案例,通过select和input元素组合,创建了具备下拉框左右选择和模糊查询功能。主要代码包括页面结构和JavaScript实现,实现了在右侧显示已分配的选项,左侧显示未分配的选项,并能进行模糊查询操作。文章强调了操作select的option、去重以及indexOf的正确使用等关键点。
摘要由CSDN通过智能技术生成

前言:

在项目中用遇到下拉框左右选择的需求,而easyui没有找到合适的插件,就想着用listbox来实现,但后来针对这个项目中没有想到合适的办法(ps:大家有了可以推荐哟),从网上看了一些demo,就想着用select来实现下拉框左右选择的效果,再结合input实现select中option项的模糊查询。

主要代码:

页面:

 @*选择窗体*@
    <div id="window" class="easyui-window" title="任务与指标信息" style="width:500px; height:395px" data-options="modal:true,collapsible:false,draggable:false,resizable:false,minimizable:false,maximizable:false,closable:false">
       <div style="display:inline;">
           <div style="display: inline;margin-left:33px"> 
               <input id="searchIndicatorName" class="easyui-textbox"  style="margin-bottom:5px; height: 32px; width: 156px"/>
           </div>
           <div style="display: inline;position:relative;">
               <a id="btnSearch" href="#" class="easyui-linkbutton" data-options="" style="margin-bottom:5px;margin-top:5px;margin-left:-5px">查询</a>
               <a id="btnSearchAll" href="#" class="easyui-linkbutton" data-options="" style="margin-bottom:5px;margin-top:5px;margin-left:3px">所有</a>
   
你可以使用 Vue 3 和 Ant Design Vue 来实现一个下拉框选择器,其中还可以输入并进行模糊搜索。这里是一个简单的示例代码: 首先,安装所需的依赖: ``` npm install vue@next ant-design-vue@next ``` 然后,在你的 Vue 组件中,引入需要的组件和样式: ```vue <template> <a-select v-model="selectedItem" mode="tags" placeholder="请输入关键字" @search="handleSearch" > <a-select-option v-for="option in filteredOptions" :key="option">{{ option }}</a-select-option> </a-select> </template> <script> import { ref, reactive, watch } from 'vue'; import { Select } from 'ant-design-vue'; export default { components: { ASelect: Select, ASelectOption: Select.Option, }, setup() { const options = ['Apple', 'Banana', 'Cherry', 'Durian', 'Elderberry']; const selectedItem = ref([]); const filteredOptions = reactive([]); watch(selectedItem, (newVal) => { console.log(newVal); }); const handleSearch = (value) => { filteredOptions.splice(0); // 清空数组 if (value) { const searchResult = options.filter((option) => option.toLowerCase().includes(value.toLowerCase()) ); filteredOptions.push(...searchResult); } }; return { selectedItem, filteredOptions, handleSearch, }; }, }; </script> <style> @import '~ant-design-vue/dist/antd.css'; </style> ``` 这个示例中,我们使用了 `a-select` 组件来实现下拉框选择器,并将 `mode` 属性设置为 "tags",以支持多选。`placeholder` 属性设置为 "请输入关键字",提供了一个搜索框用于输入关键字。 在 `@search` 事件中,我们通过过滤选项数组来实现模糊搜索功能,并将搜索结果更新到 `filteredOptions` 数组中。 最后,我们使用 `v-for` 指令来渲染 `a-select-option` 组件,将搜索结果展示为可选项。 `v-model` 指令用于绑定选中的项,可以通过 `selectedItem` 引用来获取或操作选中的项。 记得在组件中导入所需的组件和样式。这样,你就可以在你的 Vue 3 项目中使用这个下拉框选择器,并实现输入和模糊搜索了。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值