基於jquery實作智慧提示控制項intellSeach.js_jquery
一、需求
我們常常會遇到【站內搜尋】的需求,為了提高使用者體驗,我們希望能做到像百度那樣的即時智慧提示。例如:某公司人事管理系統,想搜尋李XX,只要輸入“李”,系統自然會提示一些姓李的員工,這樣方便用戶使用。說穿了,就是用戶邊輸入,系統會提示相關的結果;或者,當用戶點擊搜尋框時,就推薦一些內容,如360、百度都會提示今天的主要新聞或搜尋量大的內容。
jquery 已經有一個這樣的插件了,叫 autocomplete, 但我覺得不好用。關於autocomplete的介紹也很多,有興趣的朋友可以去試試。
看標題就知道,這裡只是分享一個插件,不會討論後台搜尋的相關演算法和過程,也就是說,後台返回特定格式的數據,控制項負責渲染結果呈現。 ok,先看一下效果圖:
樣式與控制項無關,只需要一個 input text 就可以了。
二、參數說明
控制以json格式作為傳輸格式。參數比較多,大部分都有預設值(具體看源碼),有些可能不常用,保持預設即可。如下:
url: 請求位址。如:Handler.ashx, 後台取得資料的位址
property: 要顯示的json物件的屬性。如果我們直接回傳["tom","tom cat","tom2"] 這樣的形式,那麼該屬性可以不用設定;但有時我們會回傳[{"Name":"tom","ID":" 001"},{"Name":"tom cat","ID":"002"},{"Name":"tom2","ID":"003"}] 這樣的形式,顯示的是Name,那麼設定該屬性為"Name"即可。至於我們想在點擊的時候獲得點擊的項的ID,透過點擊事件即可。
itemNumber: 顯示的項目數目。
isEmptyRequest: focus時,空白是否發動請求。就像前面說的,如果點擊搜尋框時(此時沒有內容),想要推薦一些內容,設定該屬性為true,即會發起請求。
defaultValue: 預設值。通常會是:「請輸入關鍵字...」 這類的提示。
width: 下拉清單寬度。
aligner: 要對齊的元素。
maxHeight: 最高高度。如果設定該高度,超過時就會出現捲軸。
ajax:{
timeout: 逾時時
cache: 是否為快取
},
event:{
setData: 發送請求前觸發。用於設定參數
itemClick: 點選項目觸發
enterKeydown: 按enter鍵觸發
beforeRender: 所有項目呈現前觸發
endRender: 所有項目呈現後觸發
itemBeforeRender: 在項目呈現前觸發
itemAfterRender: 在項目呈現後觸發
beforeSend: 發送請求前觸發。使用者設定請求頭部參數等,相當於jquery ajax 的 beforeSend。
}
event 裡的方法都會在適當的時候觸發,需要注意的是,所有方法都接收一個參數,該參數是一個對象,有4個屬性,某些情況如果沒有該屬性的,則為空。包括如下屬性:
jthis: jQuery object of input.
jItem: The jQuery object of the item.
data: The json string returned. If the returned json needs to be processed in the foreground, it can be obtained through the data attribute. After the processing is completed, the json string needs to be returned.
event: Event object, such as the event object when enter is pressed.
3. Example
Usage example:
$("#search").intellSearch({ url:"Handler.ashx", property:"Name", itemNumber:5, isEmptyRequest:false, defaultValue:"请输入关键字...", width:$("#search").width() + 2, maxHeight:-1, event:{ itemClick:function(obj){ alert(obj.item.ID); }, enterKeydown:function(obj){ if(obj.item){ alert("有当前项"); }else{ alert("没有当前项"); } } } });
4. Summary
If you still have your own logic to process and chain calls are supported, you can write $("#search").intellSearch({parameters...}).focus(function(){your processing ...});
Sharing this plug-in hopes to help friends in need. It is mainly used for learning. Since it is v1.0, there may still be some bugs. Friends who find them can also tell me and I will correct them in time.
Source code attached
js code
/*搜索智能提示 v1.0 date:2015.09.08 */ ;(function(w,$){ $.fn.intellSearch = function(options){ var jthis = this; var _dftOpts = { url:"",//请求地址或数组 property:"",//要显示的json对象的属性 itemNumber:5,//显示的条数 isEmptyRequest:false,//focus空白是否发起请求 defaultValue:"",//默认值 width:0,//列表宽度 aligner:jthis,//要对齐的元素 maxHeight:-1,//最大高度 ajax:{ timeout:3000,//超时时间 cache:true//是否缓存 }, event:{ /*参数说明:parameter:{jthis:"jq input",jItem:"jq item",data:"json result",event:"event"}*/ setData:null,//设置参数 itemClick:null,//点击项触发 enterKeydown:null,//按下enter键触发 beforeRender:null,//所有项呈现前触发 endRender:null,//所有项呈现后触发 itemBeforeRender:null,//项呈现前触发 itemAfterRender:null,//项呈现后触发 beforeSend:null//发送请求前触发 } }; $.extend(_dftOpts,options); if(!_dftOpts.url){ throw Error("url不能为空!"); } var jResult; var _value = ""; var _ajax = _dftOpts.ajax; var _event = _dftOpts.event; var _cache = []; var _focusCount = 0;//防止focus触发多次(sogou) /*on window*/ window.intellObj = window.intellObj || {}; /*for global event*/ window.intellDocumentClick = window.intellDocumentClick || function(e){ if(!window.intellObj.jthis){ return; } if(e.target !== window.intellObj.jthis[0]){ setIntellObj(null); } } window.intellDocumentKeydown = window.intellDocumentKeydown || function(e){ var jthis = window.intellObj.jthis; if(!jthis){ return; } var code = e.keyCode; var value = window.intellObj.value; var jResult,jCurItem,keyword; if(code === 13 || code === 38 || code === 40){ jResult = window.intellObj.jResult; jItems = jResult.find("li"); jCurItem = jResult.find("li.cur"); if(code === 13){ if(jCurItem.length > 0){ jCurItem.click(); }else{ setIntellObj(null); if(_event.enterKeydown){ _event.enterKeydown({"jthis":jthis,"event":e}); } } jthis.blur(); }else if(jItems.length > 0){ if(code === 38){ if(jCurItem.length <= 0){ jCurItem = jItems.last(); jCurItem.addClass("cur"); keyword = jCurItem.text(); }else{ var index = jCurItem.index(); jCurItem.removeClass("cur"); if(index <= 0){ keyword = value; }else{ jCurItem = jItems.eq(index-1); jCurItem.addClass("cur"); keyword = jCurItem.text(); } } jthis.val(keyword); }else{ if(jCurItem.length <= 0){ jCurItem = jItems.first(); jCurItem.addClass("cur"); keyword = jCurItem.text(); }else{ var index = jCurItem.index(); jCurItem.removeClass("cur"); if(index + 1 >= jItems.length){ keyword = value; }else{ jCurItem = jItems.eq(index+1); jCurItem.addClass("cur"); keyword = jCurItem.text(); } } jthis.val(keyword); } } } } /*event handler*/ $.fn.unintell = function(){ remove(); } $(document).unbind({click:window.intellDocumentClick,keydown:window.intellDocumentKeydown}) .bind({click:window.intellDocumentClick,keydown:window.intellDocumentKeydown}); jthis.focus(function(){ _focusCount++; if(_focusCount > 1){ return; } if(window.intellObj.jthis && jthis !== window.intellObj.jthis){ setIntellObj(null); } var keyword = attrValue(); if(keyword === _dftOpts.defaultValue){ keyword = ""; attrValue(keyword); } if(keyword || _dftOpts.isEmptyRequest){ sendRequest(); } }) jthis.blur(function(){ _focusCount = 0; if(!attrValue()){ attrValue(_dftOpts.defaultValue); } }) jthis.keyup(function(e){ if(e.keyCode === 38 || e.keyCode === 40){ return; } var keyword = attrValue(); if(!keyword){ remove(); window.intellObj.value = _value = ""; return; } if(keyword !== _value){ window.intellObj.value = _value = keyword; sendRequest(); } }); return initBox(); /*function*/ function initBox(){ attrValue(_dftOpts.defaultValue); return jthis; } function initIntell(){ generate(); register(); setIntellObj({jthis:jthis,jResult:jResult}); } function generate(){ var offset = _dftOpts.aligner.offset(); var width = _dftOpts.width ? _dftOpts.width : _dftOpts.aligner.width(); jResult = $("<ul>",{"class":"intellResult"}); jResult.width(width).css({"position":"absolute","left":offset.left,"top":offset.top + jthis.outerHeight()}); $("body").append(jResult); if(_dftOpts.maxHeight > 0){ jResult.height(_dftOpts.maxHeight).css("overflowY","scroll"); } } function remove(){ if(jResult){ jResult.remove(); jResult = null; } } function register(){ jResult.on("click","li",function(){ var jItem = $(this); var index = jItem.index(); var keyword = jItem.text(); attrValue(keyword); _value = keyword; if(_event.itemClick){ _event.itemClick({"jthis":jthis,"jItem":jItem,"item":_cache[index]}); } }).on("mouseenter","li",function(){ $(this).siblings("li").removeClass("cur").end().addClass("cur"); }).on("mouseleave","li",function(){ $(this).removeClass("cur"); }); } function setIntellObj(obj){ if(!obj){ if(window.intellObj.jResult){ window.intellObj.jResult.remove(); } window.intellObj.jthis = null; window.intellObj.jResult = null; }else{ window.intellObj.jthis = obj.jthis; window.intellObj.jResult = obj.jResult; } } function sendRequest(){ var data; if(_event.setData){ data = _event.setData({"jthis":jthis}); } $.ajax({ url:_dftOpts.url, data:data, cache:_ajax.cache, timeout:_ajax.timeout, beforeSend:function(xhr){ if(_event.beforeSend){ _event.beforeSend(xhr); } }, success:function(data){ remove(); showData(data); }, error:null }); } function showData(data){ data = $.trim(data) ? $.parseJSON(data) : data; if(_event.beforeRender){ var rs = _event.beforeRender({"jthis":jthis,"data":data}); if(rs === false){ return; } if(rs !== undefined){ data = rs; } } if(!data){ return; } var jItem,jA,jSpan,hasProp,item,text,otherTexts,isRender,index; var list = $.isArray(data) ? data : [data]; var length = list.length; length = length > _dftOpts.itemNumber ? _dftOpts.itemNumber : list.length; if(length <= 0){ return; } initIntell(); _cache.length = 0; hasProp = list[0][_dftOpts.property]; for(var i=0;i<length;i++){ item = list[i]; if(item === null || item === undefined){ continue; } text = hasProp ? item[_dftOpts.property] : item; text = $.trim(text.toString()); if(text === ""){ continue; } jItem = $("<li>",{"class":"intellResult_item"}); jA = $("<a>",{"title":text}).appendTo(jItem); jSpan = $("<span>").appendTo(jA); index = text.toLowerCase().indexOf(_value.toLowerCase()); otherTexts = splitText(text,_value,index); if(otherTexts){ jSpan.text(text.substr(index,_value.length)); if(otherTexts.length > 1){ $("<b>",{"text":otherTexts[0]}).insertBefore(jSpan); $("<b>",{"text":otherTexts[1]}).insertAfter(jSpan); }else{ if(index === 0){ $("<b>",{"text":otherTexts[0]}).insertAfter(jSpan); }else{ $("<b>",{"text":otherTexts[0]}).insertBefore(jSpan); } } }else{ jSpan.text(text); } isRender = true; if(_event.itemBeforeRender){ isRender = _event.itemBeforeRender({"jthis":jthis,"jItem":jItem,"item":item}); } if(isRender !== false){ jResult.append(jItem); if(_event.itemAfterRender){ _event.itemAfterRender({"jthis":jthis,"jItem":jItem,"item":item}); } } _cache.push(item); } if(_event.endRender){ _event.endRender({"jthis":jthis}); } jResult.show(); } function attrValue(value){ if(!value && value != ""){ return $.trim(jthis.val()); } jthis.val(value); } function splitText(text,value,index){ var tlength = text.length; var vlength = value.length; if(index === -1){ return null; } if(index === 0){ if(index + vlength >= tlength){ return null; } return [text.substr(index + vlength)]; } if(index + vlength >= tlength){ return [text.substr(0,index)]; } return [text.substr(0,index),text.substr(index + vlength)]; } } })(window,jQuery);
Style
.intellResult{margin:0;padding:0;background:#fff;border:1px solid #b6b6b6;clear:both;z-index:999;display:none;} .intellResult li{margin:0;padding:0;padding:5px 15px;height:20px;line-height:20px;overflow:hidden;text-overflow:ellipsis;cursor:pointer;white-space:nowrap;} .intellResult li.cur{background:#E5E0E0;}
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...
