5
<title>Example: Filter a Set of Existing Items on a Page</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Filter a Set of Existing Items on a Page</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
22
This example demonstrates how to use <code>AutoCompleteBase</code> alone, without a list widget, to filter a set of existing items on a page. While this is a departure from the typical autocomplete interaction pattern, <code>AutoCompleteBase</code> is flexible enough to provide an excellent solution without any unnecessary overhead.
26
To filter the list of delicious pies, type a descriptive term into the input field below. For example, to see only apple pies, type "apple", or for all fruit pies, type "fruit".
33
border: 1px solid #cfcfcf;
40
display: inline-block;
43
/* fake inline-block for IE<8 */
48
#photos .empty { font-style: italic; }
57
border: 1px solid #000;
62
#photos .hidden { display: none; }
65
<div id="demo" class="yui3-skin-sam">
66
<label for="ac-input">Filter by:</label><br>
67
<input id="ac-input" type="text">
70
<li class="photo" data-tags="apple, fruit">
71
<a href="http://www.flickr.com/photos/wonko/5213328415/in/photostream/">
72
<img src="http://farm6.static.flickr.com/5002/5213328415_4cf3aa583f_m.jpg" alt="Apple pie">
76
<li class="photo" data-tags="pecan">
77
<a href="http://www.flickr.com/photos/wonko/5213327801/in/photostream/">
78
<img src="http://farm6.static.flickr.com/5208/5213327801_1e62145da1_m.jpg" alt="Pecan pie">
82
<li class="photo" data-tags="maple custard">
83
<a href="http://www.flickr.com/photos/wonko/5213920818/in/photostream/">
84
<img src="http://farm6.static.flickr.com/5245/5213920818_bf7cd599c3_m.jpg" alt="Maple custard pie">
88
<li class="photo" data-tags="pumpkin">
89
<a href="http://www.flickr.com/photos/wonko/5213912956/in/photostream/">
90
<img src="http://farm6.static.flickr.com/5049/5213912956_7cba11aa01_m.jpg" alt="Pumpkin pie">
94
<li class="photo" data-tags="turkey, pot pie, carrots, meat, savory">
95
<a href="http://www.flickr.com/photos/wonko/2055852065/in/photostream/">
96
<img src="http://farm3.static.flickr.com/2330/2055852065_d9a7d56650_m.jpg" alt="Turkey pot pie">
100
<li class="photo" data-tags="cherry, hearts, fruit">
101
<a href="http://www.flickr.com/photos/wonko/100174720/in/photostream/">
102
<img src="http://farm1.static.flickr.com/34/100174720_dad453636d_m.jpg" alt="Cherry pie with two hearts on top">
106
<li class="photo" data-tags="lattice, sour cherry, fruit">
107
<a href="http://www.flickr.com/photos/faerye/4839675135/in/photostream/">
108
<img src="http://farm5.static.flickr.com/4083/4839675135_fb5e88da3d_m.jpg" alt="Lattice-top sour cherry pie">
112
<li class="photo" data-tags="chocolate, cream, cinnamon, meringue, christmas, tree">
113
<a href="http://www.flickr.com/photos/faerye/4283875981/">
114
<img src="http://farm5.static.flickr.com/4029/4283875981_faaccb9089_m.jpg" alt="Chocolate cream pie with cinnamon meringue">
118
<li class="photo" data-tags="strawberry, chiffon, chocolate, wafer, fruit">
119
<a href="http://www.flickr.com/photos/faerye/4592482821/">
120
<img src="http://farm2.static.flickr.com/1070/4592482821_925ba97e60_m.jpg" alt="Strawberry chiffon pie with chocolate wafer crust">
124
<li class="photo" data-tags="key lime, whipped cream, graham cracker, slice, fruit">
125
<a href="http://www.flickr.com/photos/faerye/3492566166/">
126
<img src="http://farm4.static.flickr.com/3413/3492566166_0fd62e0d43_m.jpg" alt="Key lime pie with graham cracker crust">
130
<li class="photo" data-tags="lattice top, apple, fruit">
131
<a href="http://www.flickr.com/photos/faerye/3394679580/">
132
<img src="http://farm4.static.flickr.com/3569/3394679580_8b08c46fe6_m.jpg" alt="Lattice top apple pie">
139
YUI().use('autocomplete-base', 'autocomplete-filters', function (Y) {
140
// Create a custom PieFilter class that extends AutoCompleteBase.
141
var PieFilter = Y.Base.create('pieFilter', Y.Base, [Y.AutoCompleteBase], {
142
initializer: function () {
143
this._bindUIACBase();
144
this._syncUIACBase();
148
// Create and configure an instance of the PieFilter class.
149
filter = new PieFilter({
150
inputNode: '#ac-input',
154
// Run an immediately-invoked function that returns an array of results to
155
// be used for each query, based on the photos on the page. Since the list
156
// of photos remains static, this saves time by not gathering the results
159
// If the list of results were not static, we could simply set the source
160
// to the function itself rather than invoking the function immediately,
161
// and it would then run on every query.
162
source: (function () {
165
// Build an array of results containing each photo in the list.
166
Y.all('#photos > .photo').each(function (node) {
169
tags: node.getAttribute('data-tags')
174
}()), // <-- Note the parens. This invokes the function immediately.
175
// Remove these to invoke the function on every query instead.
177
// Specify that the "tags" property of each result object contains the text
179
resultTextLocator: 'tags',
181
// Use a result filter to filter the photo results based on their tags.
182
resultFilters: 'phraseMatch'
185
// Subscribe to the "results" event and update photo visibility based on
186
// whether or not they were included in the list of results.
187
filter.on('results', function (e) {
188
// First hide all the photos.
189
Y.all('#photos > .photo').addClass('hidden');
191
// Then unhide the ones that are in the current result list.
192
Y.Array.each(e.results, function (result) {
193
result.raw.node.removeClass('hidden');
203
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam">
204
<label for="ac-input">Filter by:</label><br>
205
<input id="ac-input" type="text">
207
<ul id="photos">
208
<li class="photo" data-tags="apple, fruit">
209
<a href="http://www.flickr.com/photos/wonko/5213328415/in/photostream/">
210
<img src="http://farm6.static.flickr.com/5002/5213328415_4cf3aa583f_m.jpg" alt="Apple pie">
214
<li class="photo" data-tags="pecan">
215
<a href="http://www.flickr.com/photos/wonko/5213327801/in/photostream/">
216
<img src="http://farm6.static.flickr.com/5208/5213327801_1e62145da1_m.jpg" alt="Pecan pie">
220
<li class="photo" data-tags="maple custard">
221
<a href="http://www.flickr.com/photos/wonko/5213920818/in/photostream/">
222
<img src="http://farm6.static.flickr.com/5245/5213920818_bf7cd599c3_m.jpg" alt="Maple custard pie">
226
<li class="photo" data-tags="pumpkin">
227
<a href="http://www.flickr.com/photos/wonko/5213912956/in/photostream/">
228
<img src="http://farm6.static.flickr.com/5049/5213912956_7cba11aa01_m.jpg" alt="Pumpkin pie">
232
<li class="photo" data-tags="turkey, pot pie, carrots, meat, savory">
233
<a href="http://www.flickr.com/photos/wonko/2055852065/in/photostream/">
234
<img src="http://farm3.static.flickr.com/2330/2055852065_d9a7d56650_m.jpg" alt="Turkey pot pie">
238
<li class="photo" data-tags="cherry, hearts, fruit">
239
<a href="http://www.flickr.com/photos/wonko/100174720/in/photostream/">
240
<img src="http://farm1.static.flickr.com/34/100174720_dad453636d_m.jpg" alt="Cherry pie with two hearts on top">
244
<li class="photo" data-tags="lattice, sour cherry, fruit">
245
<a href="http://www.flickr.com/photos/faerye/4839675135/in/photostream/">
246
<img src="http://farm5.static.flickr.com/4083/4839675135_fb5e88da3d_m.jpg" alt="Lattice-top sour cherry pie">
250
<li class="photo" data-tags="chocolate, cream, cinnamon, meringue, christmas, tree">
251
<a href="http://www.flickr.com/photos/faerye/4283875981/">
252
<img src="http://farm5.static.flickr.com/4029/4283875981_faaccb9089_m.jpg" alt="Chocolate cream pie with cinnamon meringue">
256
<li class="photo" data-tags="strawberry, chiffon, chocolate, wafer, fruit">
257
<a href="http://www.flickr.com/photos/faerye/4592482821/">
258
<img src="http://farm2.static.flickr.com/1070/4592482821_925ba97e60_m.jpg" alt="Strawberry chiffon pie with chocolate wafer crust">
262
<li class="photo" data-tags="key lime, whipped cream, graham cracker, slice, fruit">
263
<a href="http://www.flickr.com/photos/faerye/3492566166/">
264
<img src="http://farm4.static.flickr.com/3413/3492566166_0fd62e0d43_m.jpg" alt="Key lime pie with graham cracker crust">
268
<li class="photo" data-tags="lattice top, apple, fruit">
269
<a href="http://www.flickr.com/photos/faerye/3394679580/">
270
<img src="http://farm4.static.flickr.com/3569/3394679580_8b08c46fe6_m.jpg" alt="Lattice top apple pie">
274
</div></pre>
279
<pre class="code prettyprint">#photos {
280
border: 1px solid #cfcfcf;
287
display: inline-block;
290
/* fake inline-block for IE<8 */
295
#photos .empty { font-style: italic; }
304
border: 1px solid #000;
308
#photos .hidden { display: none; }</pre>
313
<pre class="code prettyprint">YUI().use('autocomplete-base', 'autocomplete-filters', function (Y) {
314
// Create a custom PieFilter class that extends AutoCompleteBase.
315
var PieFilter = Y.Base.create('pieFilter', Y.Base, [Y.AutoCompleteBase], {
316
initializer: function () {
317
this._bindUIACBase();
318
this._syncUIACBase();
322
// Create and configure an instance of the PieFilter class.
323
filter = new PieFilter({
324
inputNode: '#ac-input',
328
// Run an immediately-invoked function that returns an array of results to
329
// be used for each query, based on the photos on the page. Since the list
330
// of photos remains static, this saves time by not gathering the results
331
// for each query.
333
// If the list of results were not static, we could simply set the source
334
// to the function itself rather than invoking the function immediately,
335
// and it would then run on every query.
336
source: (function () {
339
// Build an array of results containing each photo in the list.
340
Y.all('#photos > .photo').each(function (node) {
343
tags: node.getAttribute('data-tags')
348
}()), // <-- Note the parens. This invokes the function immediately.
349
// Remove these to invoke the function on every query instead.
351
// Specify that the "tags" property of each result object contains the text
352
// to filter on.
353
resultTextLocator: 'tags',
355
// Use a result filter to filter the photo results based on their tags.
356
resultFilters: 'phraseMatch'
359
// Subscribe to the "results" event and update photo visibility based on
360
// whether or not they were included in the list of results.
361
filter.on('results', function (e) {
362
// First hide all the photos.
363
Y.all('#photos > .photo').addClass('hidden');
365
// Then unhide the ones that are in the current result list.
366
Y.Array.each(e.results, function (result) {
367
result.raw.node.removeClass('hidden');
373
<h2>Complete Example Source</h2>
375
<pre class="code prettyprint"><style scoped>
377
border: 1px solid #cfcfcf;
384
display: inline-block;
387
/* fake inline-block for IE<8 */
392
#photos .empty { font-style: italic; }
401
border: 1px solid #000;
406
#photos .hidden { display: none; }
409
<div id="demo" class="yui3-skin-sam">
410
<label for="ac-input">Filter by:</label><br>
411
<input id="ac-input" type="text">
413
<ul id="photos">
414
<li class="photo" data-tags="apple, fruit">
415
<a href="http://www.flickr.com/photos/wonko/5213328415/in/photostream/">
416
<img src="http://farm6.static.flickr.com/5002/5213328415_4cf3aa583f_m.jpg" alt="Apple pie">
420
<li class="photo" data-tags="pecan">
421
<a href="http://www.flickr.com/photos/wonko/5213327801/in/photostream/">
422
<img src="http://farm6.static.flickr.com/5208/5213327801_1e62145da1_m.jpg" alt="Pecan pie">
426
<li class="photo" data-tags="maple custard">
427
<a href="http://www.flickr.com/photos/wonko/5213920818/in/photostream/">
428
<img src="http://farm6.static.flickr.com/5245/5213920818_bf7cd599c3_m.jpg" alt="Maple custard pie">
432
<li class="photo" data-tags="pumpkin">
433
<a href="http://www.flickr.com/photos/wonko/5213912956/in/photostream/">
434
<img src="http://farm6.static.flickr.com/5049/5213912956_7cba11aa01_m.jpg" alt="Pumpkin pie">
438
<li class="photo" data-tags="turkey, pot pie, carrots, meat, savory">
439
<a href="http://www.flickr.com/photos/wonko/2055852065/in/photostream/">
440
<img src="http://farm3.static.flickr.com/2330/2055852065_d9a7d56650_m.jpg" alt="Turkey pot pie">
444
<li class="photo" data-tags="cherry, hearts, fruit">
445
<a href="http://www.flickr.com/photos/wonko/100174720/in/photostream/">
446
<img src="http://farm1.static.flickr.com/34/100174720_dad453636d_m.jpg" alt="Cherry pie with two hearts on top">
450
<li class="photo" data-tags="lattice, sour cherry, fruit">
451
<a href="http://www.flickr.com/photos/faerye/4839675135/in/photostream/">
452
<img src="http://farm5.static.flickr.com/4083/4839675135_fb5e88da3d_m.jpg" alt="Lattice-top sour cherry pie">
456
<li class="photo" data-tags="chocolate, cream, cinnamon, meringue, christmas, tree">
457
<a href="http://www.flickr.com/photos/faerye/4283875981/">
458
<img src="http://farm5.static.flickr.com/4029/4283875981_faaccb9089_m.jpg" alt="Chocolate cream pie with cinnamon meringue">
462
<li class="photo" data-tags="strawberry, chiffon, chocolate, wafer, fruit">
463
<a href="http://www.flickr.com/photos/faerye/4592482821/">
464
<img src="http://farm2.static.flickr.com/1070/4592482821_925ba97e60_m.jpg" alt="Strawberry chiffon pie with chocolate wafer crust">
468
<li class="photo" data-tags="key lime, whipped cream, graham cracker, slice, fruit">
469
<a href="http://www.flickr.com/photos/faerye/3492566166/">
470
<img src="http://farm4.static.flickr.com/3413/3492566166_0fd62e0d43_m.jpg" alt="Key lime pie with graham cracker crust">
474
<li class="photo" data-tags="lattice top, apple, fruit">
475
<a href="http://www.flickr.com/photos/faerye/3394679580/">
476
<img src="http://farm4.static.flickr.com/3569/3394679580_8b08c46fe6_m.jpg" alt="Lattice top apple pie">
483
YUI().use('autocomplete-base', 'autocomplete-filters', function (Y) {
484
// Create a custom PieFilter class that extends AutoCompleteBase.
485
var PieFilter = Y.Base.create('pieFilter', Y.Base, [Y.AutoCompleteBase], {
486
initializer: function () {
487
this._bindUIACBase();
488
this._syncUIACBase();
492
// Create and configure an instance of the PieFilter class.
493
filter = new PieFilter({
494
inputNode: '#ac-input',
498
// Run an immediately-invoked function that returns an array of results to
499
// be used for each query, based on the photos on the page. Since the list
500
// of photos remains static, this saves time by not gathering the results
501
// for each query.
503
// If the list of results were not static, we could simply set the source
504
// to the function itself rather than invoking the function immediately,
505
// and it would then run on every query.
506
source: (function () {
509
// Build an array of results containing each photo in the list.
510
Y.all('#photos > .photo').each(function (node) {
513
tags: node.getAttribute('data-tags')
518
}()), // <-- Note the parens. This invokes the function immediately.
519
// Remove these to invoke the function on every query instead.
521
// Specify that the "tags" property of each result object contains the text
522
// to filter on.
523
resultTextLocator: 'tags',
525
// Use a result filter to filter the photo results based on their tags.
526
resultFilters: 'phraseMatch'
529
// Subscribe to the "results" event and update photo visibility based on
530
// whether or not they were included in the list of results.
531
filter.on('results', function (e) {
532
// First hide all the photos.
533
Y.all('#photos > .photo').addClass('hidden');
535
// Then unhide the ones that are in the current result list.
536
Y.Array.each(e.results, function (result) {
537
result.raw.node.removeClass('hidden');
541
</script></pre>
546
<div id="sidebar" class="yui3-u">
550
<div class="sidebox">
552
<h2 class="no-toc">Examples</h2>
556
<ul class="examples">
559
<li data-description="How to provide autocomplete suggestions from a local array.">
560
<a href="ac-local.html">Basic Local Data</a>
565
<li data-description="How to provide autocomplete suggestions using a remote JSONP source.">
566
<a href="ac-jsonp.html">Remote Data via JSONP</a>
571
<li data-description="How to provide autocomplete suggestions using a YQL query source.">
572
<a href="ac-yql.html">Remote Data via YQL</a>
577
<li data-description="How to provide autocomplete suggestions using a DataSource instance.">
578
<a href="ac-datasource.html">Remote Data via DataSource</a>
583
<li data-description="How to implement delimited tag completion.">
584
<a href="ac-tagging.html">Tag Completion Using Query Delimiters</a>
589
<li data-description="How to find and select Flickr photos using a YQL source and a custom autocomplete result formatter.">
590
<a href="ac-flickr.html">Find Photos on Flickr (Custom Formatting, YQL Source)</a>
595
<li data-description="How to use autocomplete-base to filter a set of existing items on a page.">
596
<a href="ac-filter.html">Filter a Set of Existing Items on a Page</a>
610
<script src="../assets/vendor/prettify/prettify-min.js"></script>
611
<script>prettyPrint();</script>