forked from PhilippeMarcMeyer/vanillaSelectBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajaxDemo.html
239 lines (208 loc) · 6.42 KB
/
ajaxDemo.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Demo</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./vanillaSelectBox.css">
<!-- internal css -->
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 0;
}
.darkRed {
color: darkred;
}
.darkBlue {
color: darkblue;
}
.navbar {
position: relative;
border: 1px solid #080808;
background-color: #222;
background-image: linear-gradient(to bottom,#3c3c3c 0,#222 100%);
background-repeat: repeat-x;
border-radius: 4px;
margin: 0;
padding: 0;
}
.navbar-brand {
float: left;
padding: 14px 15px;
font-size: 18px;
line-height: 20px;
color: #fff;
cursor: pointer;
text-decoration: none;
}
a:hover {
color: #fff;
}
.navbar-links {
font-size: 14px;
line-height: 20px;
position: relative;
margin: 0;
}
.navbar-links li {
cursor: pointer;
text-decoration: none;
}
.navbar-links li {
display: inline-block;
min-width: 120px;
text-align: center;
margin-top: 15px;
}
.navbar-links li.active {
background-image: #080808;
background: #080808;
color: #fff;
height: 100%;
padding-top: 15px;
padding-bottom: 15px;
margin: 0;
}
a {
color: #9d9d9d;
cursor: pointer;
text-decoration: none;
}
@media (max-width: 640px) {
.navbar-brand {
width: 100%;
}
.navbar-links {
padding-left: 0;
}
button.demo {
margin-right: 100% !important;
}
}
#container {
padding-top: 30px;
padding-left: 50px;
width: 90%;
font-size: 14px;
}
.demo-zone {
display: block;
width: 100%;
min-height: 200px;
}
.mini-demo-zone {
display: block;
width: 100%;
min-height: 100px;
}
.btns-active, .btns-inactive {
margin-top: 20px;
}
label {
display: inline-block;
min-width: 220px;
width: 220px;
}
button.demo {
margin-right: 0;
margin-left: 4px;
text-align: center;
white-space: nowrap;
vertical-align: middle;
user-select: none;
border-radius: 4px;
z-index: 1;
color: #333;
background: white;
background-image: linear-gradient(to bottom,#fff 0,#e0e0e0 100%);
border: 1px solid #ccc !important;
color: #333;
line-height: 20px;
font-size: 14px;
padding: 4px 12px;
margin-bottom: 2px;
}
button.demo:hover {
background-image: linear-gradient(to top,#fff 0,#e0e0e0 100%);
}
.presentation {
background: #eee;
color: #2e44b9;
border: 1px solid grey;
border-radius: 8px;
line-height: 20px;
padding: 8px;
margin-bottom: 40px;
width: 90%;
max-width: 800px;
font-size: 14px;
}
</style>
</head>
<body>
<nav class="navbar">
<div class="navbar-header">
<a class="navbar-brand" href="#">
Experiments with vanilla js
</a>
</div>
<ul class="navbar-links">
<li id="vanillaSelectBox" class="active"><a href="#vanillaSelectBox">vanillaSelectBox Ajax demo</a></li>
<li id="blog"><a href="https://philippemarcmeyer.github.io#vanillajs">Github Blog</a></li>
<li id="repos"><a href="https://github.com/PhilippeMarcMeyer/vanillaSelectBox">Github repos</a></li>
</ul>
</nav>
<div id="container">
<p class="presentation">
<b>vanillaSelectBox()</b> <br/>
Special demo for rvpatel : gets data from ajax call, build a select tage in the DOM, then uses vanillaSelectBox
</p>
<div class="demo-zone" style="text-align:left;" id="demo-ajax">
</div>
<script src="./vanillaSelectBox.js"></script>
<script>
httpRequest = new XMLHttpRequest();
let peopleVSB = null;
if (httpRequest) {
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
let response = null;
try{
response = JSON.parse(httpRequest.responseText);
}catch(error){
console.error(error);
}
if(response && response.data){
buildSelect(response.data,"demo-ajax","demo-ajax-select");
peopleVSB = new vanillaSelectBox("#demo-ajax-select",{ "maxHeight": 200, "search": true,translations: { "all": "All", "items": "ppl" } });
}
} else {
alert('There was a problem with the request.');
}
}
};
httpRequest.open('GET', 'https://dummy.restapiexample.com/api/v1/employees');
httpRequest.send();
}
function buildSelect(data,divId,SelectId){
let anOption;
let div = document.getElementById(divId);
div.innerHTML="";
let elem = document.createElement("select");
elem.setAttribute('id',SelectId);
elem.setAttribute('multiple','true');
elem.setAttribute('size','1');
div.appendChild(elem);
data.forEach(function(x){
anOption = document.createElement("option");
anOption.value = x.id;
anOption.text = x.employee_name;
elem.appendChild(anOption);
});
}
</script>
</html>