-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
186 lines (169 loc) · 7.1 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
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
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="apple-touch-icon" href="./icon.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Dynamic select</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/fontawesome.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/solid.min.css"/>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="index.css">
</head>
<body class="container pt-3">
<div class="row justify-content-center">
<form method="post" class="col-md-6" novalidate>
<div class="mb-3">
<label for="ddSel1" class="form-label">
Single select element transformed to Dropdown Select (required, invalidates on submit if empty)
</label>
<select id="ddSel1" name="users_1" class="form-select" required>
<option value="" disabled selected>Choose...</optiohn>
<option value="user1">User 1</option>
<optgroup label="Users">
<option value="user2">User 2</option>
</optgroup>
<optgroup label="terminated">
<option value="user3">User 3</option>
<option value="user4">User 4</option>
<option value="user5">User 5</option>
<option value="user6">User 6</option>
<option value="user7">User 7</option>
<option value="user8">User 8</option>
<option value="user9">User 9</option>
<option value="user10">User 10</option>
<option disabled value="user11">User 11</option>
<option value="user12">User 12</option>
</optgroup>
</select>
<div class="invalid-feedback">
Please choose a user.
</div>
</div>
<div class="mb-3">
<label for="ddSel2" class="form-label">
Multi-Select element transformed to Dropdown Select (required, invalidates on submit if empty)
</label>
<select id="ddSel2" name="users_1" class="form-select" multiple required>
<option value="user1">User 1</option>
<optgroup label="Users">
<option value="user2">User 2</option>
</optgroup>
<optgroup label="terminated">
<option value="user3">User 3</option>
<option value="user4">User 4</option>
<option value="user5">User 5</option>
<option value="user6">User 6</option>
<option value="user7">User 7</option>
<option value="user8">User 8</option>
<option value="user9">User 9</option>
<option value="user10">User 10</option>
<option disabled value="user11">User 11</option>
<option value="user12">User 12</option>
</optgroup>
</select>
</div>
<div class="mb-3">
<label for="ddSel3" class="form-label">Ajax searchable Dropdown Select</label>
<select id="ddSel3" name="users_2">
<option value="3">Brittany Bradford</option>
</select>
</div>
<div class="d-flex justify-content-center mb-3 gap-3">
<button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#exampleModal">Open Modal</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="ddSel4" class="form-label">Select in Modal</label>
<select id="ddSel4">
<option value="1">Brittany Bradford</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script src="bootstrapDropdownSelect.js"></script>
<script>
/**
*
* @type {BootstrapDropdownSelectOptions}
*/
const options = {
placeholder: 'Select User',
url: 'https://api.slingacademy.com/v1/sample-data/users',
queryParams: (searchQuery, previousParams, previousRequestData) => {
const limit = 20;
let params = previousParams ? previousParams : new URLSearchParams();
// add limit to previous offset or set to 0 if no previousParams
let offset;
if (!previousParams || searchQuery !== previousParams.get('search')) {
// set offset to 0 if there was no previous request or if there's a new search query
offset = 0;
} else {
// otherwise, add limit to the previous offset to get this offset
offset = parseInt(params.get('offset')) + limit;
}
params.set('offset', offset);
params.set('limit', limit);
if (!searchQuery || searchQuery.trim().length === 0) {
params.delete('search');
} else {
params.set('search', searchQuery);
}
return params;
},
processData: (data, reqParams) => {
const processed = data.users.map(u => {
return {
value: `${u.id}`,
label: `${u.first_name} ${u.last_name}`,
htmlLabel: `<div class="fw-bold">${u.first_name} ${u.last_name}</div><div class="text-secondary">${u.city}, ${u.country}</div>`
};
});
return {
data: processed,
hasMore: data.offset < 50,
// hasMore: data.offset + data.limit < data.total_users,
optGroup: `Page ${data.offset / data.limit + 1}`
};
}
};
const dropdown1 = new BootstrapDropdownSelect(document.getElementById('ddSel1'), {placeholder: 'Search User'});
const dropdown2 = new BootstrapDropdownSelect(document.getElementById('ddSel2'), {placeholder: 'Search User'});
const dropdown3 = new BootstrapDropdownSelect(document.getElementById('ddSel3'), options);
const dropdown4 = new BootstrapDropdownSelect(document.getElementById('ddSel4'), {placeholder: 'Search User'});
// demonstrate form validation with demo code from Bootstrap documentation
const form = document.querySelector('form');
form.addEventListener('submit', function (e) {
e.preventDefault();
e.stopPropagation();
form.classList.add('was-validated');
});
</script>
</body>
</html>