Files
youfang-app/fangija-zsyf/js/searchDistrict-add.js
gongjin 6f1d45be77 update
2018-04-17 18:00:26 +08:00

187 lines
4.9 KiB
JavaScript

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})
$("#input_address").val(this.searchDistricts[index].name);
$("[name=region]").val(this.searchDistricts[index].region)
$("[name=name]").val(this.searchDistricts[index].name)
$("[name=cityAddress]").val(this.searchDistricts[index].city)
$("[name=address]").val(this.searchDistricts[index].address)
$.closeModal(".popup-about02");
$("input").blur();
}
}
})
$("#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").val(),
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{
$("#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) {
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").val()==""){
$.toast("请选择城市")
return false;
}
$('.historyList').show();
$("#notFound").hide();
$('.hotList').show();
$('.searchList').hide();
addHotDistricts($("#input_city").val());
addHistory();
$.popup(".popup-about02")
// $("input").blur()
});
var hotVue = new Vue({
el: '#hotList',
data: {
hotDists: []
},methods: {
openHotDetail:function(index) {
$("#input_address").val(this.hotDists[index].name);
$("[name=region]").val(this.hotDists[index].region);
$("[name=name]").val(this.hotDists[index].name);
$("[name=cityAddress]").val(this.hotDists[index].city);
// $.closeModal(".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);
}
hotVue.$data.hotDists=dists
}
}).catch(function (error) {
console.log(error);
});
}
var histories = [];
var historyVue = new Vue({
el: '#districtList',
data: {
histories: histories
},methods: {
openHistoryDetail:function(index) {
$("#input_address").val(this.histories[index].name);
$("[name=region]").val(this.histories[index].region);
$("[name=name]").val(this.histories[index].name);
$("[name=cityAddress]").val(this.histories[index].city);
$.closeModal(".popup-about02");
// $("input").blur();
}
}
})
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(){
localStorage.removeItem('history')
historyVue.histories=[];
});