函数
function group_pinyin(list,group_flag){
var array=[];
$.each(list,function(){
var store=[];
var group = this.FIRST_LETTER;//首字母
var item = this;
var array_index=-1;
var id = this[group_flag];
$.each(array,function(i){
if(this[group]){
store = this[group];
array_index = i;
return false;
}
})
var add = true;
$.each(store,function(){
if(this[group_flag]==id){
add=false;
}
})
if(add)store.push(item);
var obj = {sort:group};
obj[group]=store;
if(array_index!=-1){
array[array_index]=obj;
}else{
array.push(obj);
}
})
return array;
}
引用
ajax({data:data,success:function(json){
var citys = toJSON(json.citys);
//console.log(citys);
citys = group_pinyin(citys,"PROVINCE_ID");
var stores = toJSON(json.stores);
var list = toJSON(json.list);
//citys = citys[0].FIRST_LETTER;
//console.log(stores);
//取得城市
for(var i=0;i<citys.length;i++){
var key = citys[i].sort;
var list = citys[i][key];
$.each(list,function(){
var hotId = this.IS_HOT;
var cityID = this.CITY_ID;
if(hotId == 12781001){
var hotCity = this.SIMPLIFIED_NAME;
$("#city_item_1").append('<a href="javascript:void(0);" data-id="'+cityID+'">'+hotCity+'</a>');
}
var cityName=this.SIMPLIFIED_NAME;
$(".city_"+key).append('<a href="javascript:void(0);" data-id="'+cityID+'">'+cityName+'</a>');
})
}
//FIRST_LETTER
//SIMPLIFIED_NAME CITY_ID
//取得门店
for(var i=0;i<stores.length;i++){
var storeName = stores[i].NAME;
var storeID = stores[i].CITY_ID;
storesAdds.push(storeName);
//console.log(storeName);
};
//取得城市下面的门店
$(".city_item").find("a").on("click",function(event){
event.stopPropagation();
var cityID = parseInt($(this).attr("data-id"));
$(".city_item_adds").show();
$(".city_item_adds").find("ul").empty();
$(".tab_box").hide();
$(".city_item").hide();
var flag=0;
for(var i=0;i<stores.length;i++){
var storeID = stores[i].CITY_ID;
if(storeID == cityID){
flag++;
var storeName = stores[i].NAME;
var store_ID = stores[i].STORE_ID;
$(".city_item_adds").find("ul").append('<li id="'+store_ID+'">'+storeName+'</li>');
}
}
if(flag==0){
$(".city_item_adds").find("ul").empty().append('暂时还没有门店');
}
});
}});