fuzzyQuery.js
/** * 封装 模糊查询 * 容器id #searchResult * 关键字keyWord keyWord * 数组数据searchDataArr searchDataArr */ function FuzzyQuery(options){ // 元素 var oList = document.getElementById(options.id); // 查询 function query(){ // 关键字 var keyWord = options.keyWord; // 搜索结果 var searchResultList = searchByRegExp(keyWord, options.searchDataArr); // 显示数据 renderAreas(searchResultList); } // 显示数据 function renderAreas(list){ if(!(list instanceof Array)){ return ; } oList.innerHTML = ''; var len = list.length; var item = null; for(var i=0;i<len;i++){ item = document.createElement('li'); item.className = 'mui-table-view-cell'; item.floorId = list[i].floorId; item.modelScene = list[i].modelScene; item.innerHTML = list[i].floorName; item.onclick = function(){ // 跳转传值 if(this.modelScene === 'suidao'){ // 跳转隧道页 mui.openWindow({ url:'query_area_tunnel.html', id:'query_area_tunnel.html', extras:{ floorId:this.floorId, token:options.token } }); }else if(this.modelScene === 'shujing'){ // 跳转竖井页 mui.openWindow({ url:'query_area_shaft.html', id:'query_area_shaft.html', extras:{ floorId:this.floorId, token:options.token } }); } }; oList.appendChild(item); } } //正则匹配 function searchByRegExp(keyWord, list){ if(!(list instanceof Array)){ return ; } var len = list.length; var arr = []; var reg = new RegExp(keyWord); for(var i=0;i<len;i++){ //如果字符串中不包含目标字符会返回-1 if(list[i].floorName.match(reg)){ arr.push(list[i]); } } return arr; } // 触发查询 query(); }
.