This is a custom data drop-down selection imitating Ctrip, which describes some more important parameters, which is convenient for beginners to
/* *id: id The parent element of the current plug-in *data: json Selected data (json format) *bool: true/false Data If you want to separate the data into two values true *config: Configure the internal object of data (type takes effect when bool is true) { id: data field, name: data name, alias: data name first letter, type: data type} *fn: function fn(){} Callback function, click the function executed when generating the tag*/ function YSelect(id,Ydata,bool,config,fn){ var data = []; Yselect_close = {}; var name_list = {A:[],B:[],C:[],D:[],E:[],F:[],G:[],H:[],I:[],J:[],K:[],L:[],M:[],N:[],O:[],P:[],Q:[],R:[],S:[],T:[],U:[],V:[],W:[],X:[],Y:[],Z:[],Other:[]} //Create a new array to prevent undefined var YDropdowns = document.getElementById(id); function Pretreatment(){ //Process data var CatalogData = []; var BrandData = []; var china = []; var foreign = []; for (var i in Ydata) { if (Ydata[i][config.type] == null) CatalogData.push(Ydata[i][config.id]+';'+Ydata[i][config.name]+';'+Ydata[i][config.alias]) else BrandData.push(Ydata[i][config.id]+';'+Ydata[i][config.name]+';'+Ydata[i][config.type]) }; if (BrandData != "") { for (var i = 0 ; i < BrandData.length ; i++) { var str = BrandData[i].split(';') if (str[3] == 0) china.push(str[0] + ';' + str[1] + ';' + str[2]); else foreign.push(str[0] + ';' + str[1] + ';' + str[2]); } if (bool) { data = china; return data; }else { data = foreign; return data; } }else{ data = CatalogData; return data; //Return data} }; function jsondata(data_name,ida){ //Process the data returned by Pretreatment var title = []; //Define the initial letter var Ahtml = {A:[],B:[],C:[],D:[],E:[],F:[],G:[],H:[],I:[],J:[],K:[],L:[],M:[],N:[],O:[],P:[],Q:[],R:[],S:[],T:[],U:[],V:[],W:[],X:[],Y:[],Z:[],Other:[]}; //Create a new array to prevent undefined var Yselect_box = document.createElement("div"); //Create a new selection box parent element Yselect_box.id = ida+"_Yselect_box"; var Yselect_div = document.createElement("div"); //Container with letter return value var Yselect_letter = document.createElement("p"); //26 letter container Yselect_letter.id = ida+'_Yselect_letter' var stylelink = document.createElement("style") //Create skin, with two sets of skins to choose from //stylelink.innerHTML = '#'+id+'_Yselect_box{width: 100%;position:absolute;top:30px;left:0;z-index:9999;background:#ffff}#'+id+'_Yselect_box p{background: #469bde;padding:0 10px;}#'+id+'_Yselect_box p span.close{font: 16px/20px Microsoft Yahei;cursor:pointer;position:absolute;top:0;right:0;color:#f00}#'+id+'_Yselect_box .hide_tag{border: 1px solid #469bde;padding:5px;display: none;min-height:100px;max-height:300px;min-width:300px;max-width:800px;overflow:auto}#'+id+'_Yselect_box pa{padding:5px;line-height: 28px;color:#fff}#'+id+'_Yselect_box .hide_tag a{width:80px;display:block;text-decoration:none;padding:5px;line-height: 12px;font-size:12px;float:left;color:#444;overflow: hidden; text-overflow:ellipsis;white-space:nowrap;}#'+id+'_Yselect_box .hide_tag a:hover{background:#469bde;color:#fff;}#'+id+'_Yselect_box .cur{border-bottom: 2px solid #fac51f}'; stylelink.innerHTML = '#'+ida+'_Yselect_box{width: 100%;min-width:500px;max-width:800px;position:absolute;top:30px;left:0;z-index:9999;background:#fff;border:1px solid #ddd;}#'+ida+'_Yselect_box p{padding:0 10px;}#'+ida+'_Yselect_box p span.close{font: 16px/20px Microsoft Yahei;cursor:pointer;position:absolute;top:0;right:0;color:#666}#'+ida+'_Yselect_box .hide_tag{padding:5px 10px;display: none;min-height:50px;max-height:200px;overflow:auto}#'+ida+'_Yselect_box pa{padding:5px;line-height: 28px;color:#333;border-bottom:2px solid #bbb;font-weight:bold}#'+ida+'_Yselect_box .hide_tag a{width:80px;display:block;text-decoration:none;padding:5px;line-height: 12px;font-size:12px;float:left;color:#444;overflow: hidden; text-overflow:ellipsis;white-space:nowrap;}#'+ida+'_Yselect_box .hide_tag a:hover{background:#469bde;color:#fff;}#'+ida+'_Yselect_box a.cur{border-bottom: 2px solid #469bde;color:#469bde}'; for (var i = 0 ; i < data_name.length ; i++) { var str = data_name[i].split(';') title.push(str[2].charAt(0).toLocaleUpperCase()) //Judge the initial letter and assign it to title }; for(var j in title){ if(name_list[tit[j]]) name_list[tit[j]].push(data_name[j]); else name_list.other.push(data_name[j]) } //Insert data into name_list for (var k in name_list){ // Process data, separate by letters, hide if (name_list[k].length != 0) { for (var i = 0 ; i < name_list[k].length ; i++) { var allhtml = '<a href="javascript:" onclick="'+fn+'(this)" name="'+ name_list[k][i].split(';')[0] + '">' + name_list[k][i].split(';')[1] + '</a>'; Ahtml[k] += allhtml ; }; Yselect_letter.innerHTML += '<a href="javascript:" >'+k+'</a>'; Yselect_div.innerHTML += '<div>'+Ahtml[k]+'</div>'; }; } var bool = true Yselect_box.innerHTML = '<p id="'+ida+'_Yselect_letter'+'">'+Yselect_letter.innerHTML+'<span>x</span></p>'+Yselect_div.innerHTML; YDropdowns.appendChild(Yselect_box); YDropdowns.appendChild(stylelink); //Insert select into the page Yselect_box.getElementsByTagName('p')[0].getElementsByTagName('span')[0].onclick = function Yselect_close(){ YDropdowns.removeChild(Yselect_box) YDropdowns.removeChild(stylelink); bool = false } //Click to destroy select Yselect_close.close = function Yselect_close(obool){ if(obool){ YDropdowns.removeChild(Yselect_box) YDropdowns.removeChild(stylelink); bool = false return }; if (bool) { YDropdowns.removeChild(Yselect_box) YDropdowns.removeChild(stylelink); } } //Destroy the select function return Yselect_close } jsondata(Pretreatment(),id) navlist(id) function navlist(ids){ //tab tab processing function var a = document.getElementById(ids+'_Yselect_letter').getElementsByTagName('a'); var div = document.getElementById(ids+'_Yselect_box').getElementsByTagName('div'); div[0].style.display = "block" ; a[0].className = "cur"; for (var i = 0 ; i < a.length ; i++) { a[i].index = i; a[i].onclick = function(){ for (var j = 0 ; j < a.length ; j++){ div[j].style.display = "none"; a[j].className = ""; }; div[this.index].style.display = "block"; this.className = "cur" }; }; }; }; };