Commit a1f02e706f0be8807dad8aae6ef18e2f1d9e22d3

Authored by Daniela Feitosa
1 parent 03254351

Autocomplete fields on page load

(ActionItem3003)
Showing 1 changed file with 52 additions and 47 deletions   Show diff stats
public/javascripts/city_state_validation.js
1 1 (function($){
  2 + autoCompleteStateCity($);
2 3 $('[id$="_country"]').change(function(){
3   - var country_selected = $('[id$="_country"] option:selected').val()
4   - if(country_selected == "BR")
5   - {
6   - $('#state_field').autocomplete({
7   - source : function(request, response){
8   - $.ajax({
9   - type: "GET",
10   - url: '/account/search_state',
11   - data: {state_name: request.term},
12   - success: function(result){
13   - response(result);
14   - },
15   - error: function(ajax, stat, errorThrown) {
16   - console.log('Link not found : ' + errorThrown);
17   - }
18   - });
19   - },
  4 + autoCompleteStateCity($);
  5 + })
  6 +})(jQuery);
  7 +
  8 +function autoCompleteStateCity($) {
  9 + var country_selected = $('[id$="_country"] option:selected').val()
  10 + if(country_selected == "BR")
  11 + {
  12 + $('#state_field').autocomplete({
  13 + source : function(request, response){
  14 + $.ajax({
  15 + type: "GET",
  16 + url: '/account/search_state',
  17 + data: {state_name: request.term},
  18 + success: function(result){
  19 + response(result);
  20 + },
  21 + error: function(ajax, stat, errorThrown) {
  22 + console.log('Link not found : ' + errorThrown);
  23 + }
  24 + });
  25 + },
20 26  
21   - minLength: 3
22   - });
  27 + minLength: 3
  28 + });
23 29  
24   - $('#city_field').autocomplete({
25   - source : function(request, response){
26   - $.ajax({
27   - type: "GET",
28   - url: '/account/search_cities',
29   - data: {city_name: request.term, state_name: $("#state_field").val()},
30   - success: function(result){
31   - response(result);
32   - },
33   - error: function(ajax, stat, errorThrown) {
34   - console.log('Link not found : ' + errorThrown);
35   - }
36   - });
37   - },
  30 + $('#city_field').autocomplete({
  31 + source : function(request, response){
  32 + $.ajax({
  33 + type: "GET",
  34 + url: '/account/search_cities',
  35 + data: {city_name: request.term, state_name: $("#state_field").val()},
  36 + success: function(result){
  37 + response(result);
  38 + },
  39 + error: function(ajax, stat, errorThrown) {
  40 + console.log('Link not found : ' + errorThrown);
  41 + }
  42 + });
  43 + },
38 44  
39   - minLength: 3
40   - });
  45 + minLength: 3
  46 + });
  47 + }
  48 + else
  49 + {
  50 + if ($('#state_field').data('autocomplete')) {
  51 + $('#state_field').autocomplete("destroy");
  52 + $('#state_field').removeData('autocomplete');
41 53 }
42   - else
43   - {
44   - if ($('#state_field').data('autocomplete')) {
45   - $('#state_field').autocomplete("destroy");
46   - $('#state_field').removeData('autocomplete');
47   - }
48 54  
49   - if ($('#city_field').data('autocomplete')) {
50   - $('#city_field').autocomplete("destroy");
51   - $('#city_field').removeData('autocomplete');
52   - }
  55 + if ($('#city_field').data('autocomplete')) {
  56 + $('#city_field').autocomplete("destroy");
  57 + $('#city_field').removeData('autocomplete');
53 58 }
54   - })
55   -})(jQuery);
  59 + }
  60 +}
... ...