-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathph-address-selector.js
194 lines (166 loc) · 6.63 KB
/
ph-address-selector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* __________________________________________________________________
*
* Phillipine Address Selector
* __________________________________________________________________
*
* MIT License
*
* Copyright (c) 2020 Wilfred V. Pine
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package Phillipine Address Selector
* @author Wilfred V. Pine <only.master.red@gmail.com>
* @copyright Copyright 2020 (https://dev.confired.com)
* @link https://github.com/redmalmon/philippine-address-selector
* @license https://opensource.org/licenses/MIT MIT License
*/
var my_handlers = {
// fill province
fill_provinces: function() {
//selected region
var region_code = $(this).val();
// set selected text to input
var region_text = $(this).find("option:selected").text();
let region_input = $('#region-text');
region_input.val(region_text);
//clear province & city & barangay input
$('#province-text').val('');
$('#city-text').val('');
$('#barangay-text').val('');
//province
let dropdown = $('#province');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose State/Province</option>');
dropdown.prop('selectedIndex', 0);
//city
let city = $('#city');
city.empty();
city.append('<option selected="true" disabled></option>');
city.prop('selectedIndex', 0);
//barangay
let barangay = $('#barangay');
barangay.empty();
barangay.append('<option selected="true" disabled></option>');
barangay.prop('selectedIndex', 0);
// filter & fill
var url = 'ph-json/province.json';
$.getJSON(url, function(data) {
var result = data.filter(function(value) {
return value.region_code == region_code;
});
result.sort(function(a, b) {
return a.province_name.localeCompare(b.province_name);
});
$.each(result, function(key, entry) {
dropdown.append($('<option></option>').attr('value', entry.province_code).text(entry.province_name));
})
});
},
// fill city
fill_cities: function() {
//selected province
var province_code = $(this).val();
// set selected text to input
var province_text = $(this).find("option:selected").text();
let province_input = $('#province-text');
province_input.val(province_text);
//clear city & barangay input
$('#city-text').val('');
$('#barangay-text').val('');
//city
let dropdown = $('#city');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose city/municipality</option>');
dropdown.prop('selectedIndex', 0);
//barangay
let barangay = $('#barangay');
barangay.empty();
barangay.append('<option selected="true" disabled></option>');
barangay.prop('selectedIndex', 0);
// filter & fill
var url = 'ph-json/city.json';
$.getJSON(url, function(data) {
var result = data.filter(function(value) {
return value.province_code == province_code;
});
result.sort(function(a, b) {
return a.city_name.localeCompare(b.city_name);
});
$.each(result, function(key, entry) {
dropdown.append($('<option></option>').attr('value', entry.city_code).text(entry.city_name));
})
});
},
// fill barangay
fill_barangays: function() {
// selected barangay
var city_code = $(this).val();
// set selected text to input
var city_text = $(this).find("option:selected").text();
let city_input = $('#city-text');
city_input.val(city_text);
//clear barangay input
$('#barangay-text').val('');
// barangay
let dropdown = $('#barangay');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose barangay</option>');
dropdown.prop('selectedIndex', 0);
// filter & Fill
var url = 'ph-json/barangay.json';
$.getJSON(url, function(data) {
var result = data.filter(function(value) {
return value.city_code == city_code;
});
result.sort(function(a, b) {
return a.brgy_name.localeCompare(b.brgy_name);
});
$.each(result, function(key, entry) {
dropdown.append($('<option></option>').attr('value', entry.brgy_code).text(entry.brgy_name));
})
});
},
onchange_barangay: function() {
// set selected text to input
var barangay_text = $(this).find("option:selected").text();
let barangay_input = $('#barangay-text');
barangay_input.val(barangay_text);
},
};
$(function() {
// events
$('#region').on('change', my_handlers.fill_provinces);
$('#province').on('change', my_handlers.fill_cities);
$('#city').on('change', my_handlers.fill_barangays);
$('#barangay').on('change', my_handlers.onchange_barangay);
// load region
let dropdown = $('#region');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose Region</option>');
dropdown.prop('selectedIndex', 0);
const url = 'ph-json/region.json';
// Populate dropdown with list of regions
$.getJSON(url, function(data) {
$.each(data, function(key, entry) {
dropdown.append($('<option></option>').attr('value', entry.region_code).text(entry.region_name));
})
});
});