This example demonstrates how to use the queryDelimiter
config, a custom result filter, and a couple of event handlers to provide suggestions as a user enters tags in an input field. The result list is prepopulated with suggested tags and is displayed immediately when the input field receives focus rather than waiting for the user to begin typing.
HTML
Note: be sure to add the yui3-skin-sam
classname to the
page's <body>
element or to a parent element of the widget in order to apply
the default CSS skin. See Understanding Skinning.
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> <label for="ac-input">Tags:</label><br> <input id="ac-input" type="text"> </div>
JavaScript
Tags Array
While a remote result source could also be used, in this example we'll use the following local array of suggested tags to keep things simple:
var tags = [ 'css', 'css3', 'douglas crockford', 'ecmascript', 'html', 'html5', 'java', 'javascript', 'json', 'node.js', 'pie', 'yui' ];
Implementation
YUI().use('autocomplete', 'autocomplete-filters', 'autocomplete-highlighters', function (Y) { var inputNode = Y.one('#ac-input'); inputNode.plug(Y.Plugin.AutoComplete, { allowTrailingDelimiter: true, minQueryLength: 0, queryDelay: 0, queryDelimiter: ',', source: tags, resultHighlighter: 'startsWith', // Chain together a startsWith filter followed by a custom result filter // that only displays tags that haven't already been selected. resultFilters: ['startsWith', function (query, results) { // Split the current input value into an array based on comma delimiters. var selected = inputNode.get('value').split(/\s*,\s*/); // Convert the array into a hash for faster lookups. selected = Y.Array.hash(selected); // Filter out any results that are already selected, then return the // array of filtered results. return Y.Array.filter(results, function (result) { return !selected.hasOwnProperty(result.text); }); }] }); // When the input node receives focus, send an empty query to display the full // list of tag suggestions. inputNode.on('focus', function () { inputNode.ac.sendRequest(''); }); // After a tag is selected, send an empty query to update the list of tags. inputNode.ac.after('select', function () { // Send the query on the next tick to ensure that the input node's blur // handler doesn't hide the result list right after we show it. setTimeout(function () { inputNode.ac.sendRequest(''); inputNode.ac.show(); }, 1); }); });
Complete Example Source
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> <label for="ac-input">Tags:</label><br> <input id="ac-input" type="text"> </div> <script> YUI().use('autocomplete', 'autocomplete-filters', 'autocomplete-highlighters', function (Y) { var inputNode = Y.one('#ac-input'), tags = [ 'css', 'css3', 'douglas crockford', 'ecmascript', 'html', 'html5', 'java', 'javascript', 'json', 'node.js', 'pie', 'yui' ]; inputNode.plug(Y.Plugin.AutoComplete, { allowTrailingDelimiter: true, minQueryLength: 0, queryDelay: 0, queryDelimiter: ',', source: tags, resultHighlighter: 'startsWith', // Chain together a startsWith filter followed by a custom result filter // that only displays tags that haven't already been selected. resultFilters: ['startsWith', function (query, results) { // Split the current input value into an array based on comma delimiters. var selected = inputNode.get('value').split(/\s*,\s*/); // Convert the array into a hash for faster lookups. selected = Y.Array.hash(selected); // Filter out any results that are already selected, then return the // array of filtered results. return Y.Array.filter(results, function (result) { return !selected.hasOwnProperty(result.text); }); }] }); // When the input node receives focus, send an empty query to display the full // list of tag suggestions. inputNode.on('focus', function () { inputNode.ac.sendRequest(''); }); // After a tag is selected, send an empty query to update the list of tags. inputNode.ac.after('select', function () { // Send the query on the next tick to ensure that the input node's blur // handler doesn't hide the result list right after we show it. setTimeout(function () { inputNode.ac.sendRequest(''); inputNode.ac.show(); }, 1); }); }); </script>