Home >Web Front-end >JS Tutorial >Automatic prompt function when searching based on javascript_javascript skills
Automatic prompt function when searching based on javascript_javascript skills
WBOYOriginal
2016-05-16 16:24:141009browse
When the amount of data is not very large and there is no functional interface corresponding to the back-end, some simple search functions are basically implemented by the front-end. I happened to use it recently, so I wrote one and posted it to share with everyone:
Rendering:
Function description:
After pressing the keyboard, search for the Chinese characters in the entry, the corresponding pinyin and number of the Chinese characters;
Implementation ideas:
First convert the Chinese characters in the entry into Pinyin, then splice the Chinese characters, Pinyin, and numbers into a regular string, put it into an array, and then determine whether the value in the input is a Chinese character, Pinyin, or a number each time you press the keyboard. , and then loop through the array according to a fixed rule, so that the corresponding entry can be found;
Activation method:
// search-test-inner ---> Outermost div
// search-value ---> input input box
// search-value-list ---> Search results display div
// search-li ---> Search items
new SEARCH_ENGINE("search-test-inner","search-value","search-value-list","search-li");
Note: The search entry plus two temporary data, data-name and data-phone, are used to store Chinese characters and numbers.
Note: Pinyin conversion uses a plug-in called jQuery.Hz2Py-min.js. Since this plug-in can only convert the value in the input, there is an extra step in the code. First, put the value into a temporary input. , and then convert.
//------------------------------------------------ ---【Initialization】
function SEARCH_ENGINE(dom,searchInput,searchResultInner,searchList){
//Array to store Pinyin, Chinese characters and numbers
This.searchMemberArray = [];
//Object of action
This.dom = $("." dom);
//Search box
This.searchInput = "." searchInput;
//Search results box
This.searchResultInner = this.dom.find("." searchResultInner);
//List of search objects
This.searchList = this.dom.find("." searchList);
//Convert to pinyin and store in array
This.transformPinYin();
//Bind search event
This.searchActiveEvent();
}
SEARCH_ENGINE.prototype = {
//-----------------------------【Convert to Pinyin and store Pinyin, Chinese characters, and numbers into the array】
TransformPinYin : function(){
//Temporarily store data objects
$("body").append('');
var $pinyin = $("input.pingying-box");
for(var i=0;i
// Store the name and convert it to Pinyin
$pinyin.val(this.searchList.eq(i).attr("data-name"));
//Convert Chinese characters to Pinyin
var pinyin = $pinyin.toPinyin().toLowerCase().replace(/s/g,"");
//Chinese characters
var cnCharacter = this.searchList.eq(i).attr("data-name");
// Numbers <<>
var digital = this.searchList.eq(i).attr("data-phone");
//Save into array
This.searchMemberArray.push(pinyin "&" cnCharacter "&" digital);
}
//Delete temporary storage data object
$pinyin.remove();
},
//--------------------------------[Fuzzy search keyword]
fuzzySearch : function(type,val){
var s;
var returnArray = [];
//Pinyin
If(type === "pinyin"){
s = 0;
}
//Chinese characters
else if(type === "cnCharacter"){
s = 1;
}
//Number
else if(type === "digital"){
s = 2;
}
for(var i=0;i
// Contains the characters
If(this.searchMemberArray[i].split("&")[s].indexOf(val) >= 0){
returnArray.push(this.searchMemberArray[i]);
}
}
return returnArray;
},
//-----------------------------【Output search results】
PostMemberList : function(tempArray){
var html = '';
//There are search results if(tempArray.length > 0){
html = '
搜索结果(' tempArray.length ')
';
for(var i=0;i
var sArray = tempArray[i].split("&");
html = '
';
html = '' sArray[2] '';
html = '' sArray[1] '';
html = '
Isn’t the effect great? Friends, you can use it in your own projects after beautifying it
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn