Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to override default autoComplete item rendering via options #3

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Added renderItem() function to options
  • Loading branch information
Alan McNea committed Nov 21, 2014
commit 88647b43302f774f1e3601cd8bdc3a3c971a2489
18 changes: 15 additions & 3 deletions jquery.auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@

(function($){
$.fn.autoComplete = function(options){
var o = $.extend({ source: '', minChars: 3, delay: 100, cache: true, menuClass: '', onSelect: function(term){} }, options);
var defaultO = {
source: '',
minChars: 3,
delay: 100,
cache: true,
menuClass: '',
onSelect: function(term){},
renderItem: function (item, search) {
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>';
}
},
o = $.extend(defaultO, options);

// public methods
if (typeof options == 'string') {
Expand Down Expand Up @@ -87,9 +99,9 @@
var val = that.val();
that.cache[val] = data;
if (data.length && val.length >= o.minChars) {
var s = '', re = new RegExp("(" + val.split(' ').join('|') + ")", "gi");
var s = '';
for (i=0;i<data.length;i++)
s += '<div class="autocomplete-suggestion" data-val="'+data[i]+'">'+data[i].replace(re, "<b>$1</b>")+'</div>';
s += o.renderItem(data[i], val);
that.sb.html(s).show();
}
else
Expand Down