Skip to content

Commit d8b8405

Browse files
committed
improve demo page scripts
1 parent 03759a1 commit d8b8405

File tree

1 file changed

+60
-61
lines changed

1 file changed

+60
-61
lines changed

demo/index.php

+60-61
Original file line numberDiff line numberDiff line change
@@ -71,81 +71,80 @@
7171
<script src="./assets/js/bootstrap.min.js"></script>
7272
7373
<!-- Current doc JS -->
74+
7475
<script type="text/javascript">
75-
function reloadProvinces(lang)
76-
{
77-
$('.province').find("option:gt(0)").remove();
78-
$('.district').find("option:gt(0)").remove();
79-
$('.municipality').find("option:gt(0)").remove();
80-
$('.ward').find("option:gt(0)").remove();
81-
82-
$.ajax({
83-
url: 'api/provinces.php?lang='+lang,
84-
type: 'GET',
85-
dataType: 'json',
86-
}).done(function (data) {
87-
var select = $('.province');
76+
$(document).ready(function () {
77+
// Utility function to clear dependent dropdowns
78+
function clearDropdowns(...selectors) {
79+
selectors.forEach(selector => $(selector).find("option:gt(0)").remove());
80+
}
81+
82+
// Utility function to populate a dropdown
83+
function populateDropdown(selector, data) {
84+
const select = $(selector);
8885
$.each(data, function (key, value) {
89-
select.append('<option value=' + key + '>' + value + '</option>');
86+
select.append(`<option value="${key}">${value}</option>`);
9087
});
91-
});
92-
}
93-
$(document).ready(function (){
94-
var lang = $('.language').val();
95-
reloadProvinces(lang);
96-
$('.language').on('change', function (){
97-
lang = $('.language').val();
98-
reloadProvinces(lang);
99-
});
88+
}
89+
90+
// Function to fetch data and populate a dropdown
91+
function fetchAndPopulate(url, targetSelector, dependentSelectors = []) {
92+
clearDropdowns(...dependentSelectors);
10093
101-
$('.province').on('change', function () {
102-
$('.district').find("option:gt(0)").remove();
103-
$('.municipality').find("option:gt(0)").remove();
104-
$('.ward').find("option:gt(0)").remove();
105-
var selected = $(this).find(":selected").attr('value');
10694
$.ajax({
107-
url: 'api/districts.php?lang='+lang+'&&province_id=' + selected,
95+
url,
10896
type: 'GET',
10997
dataType: 'json',
110-
}).done(function (data) {
111-
var select = $('.district');
112-
$.each(data, function (key, value) {
113-
select.append('<option value=' + key + '>' + value + '</option>');
114-
});
11598
})
99+
.done(data => populateDropdown(targetSelector, data))
100+
.fail(() => console.error(`Failed to fetch data from ${url}`));
101+
}
102+
103+
// Reload provinces based on the selected language
104+
function reloadProvinces(lang) {
105+
const url = `api/provinces.php?lang=${lang}`;
106+
fetchAndPopulate(url, '.province', ['.district', '.municipality', '.ward']);
107+
}
108+
109+
// Event handlers
110+
const langSelector = '.language';
111+
const provinceSelector = '.province';
112+
const districtSelector = '.district';
113+
const municipalitySelector = '.municipality';
114+
const wardSelector = '.ward';
115+
116+
$(langSelector).on('change', function () {
117+
const lang = $(langSelector).val();
118+
reloadProvinces(lang);
116119
});
117120
118-
$('.district').on('change', function () {
119-
$('.municipality').find("option:gt(0)").remove();
120-
$('.ward').find("option:gt(0)").remove();
121-
var selected = $(this).find(":selected").attr('value');
122-
$.ajax({
123-
url: 'api/municipalities.php?lang='+lang+'&&district_id=' + selected,
124-
type: 'GET',
125-
dataType: 'json',
126-
}).done(function (data) {
127-
var select = $('.municipality');
128-
$.each(data, function (key, value) {
129-
select.append('<option value=' + key + '>' + value + '</option>');
130-
});
131-
})
121+
$(provinceSelector).on('change', function () {
122+
const lang = $(langSelector).val();
123+
const provinceId = $(this).val();
124+
const url = `api/districts.php?lang=${lang}&province_id=${provinceId}`;
125+
fetchAndPopulate(url, districtSelector, [districtSelector, municipalitySelector, wardSelector]);
132126
});
133127
134-
$('.municipality').on('change', function () {
135-
var selected = $(this).find(":selected").attr('value');
136-
$('.ward').find("option:gt(0)").remove();
137-
$.ajax({
138-
url: 'api/wards.php?lang='+lang+'&&municipality_id=' + selected,
139-
type: 'GET',
140-
dataType: 'json',
141-
}).done(function (data) {
142-
var select = $('.ward');
143-
$.each(data, function (key, value) {
144-
select.append('<option value=' + key + '>' + value + '</option>');
145-
});
146-
})
128+
$(districtSelector).on('change', function () {
129+
const lang = $(langSelector).val();
130+
const districtId = $(this).val();
131+
const url = `api/municipalities.php?lang=${lang}&district_id=${districtId}`;
132+
fetchAndPopulate(url, municipalitySelector, [municipalitySelector, wardSelector]);
147133
});
134+
135+
$(municipalitySelector).on('change', function () {
136+
const lang = $(langSelector).val();
137+
const municipalityId = $(this).val();
138+
const url = `api/wards.php?lang=${lang}&municipality_id=${municipalityId}`;
139+
fetchAndPopulate(url, wardSelector, [wardSelector]);
140+
});
141+
142+
// Initial setup
143+
const initialLang = $(langSelector).val();
144+
reloadProvinces(initialLang);
148145
});
149146
</script>
147+
148+
150149
</body>
151150
</html>

0 commit comments

Comments
 (0)