-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.html
106 lines (103 loc) · 2.97 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CountryCity by shivammathur</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/css/select2.min.css" rel="stylesheet" />
</head>
<body>
<header class="text-center">
<h1 class="pt-4 mt-4 pb-4 mb-4">CountryCity</h1>
</header>
<section>
<div class="container">
<div class="row">
<div class="col-lg-2"></div>
<div class="col-lg-4">
<label for="countries"><strong>Countries</strong></label>
<select class="pt-4 pb-4 countries js-example-basic-single js-states form-control" id="countries" name="countries">
<option value="">Select One</option>
</select>
</div>
<div class="col-lg-4">
<label for="cities"><strong>Cities</strong></label>
<select class="pt-4 pb-4 cities js-example-basic-single js-states form-control" id="cities" name="cities">
<option value="">Select One</option>
</select>
</div>
<div class="col-lg-2"></div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.4.0/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.min.js"></script>
<script type="text/javascript">
const base= "https://shivammathur.com/countrycity";
const country_el = $('#countries');
/**
* Function to map the API data in select format
*
* @param data
* @returns array
*/
const mapper = (data) => {
return data.map((item) => {
return {
id: item,
text: item
};
});
};
// Initialise countries select dropdown.
country_el.select2({
placeholder: "Choose a country",
ajax: {
dataType: 'json',
url: (params) => {
if(params.term) {
return base + "/countries/" + params.term;
} else {
return base + "/countries";
}
},
processResults: (data) => {
return {
"results": mapper(data)
};
}
}
});
// When a country is selected update cities select dropdown.
country_el.change(() => {
const country = country_el.val();
$('#cities option').not(':first').remove();
$('#cities').select2({
placeholder: "Choose a city in " + country,
ajax: {
dataType: 'json',
url: (params) => {
if (params.term) {
return base + "/cities/" + country + "/" + params.term;
} else {
return base + "/cities/" + country;
}
},
processResults: (data) => {
return {
"results": mapper(data)
};
}
}
});
});
// Initialise cities select dropdown.
$("#cities").select2({
placeholder: "Choose a country first",
data: null,
});
</script>
</body>
</html>