Главная > Javascript, PHP > ui autocomplete пользовательский формат данных и функционал

ui autocomplete пользовательский формат данных и функционал

JS:

$(".input_place").autocomplete({
	source:"/?ajax_action=get_places",
	minLength:4,
	position:{my:"left top",at:"left bottom"},
	select:function(event,ui){
		var loc=$(this).closest('form').attr('action');
		if(ui.item.country_id){
			loc+='?country[]='+ui.item.country_id;
			$('#hidden_country').val(ui.item.country_id);
		}
		if(ui.item.district_id){
			loc+='&district[]='+ui.item.district_id;
			$('#hidden_district').val(ui.item.district_id);
		}
		if(ui.item.city_id){
			loc+='&city[]='+ui.item.city_id;
			$('#hidden_city').val(ui.item.city_id);
		}
		var selected=ui.item.country_name;
		if(ui.item.district_name!=''){
			selected=ui.item.district_name;
		}
		if(ui.item.city_name!=''){
			selected=ui.item.city_name;
		}
		$(this).val(selected);
		if($('#hidden_country').val()==undefined){
			window.location.href=loc;
		}
		return false;
	}
}).autocomplete("instance")._renderItem=function(ul,item){
	var li=item.country_name;
	if(item.district_name!=''){
		li+=' '+item.district_name;
	}
	if(item.city_name!=''){
		li+=' '+item.city_name;
	}
	return $("<li>").append(li).appendTo(ul);
};
if($_GET['ajax_action']=='get_places'){
	$places=array();
	$ajax_countries=get_from_base('*','countries',"shown=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `districts` WHERE`shown`=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `cities` WHERE`shown`=1 and `name` LIKE '%".$_GET['term']."%'))))",'pos');
	if(count($ajax_countries)>0){
		foreach($ajax_countries as $country){
			$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>0,'district_name'=>'','city_id'=>0,'city_name'=>'');
			$ajax_districts=get_from_base('*','districts',"`parent`=".$country['id']." and `shown`=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `cities` WHERE`shown`=1 and `name` LIKE '%".$_GET['term']."%'))",'pos');
			if(count($ajax_districts)>0){
				foreach($ajax_districts as $district){
					$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>$district['id'],'district_name'=>$district['name'],'city_id'=>0,'city_name'=>'');
					$ajax_cities=get_from_base('*','cities',"`parent`=".$district['id']." and `shown`=1 and `name` LIKE '%".$_GET['term']."%'",'pos');
					if(count($ajax_cities)>0){
						foreach($ajax_cities as $city){
							$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>$district['id'],'district_name'=>$district['name'],'city_id'=>$city['id'],'city_name'=>$city['name']);
						}
					}
				}
			}
		}
	}
	header('Content-Type: application/json');
	echo json_encode($places);
	exit();
}

https://jqueryui.com/autocomplete/#custom-data

Categories: Javascript, PHP Tags: , ,
  1. Пока что нет комментариев.
Похожие публикации