var xhr; var searchDistVue = new Vue({ el: '#searchList', data: { searchDistricts: [] },methods: { openDistrictDetail:function(index) { saveHistory({district:this.searchDistricts[index].region,name:this.searchDistricts[index].name,city:this.searchDistricts[index].city}) clicked(BASE_PATH+'/district/details?city='+this.searchDistricts[index].city+'®ion='+this.searchDistricts[index].region+'&name='+this.searchDistricts[index].name,true); } } }) $("#districtName").bind("input propertychange keyup" , function () { var keyword = $(this).val() if(xhr){ xhr.abort() } if(keyword == ""){ // $('.searchList #searchList').empty() return } axios.get(BASE_PATH+"/rest/districts/search", { params: { keywords : keyword, cityName:$("#input_city").text(), max:20 } }).then(function (response) { var datas = response.data.data $('.hotList').hide(); $('.historyList').hide(); if(datas == null || datas.length == 0){ $("#notFound").show() searchDistVue.$data.searchDistricts = [] //$('.searchList #searchList').empty(); }else{ console.log(keyword +" "+datas.length); $("#notFound").hide() var dists = []; for(var i in datas){ var o = datas[i] var map={city:o.city, region:o.district, name:o.name,address:o.address}; dists.push(map); } //searchDistVue.$set("searchDistricts") = dists; searchDistVue.$data.searchDistricts=dists $('.searchList').show(); } }).catch(function (error) { alert(error) console.log(error); }); }) function saveHistory(district) { var history = recover() var keys = [] var newHistory = [] newHistory.push(district) keys.push(district.city + "#" + district.region + "#" + district.name) for(var i = 0; i < history.length; i++){ var key = history[i].city + "#" +history[i].region + "#" + history[i].name if($.inArray(key, keys) > -1){ continue } if(newHistory.length < 5){ keys.push(key) newHistory.push(history[i]) }else{ break } } //$.cookie('history',JSON.stringify(newHistory) ,{ expires: 7}) localStorage.setItem("history", JSON.stringify(newHistory)); } function recover() { //var history = $.cookie('history') var history = localStorage.getItem('history'); if(history == undefined || history == ''){ return new Array() } return JSON.parse(history) } /* 弹出搜索小区页面 */ $("body").delegate(".open-about02", "click", function () { $("#districtName").val(""); if($("#input_city").text()==""){ $.toast("请选择城市") return false; } $('.historyList').show(); $("#notFound").hide(); $('.hotList').show(); $('.searchList').hide(); addHotDistricts($("#input_city").text()); addHistory(); $.popup(".popup-about02") $("input").blur() }); //添加热门小区 function addHotDistricts(cityParam){ axios.get(BASE_PATH+"/rest/districts/hotDistricts", { params: { city: cityParam, top:6 } }).then(function (response) { var datas = response.data.data if(datas.length>0){ var dists = []; for(var i in datas){ var o = datas[i] var map={city:o.city, region:o.region, name:o.name}; dists.push(map); } new Vue({ el: '#hotList', data: { hotDists: dists } }) } }).catch(function (error) { alert(error) console.log(error); }); } var histories = []; var historyVue = new Vue({ el: '#districtList', data: { histories: histories },methods: { openHistoryDetail:function(index) { clicked(BASE_PATH+'/district/details?city='+this.histories[index].city+'®ion='+this.histories[index].region+'&name='+this.histories[index].name,true); } } }) function addHistory(){ var history = recover() if(history.length>0){ histories = [] $(history).each(function(){ var map={city:this.city, region:this.district, name:this.name}; histories.push(map) }) historyVue.histories=histories; }/*else{ $(".historyList #districtList").empty(); }*/ } //点击清空 $("#clearSearchHistory").click(function(){ //$.cookie('history',JSON.stringify([]) ,{ expires: 7}); localStorage.removeItem('history') historyVue.histories=[]; //$("#districtList").empty(); //$.popup(".popup-about02") });