关于 devbridge-autocomplete 插件多选操作的实现方法
目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件。
我最终选择了 devbridge 的 autocomplete 插件,主要是不想引用 jquery-ui 的 css 文件。
官方网址:https://www.devbridge.com/sourcery/components/jquery-autocomplete/
先看一下autocomplete的参数
serviceUrl:服务器端的URL或者是返回 Url 字符串的回调函数ajaxSettings:jQuery Ajax 请求的额外配置lookup:查询的数据列表。字符串数组或者对象字面量(格式{ value: 'string', data: any })lookupFilter:function (suggestion, query, queryLowerCase) {}本地数据查询的过滤函数lookupLimit:查询条数限制,默认值:no limitonSelect:function (suggestion) {},用户选择查询结果后的毁掉函数minChars:触发提示的最小单词数,默认值:1maxHeight:提示列表容器的最大高度,默认值:300deferRequestBy:延迟Ajax请求的毫秒数,默认值:0width:提示容器的宽度,默认值:autoparams:参数传递的请求,可选formatResult:function (suggestion, currentValue) {}delimiter:字符串或正则表达式,分割输入值并将最后一个作为查询词,一般逗号分割zIndex:提示容器的z-index值,默认值:9999type:获取提示的 Ajax 请求方式,默认值:getnoCache:是否缓存提示结果,默认值:falseonSearchStart:function (query) {}onSearchComplete:function (query, suggestions) {}onSearchError:function (query, jqXHR, textStatus, errorThrown) {}onInvalidateSelection:function () {},选择提示结果后,如果input的值发生变化则调用函数triggerSelectOnValidInput:如果匹配查询,只要聚焦 input 就触发 onSelect 函数,默认值:truepreventBadQueries:默认值:truebeforeRender:function (container) {}在展示查询结果之前调用函数tabDisabled:默认值:falseparamName:默认值:'query'transformResult:function(response, originalQuery) {}autoSelectFirst:是否自动填充查询列表的第一项,默认值:falseappendTo:查询列表容器被添加到那个元素中,默认值:document.bodydataType:服务器返回的数据格式showNoSuggestionNotice:如果查询结果为空是否有提示语,默认值:falsenoSuggestionNotice:提示语,默认值:No resultsforceFixPosition:默认值:falseorientation:提示容器的垂直位置,默认值:'bottom',可选值'top','auto'groupBy:提示数据对象的属性值
配置参数很多,但是有用的可能就几个。
实现多选的关键参数是 delimiter, onInvalidateSelection , triggerSelectOnValidInput
实际项目中autocomplete的难点在于需要查询结果的索引值并保存到隐藏域中,所以 onInvalidateSelection ,triggerSelectOnValidInput 这两个参数非常关键
示例演示
该演示代码的多选没有删除操作,留给大家一点思考的余地。当然多选的方式不唯一。
See the Pen autocomplete by Zongbin (@nzbin) on CodePen.
感谢您的阅读,如果您对我的文章感兴趣,可以关注我的博客,我是叙帝利,下篇文章再见!
高颜值的渐变编辑器组件,支持所有 CSS 渐变语法 https://github.com/acrodata/gradient-picker
低代码平台必备轻量级 GUI 库 https://github.com/acrodata/gui
适用于 Angular 的 CodeMirror 6 组件 https://github.com/acrodata/code-editor
适用于 Angular 的水印组件(防删除,盲水印) https://github.com/acrodata/watermark
支持拖拽和缩放的弹窗组件 https://github.com/acrodata/rnd-dialog
开发低代码平台的必备拖拽库 https://github.com/ng-dnd/ng-dnd
基于 Angular Material 的中后台管理框架 https://github.com/ng-matero/ng-matero
Angular Material Extensions 扩展组件库 https://github.com/ng-matero/extensions
Unslider 轮播图插件纯 JS 实现 https://github.com/nzbin/unsliderjs
仿 Windows 照片查看器插件 https://github.com/nzbin/photoviewer
仿 Windows 照片查看器插件 jQuery 版 https://github.com/nzbin/magnify
完美替代 jQuery 的模块化 DOM 库 https://github.com/nzbin/domq
简化类名的轻量级 CSS 框架 https://github.com/nzbin/snack
与任意 UI 框架搭配使用的通用辅助类 https://github.com/nzbin/snack-helper
单元素纯 CSS 加载动画 https://github.com/nzbin/three-dots
有趣的 jQuery 卡片抽奖插件 https://github.com/nzbin/CardShow
悬疑科幻电影推荐 https://github.com/nzbin/movie-gallery
锻炼记忆力的小程序 https://github.com/nzbin/memory-stake

浙公网安备 33010602011771号