forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathselect.html
206 lines (190 loc) · 8.37 KB
/
select.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<title>Documentation - Materialize</title>
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed" href="../../docs/images/favicon/apple-touch-icon-152x152.png">
<meta name="msapplication-TileColor" content="#FFFFFF">
<meta name="msapplication-TileImage" content="images/favicon/mstile-144x144.png">
<link rel="icon" href="../../docs/images/favicon/favicon-32x32.png" sizes="32x32">
<!-- Android 5 Chrome Color -->
<meta name="theme-color" content="#EE6E73">
<link href="../../dist/css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection">
<style>
body {
padding: 16px;
}
.input-field{
padding-bottom: 2em;
}
</style>
</head>
<body>
<div class="row">
<div class="input-field col s12 m6">
<select id="select-test-1">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label for="select-test-1">Single-Select</label>
<div>
<button class="btn1">Get Single-Select Values</button>
</div>
</div>
<div class="input-field col s12 m6">
<select id="select-test-2" multiple>
<option value="" disabled>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label for="select-test-2">Multi-Select</label>
<div>
<button class="btn2">Get Multi-Select Values</button>
</div>
</div>
</div>
<div class="input-field col s12">
<select id="select-test-3">
<optgroup label="team 1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</optgroup>
<optgroup label="team 2">
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</optgroup>
</select>
<label for="select-test-3">Single-Select with Optgroups</label>
</div>
<div class="row">
<h5>Selects with Images</h5>
<div class="input-field col s12 m6">
<select id="select-test-4" class="icons">
<option value="" disabled selected>Choose your option</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_a.png">example 1</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_b.png">example 2</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_c.png">example 3</option>
</select>
<label for="select-test-4">Single-Select with Images (right)</label>
</div>
<div class="input-field col s12 m6">
<select id="select-test-5" class="icons">
<option value="" disabled selected>Choose your option</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_a.png" class="left">example 1</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_b.png" class="left">example 2</option>
<option value="" data-icon="../../docs/images/placeholder/250x250_c.png" class="left">example 3</option>
</select>
<label for="select-test-5">Single-Select with Images (left)</label>
</div>
</div>
<div class="row">
<h5>Select with selected Options</h5>
<div class="input-field col s12 m6">
<select id="select-test-6">
<option value="" disabled>Choose your option</option>
<option value="1">Option 1</option>
<option value="2" selected>Option 2</option>
<option value="3">Option 3</option>
</select>
<label for="select-test-6">Single-Select with Value set</label>
</div>
<div class="input-field col s12 m6">
<select id="select-test-7" multiple class="icons">
<option value="" disabled selected>Choose your option</option>
<option value="1" selected data-icon="../../docs/images/placeholder/250x250_a.png">example 1</option>
<option value="2" selected data-icon="../../docs/images/placeholder/250x250_b.png">example 2</option>
<option value="3" disabled data-icon="../../docs/images/placeholder/250x250_c.png">example 3</option>
<option value="4" selected disabled data-icon="../../docs/images/placeholder/250x250_d.png">example 3</option>
<option value="5" data-icon="../../docs/images/placeholder/250x250_e.png">example 4</option>
<option value="6">example 5</option>
</select>
<label for="select-test-7">Multi-Select with Images (right) and Pre-Selections</label>
</div>
</div>
<div class="row">
<h5>Dynamically generated Select-Options <button class="btn3">Create + Init Select</button></h5>
<div class="input-field col s12 m6">
<select id="select-test-8" class="dynamic-select-single"></select>
<label for="select-test-8">Dynamic Single-Select</label>
</div>
<div class="input-field col s12 m6">
<select id="select-test-9" class="dynamic-select-multi" multiple></select>
<label for="select-test-9">Dynamic Multi-Select</label>
</div>
</div>
<div class="row">
<div class="col s12 m6">
<label for="select-test-10">Browser Single-Select</label>
<select id="select-test-10" class="browser-default">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
<div class="col s12 m6">
<label for="select-test-11">Browser Multi-Select</label>
<select id="select-test-11" class="browser-default" multiple style="height:150px;">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
</div>
<!-- Scripts -->
<script src="../../dist/js/materialize.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
elems[0].addEventListener('change', e => console.log("First Select changed!"));
elems[1].addEventListener('change', e => console.log("Second Select changed!"));
var instances = M.FormSelect.init(elems, {
// specify options here
});
// Methods
document.querySelector('.btn1').addEventListener('click', e => {
alert(instances[0].getSelectedValues());
});
document.querySelector('.btn2').addEventListener('click', e => {
alert(instances[1].getSelectedValues());
});
document.querySelector('.btn3').addEventListener('click', e => {
const optionIndexes = [1,2,3,4,5,6];
// Single-Select
const singleSelect = document.querySelector('.dynamic-select-single');
singleSelect.innerHTML = '';
optionIndexes.map(index => {
const option = document.createElement('option');
option.value = index;
option.innerText = 'option '+index;
const isSelected = index === 3;
// Set via Attribute
if (isSelected)
option.setAttribute('selected', 'selected');
singleSelect.appendChild(option);
});
// Multi-Select
const multiSelect = document.querySelector('.dynamic-select-multi');
multiSelect.innerHTML = '';
optionIndexes.map(index => {
const option = document.createElement('option');
option.value = index;
option.innerText = 'option '+index;
// Set directly (all even options)
option.selected = index % 2 === 0;
multiSelect.appendChild(option);
});
// Init both
M.FormSelect.init(singleSelect, {});
M.FormSelect.init(multiSelect, {});
});
});
</script>
</body>
</html>