From f3de0545f38385a3861e68163a6b4a560ecdc997 Mon Sep 17 00:00:00 2001 From: Damiano Ciarla Date: Thu, 17 Apr 2014 12:32:12 +0200 Subject: [PATCH] Added callback after ajax request --- .gitignore | 2 ++ Form/Type/SelectCityFormType.php | 13 +++++++++++++ Resources/public/js/select_city.js | 20 ++++++++++++++++---- Resources/views/Form/select_city.html.twig | 18 ++++++++++-------- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index b62fece..03a2f6c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ vendor/* web/uploads/* web/bundles/* +.idea + # Configuration files app/config/parameters.ini app/config/parameters.yml diff --git a/Form/Type/SelectCityFormType.php b/Form/Type/SelectCityFormType.php index c5f5246..fee7ccb 100755 --- a/Form/Type/SelectCityFormType.php +++ b/Form/Type/SelectCityFormType.php @@ -8,6 +8,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormView; use DCS\Form\SelectCityFormFieldBundle\Model; class SelectCityFormType extends AbstractType @@ -37,6 +38,14 @@ public function __construct( $this->cityManager = $cityManager; } + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars = array_merge($view->vars, array( + 'callback_country' => $options['callback_country'], + 'callback_region' => $options['callback_region'], + )); + } + public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -135,12 +144,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) 'country_required' => true, 'region_required' => true, 'city_required' => true, + 'callback_country' => 'callbackCountry', + 'callback_region' => 'callbackRegion', )); $resolver->setAllowedTypes(array( 'country_required' => 'bool', 'region_required' => 'bool', 'city_required' => 'bool', + 'callback_country' => 'string', + 'callback_region' => 'string', )); } diff --git a/Resources/public/js/select_city.js b/Resources/public/js/select_city.js index 2fabb79..5ecf978 100755 --- a/Resources/public/js/select_city.js +++ b/Resources/public/js/select_city.js @@ -1,12 +1,12 @@ window.select_region_base_url = null; window.select_city_base_url = null; window.select_city_instance = []; -window.select_city = function (countryId, regionId, cityId) { +window.select_city = function (countryId, regionId, cityId, callbackCountry, callbackRegion) { var countryElement = document.getElementById(countryId); var regionElement = document.getElementById(regionId); var cityElement = document.getElementById(cityId); - var loadJsonData = function (url, element, param) { + var loadJsonData = function (url, element, param, type) { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); @@ -24,6 +24,18 @@ window.select_city = function (countryId, regionId, cityId) { option.value = info.id; element.appendChild(option); } + switch (type) { + case 'country': + if (callbackCountry !== null) { + callbackCountry(data, element); + } + break; + case 'region': + if (callbackRegion !== null) { + callbackRegion(data, element); + } + break; + } } }; @@ -39,12 +51,12 @@ window.select_city = function (countryId, regionId, cityId) { var url = window.select_region_base_url.replace("countryId", countryElement.options[countryElement.selectedIndex].value); emptyOptionFromSelect(regionElement); emptyOptionFromSelect(cityElement); - loadJsonData(url, regionElement, 'regionName'); + loadJsonData(url, regionElement, 'regionName', 'country'); }; regionElement.onchange = function () { var url = window.select_city_base_url.replace("regionId", regionElement.options[regionElement.selectedIndex].value); emptyOptionFromSelect(cityElement); - loadJsonData(url, cityElement, 'cityName'); + loadJsonData(url, cityElement, 'cityName', 'region'); }; }; \ No newline at end of file diff --git a/Resources/views/Form/select_city.html.twig b/Resources/views/Form/select_city.html.twig index e47cdae..747c338 100755 --- a/Resources/views/Form/select_city.html.twig +++ b/Resources/views/Form/select_city.html.twig @@ -2,15 +2,17 @@ {% spaceless %} {{ form_widget(form) }} {% endspaceless %} {% endblock %} \ No newline at end of file