forked from handsontable/handsontable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.html
312 lines (261 loc) · 10.5 KB
/
autocomplete.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Autocomplete cell type - Handsontable</title>
<!--
Loading Handsontable (full distribution that includes all dependencies)
-->
<script data-jsfiddle="common" src="../dist/handsontable.full.js"></script>
<link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.full.css">
<!--
Loading demo dependencies. They are used here only to enhance the examples on this page
-->
<link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
<script src="js/samples.js"></script>
<script src="js/highlight/highlight.pack.js"></script>
<link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
<link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
<!--
Facebook open graph. Don't copy this to your project :)
-->
<meta property="og:title" content="Autocomplete cell type">
<meta property="og:description"
content="This example shows the usage of the Autocomplete feature.">
<meta property="og:url" content="http://handsontable.com/demo/autocomplete.html">
<meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="409">
<meta property="og:image:height" content="164">
<link rel="canonical" href="http://handsontable.com/demo/autocomplete.html">
<!--
Google Analytics for GitHub Page. Don't copy this to your project :)
-->
<script src="js/ga.js"></script>
<script data-jsfiddle="common">
function getCarData() {
return [
["Nissan", 2009, "black", "black"],
["Nissan", 2006, "blue", "blue"],
["Chrysler", 2004, "yellow", "black"],
["Volvo", 2012, "white", "gray"]
];
}
</script>
</head>
<body>
<div class="wrapper">
<div class="wrapper-row">
<div id="global-menu-clone">
<h1><a href="../index.html">Handsontable</a></h1>
</div>
<div id="container">
<div class="columnLayout">
<div class="rowLayout">
<div class="descLayout">
<div class="pad">
<h2>Autocomplete cell type</h2>
<p>This page shows how to configure Handsontable with Autocomplete cell type, which expands the
<a href="handsontable.html">Handsontable cell type.</a> with features typical for a combo box that
updates its options based on user input.</p>
<p>There are three demos on this page:</p>
<ul>
<li><a href="#lazy">Autocomplete lazy mode</a></li>
<li><a href="#strict">Autocomplete strict mode</a></li>
<li><a href="#strict-ajax">Autocomplete strict mode (Ajax)</a></li>
</ul>
</div>
</div>
</div>
<div class="rowLayout">
<div class="descLayout">
<div class="pad" data-jsfiddle="example1">
<a name="lazy"></a>
<h2>Autocomplete lazy mode</h2>
<p>This example shows the usage of the Autocomplete feature in the default <strong>lazy mode</strong>. In this mode,
user can choose one of the suggested options while typing or enter a custom value that is not included in the suggestions.</p>
<p>In this mode, the mouse and keyboard bindings are identical as in <a href="handsontable.html">Handsontable cell type.</a></p>
<p>The options are rendered from the <code>source</code> property which can be an array, or a function that returns an array.</p>
<div id="example1"></div>
<p>
<button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
data to console
</button>
</p>
</div>
</div>
<div class="codeLayout">
<div class="pad">
<div class="jsFiddle">
<button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
</div>
<script data-jsfiddle="example1">
var example1 = document.getElementById('example1');
var hot1 = new Handsontable(example1,{
data: getCarData(),
startRows: 7,
startCols: 4,
colHeaders: ["Car", "Year", "Chassis color", "Bumper color"],
columns: [
{
type: 'autocomplete',
source: ["BMW", "Chrysler", "Nissan", "Suzuki", "Toyota", "Volvo"],
strict: false
},
{type: 'numeric'},
{
type: 'autocomplete',
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white", "purple", "lime", "olive", "cyan"],
strict: false
},
{
type: 'autocomplete',
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white", "purple", "lime", "olive", "cyan"],
strict: false
}
]
});
</script>
</div>
</div>
</div>
<div class="rowLayout">
<div class="descLayout">
<div class="pad" data-jsfiddle="example2">
<a name="strict"></a>
<h2>Autocomplete strict mode</h2>
<p>This is the same example as above with a difference that autocomplete now runs in <strong>strict
mode</strong>.</p>
<p>In this mode, the autocomplete cells will only accept values that are defined in the source
array.</p>
<p>In this mode, the mouse and keyboard bindings are identical as in <a href="handsontable.html">Handsontable cell type</a> with the below differences:</p>
<ul>
<li>If there is at least one option visible, there always is a selection in HOT-in-HOT.</li>
<li>When the first row is selected, pressing <kbd>Arrow up</kbd> does not deselect HOT-in-HOT. Instead behaves as the ENTER key but moves the selection in the main HOT upwards.</li>
</ul>
<p>In strict mode, the <strong>allowInvalid</strong> option determines the behaviour in case of manual user input:</p>
<ul>
<li><code>allowInvalid: true</code> (optional) - allows manual input of value that does not exist in the
<code>source</code>. In this case, the field background highlight becomes red and the selection
advances to the next cell
</li>
<li><code>allowInvalid: false</code> - does not allow manual input of value that does not exist in the
<code>source</code>. In this case, the <kbd>ENTER</kbd> key is ignored and the editor field remains opened.</li>
</ul>
<div id="example2"></div>
<p>
<button name="dump" data-dump="#example2" data-instance="hot2" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
data to console
</button>
</p>
</div>
</div>
<div class="codeLayout">
<div class="pad">
<div class="jsFiddle">
<button class="jsFiddleLink" data-runfiddle="example2">Edit in jsFiddle</button>
</div>
<script data-jsfiddle="example2">
var container2 = document.getElementById('example2');
var hot2 = new Handsontable(container2,{
data: getCarData(),
startRows: 7,
startCols: 4,
colHeaders: ["Car<br>(allowInvalid true)", "Year", "Chassis color<br>(allowInvalid false)", "Bumper color<br>(allowInvalid true)"],
columns: [
{
type: 'autocomplete',
source: ["BMW", "Chrysler", "Nissan", "Suzuki", "Toyota", "Volvo"],
strict: true
//allowInvalid: true //true is default
},
{},
{
type: 'autocomplete',
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white", "purple", "lime", "olive", "cyan"],
strict: true,
allowInvalid: false
},
{
type: 'autocomplete',
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white", "purple", "lime", "olive", "cyan"],
strict: true,
allowInvalid: true //true is default
}
]
});
</script>
</div>
</div>
</div>
<div class="rowLayout">
<div class="descLayout">
<div class="pad">
<a name="strict-ajax"></a>
<h2>Autocomplete strict mode (Ajax)</h2>
<p>Autocomplete can be also used with Ajax data source. In the below example, suggestions for the "Car" column are
loaded from server.</p>
<p>To load data from remote (asynchronous) source, assign a function to the 'source' property. Function should
perform the server side request and call the callback function when the result is available.</p>
<div id="example3"></div>
<p>
<button name="dump" data-dump="#example3" data-instance="hot3" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
data to console
</button>
</p>
</div>
</div>
<div class="codeLayout">
<div class="pad">
<div class="jsFiddle">
<!--
Ajax example will not work with JSFiddle at the moment because `php/cars.php` does not implement CORS.
<button class="jsFiddleLink">Edit in jsFiddle</button>
-->
</div>
<script>
var container3 = document.getElementById('example3');
var hot3 = new Handsontable(container3,{
data: getCarData(),
startRows: 7,
startCols: 4,
colHeaders: ["Car", "Year", "Chassis color", "Bumper color"],
columns: [
{
type: 'autocomplete',
source: function (query, process) {
$.ajax({
//url: 'php/cars.php', //commented out because our website is hosted on static GitHub Pages
url: 'json/autocomplete.json',
dataType: 'json',
data: {
query: query
},
success: function (response) {
console.log("response", response);
//process(JSON.parse(response.data)); //JSON.parse takes string as a argument
process(response.data);
}
});
},
strict: true
},
{} /*Year is a default text column*/,
{} /*Chassis color is a default text column*/,
{} /*Bumper color is a default text column*/
]
});
</script>
</div>
</div>
</div>
<div class="footer-text">Handsontable © 2012-2014 Marcin Warpechowski and contributors.<br> Code and documentation
licensed under the The MIT License.
</div>
</div>
</div>
</div>
</div>
<div id="outside-links-wrapper"></div>
</body>
</html>