Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions 19 auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var autoComplete = (function(){
offsetTop: 1,
cache: 1,
menuClass: '',
container: 'body',
renderItem: function (item, search){
// escape special characters
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
Expand All @@ -56,16 +57,24 @@ var autoComplete = (function(){
that.sc = document.createElement('div');
that.sc.className = 'autocomplete-suggestions '+o.menuClass;

// If adding into a results container, remove the position absolute css styles
if (o.container !== "body") {
that.sc.className = that.sc.className + ' autocomplete-suggestions--in-container';
}

that.autocompleteAttr = that.getAttribute('autocomplete');
that.setAttribute('autocomplete', 'off');
that.cache = {};
that.last_val = '';

that.updateSC = function(resize, next){
var rect = that.getBoundingClientRect();
that.sc.style.left = Math.round(rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft) + 'px';
that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px';
that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth
if (o.container === 'body') {
// If the container is not the body, do not absolutely position in the window.
var rect = that.getBoundingClientRect();
that.sc.style.left = Math.round(rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft) + 'px';
that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px';
that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's still beneficial to set width when using a non-body container.

}
if (!resize) {
that.sc.style.display = 'block';
if (!that.sc.maxHeight) { that.sc.maxHeight = parseInt((window.getComputedStyle ? getComputedStyle(that.sc, null) : that.sc.currentStyle).maxHeight); }
Expand All @@ -82,7 +91,7 @@ var autoComplete = (function(){
}
}
addEvent(window, 'resize', that.updateSC);
document.body.appendChild(that.sc);
document.querySelector(o.container).appendChild(that.sc);

live('autocomplete-suggestion', 'mouseleave', function(e){
var sel = that.sc.querySelector('.autocomplete-suggestion.selected');
Expand Down
48 changes: 48 additions & 0 deletions 48 demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ <h4>Advanced suggestions handling and custom layout</h4>
However, when choosing an item, the <span class="inline-code">onSelect()</span> callback returns all required information.
</p>


<h4>Customizing the Display Container</h4>
<p>Sometimes you don't want the container to be just placed at the end of the body tag, and it would make more sense for your use case to have it display in a different location.
To do something like that, you would just have to scaffold out an empty block container (div, section, etc) for results, and then pass a class or ID attached to that container into
the options when you initalize autocomplete:</p>


<form onsubmit="return false;" class="pure-form" style="margin:30px 0 40px">
<input id="container-demo" autofocus type="text" name="q" placeholder="See the results ..." style="width:100%;max-width:600px;outline:0">
</form>

<div class="results-container"></div>

<pre class="prettyprint"><code>var demo3 = new autoComplete({
selector: '#container-demo',
minChars: 1,
container: '.results-container',
source: function(term, suggest){
term = term.toLowerCase();
var choices = ['ActionScript', 'AppleScript', 'Asp', 'Assembly', 'BASIC', 'Batch', 'C', 'C++', 'CSS', 'Clojure', 'COBOL', 'ColdFusion', 'Erlang', 'Fortran', 'Groovy', 'Haskell', 'HTML', 'Java', 'JavaScript', 'Lisp', 'Perl', 'PHP', 'PowerShell', 'Python', 'Ruby', 'Scala', 'Scheme', 'SQL', 'TeX', 'XML'];
var suggestions = [];
for (i=0;i&lt;choices.length;i++)
if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]);
suggest(suggestions);
}
});</code></pre>

<pre class="prettyprint"><code>&lt;form onsubmit="return false;" class="pure-form" style="margin:30px 0 40px"&gt;
&lt;input id="container-demo" autofocus type="text" name="q" placeholder="See the results ..." style="width:100%;max-width:600px;outline:0"&gt;
&lt;/form&gt;

&lt;div class="results-container"&gt;&lt;/div&gt;</code></pre>


<div style="margin:60px 0 40px;overflow:hidden;border-top:1px solid #eee;padding-top:40px">
<span id="github_social"></span>
<div style="float:left;margin-right:35px">
Expand Down Expand Up @@ -301,6 +335,20 @@ <h4>Advanced suggestions handling and custom layout</h4>
}
});

var demo3 = new autoComplete({
selector: '#container-demo',
minChars: 1,
container: '.results-container',
source: function(term, suggest){
term = term.toLowerCase();
var choices = ['ActionScript', 'AppleScript', 'Asp', 'Assembly', 'BASIC', 'Batch', 'C', 'C++', 'CSS', 'Clojure', 'COBOL', 'ColdFusion', 'Erlang', 'Fortran', 'Groovy', 'Haskell', 'HTML', 'Java', 'JavaScript', 'Lisp', 'Perl', 'PHP', 'PowerShell', 'Python', 'Ruby', 'Scala', 'Scheme', 'SQL', 'TeX', 'XML'];
var suggestions = [];
for (i=0;i<choices.length;i++)
if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]);
suggest(suggestions);
}
});

if (~window.location.href.indexOf('http')) {
(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;po.src = 'https://apis.google.com/js/plusone.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);})();
(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.4&appId=114593902037957";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.