diff --git a/docs/javascript.html b/docs/javascript.html index d489241157..53130bd16f 100644 --- a/docs/javascript.html +++ b/docs/javascript.html @@ -1353,7 +1353,7 @@ $('.myCarousel').carousel({ Name type - default + default description @@ -1370,6 +1370,18 @@ $('.myCarousel').carousel({ 8 The max number of items to display in the dropdown. + + matcher + function + case sensitive + The method used to determine if a query matches an item. Accepts a single argument, the item against which to test the query. Access the current query with this.query. Return a boolean true if query is a match. + + + sorter + function + no sort + Method used to sort autocomplete results. Accepts a single argument items and has the scope of the typeahead instance. Reference the current query with this.query. + @@ -1378,6 +1390,9 @@ $('.myCarousel').carousel({
 <input type="text" data-provide="typeahead">
 
+

Methods

+

.typeahead(options)

+

Initializes an input with a typahead.

diff --git a/docs/templates/pages/javascript.mustache b/docs/templates/pages/javascript.mustache index 9113c1776f..3c7a3b101f 100644 --- a/docs/templates/pages/javascript.mustache +++ b/docs/templates/pages/javascript.mustache @@ -1288,7 +1288,7 @@ $('.myCarousel').carousel({ {{_i}}Name{{/i}} {{_i}}type{{/i}} - {{_i}}default{{/i}} + {{_i}}default{{/i}} {{_i}}description{{/i}} @@ -1305,6 +1305,18 @@ $('.myCarousel').carousel({ 8 {{_i}}The max number of items to display in the dropdown.{{/i}} + + {{_i}}matcher{{/i}} + {{_i}}function{{/i}} + case sensitive + {{_i}}The method used to determine if a query matches an item. Accepts a single argument, the item against which to test the query. Access the current query with this.query. Return a boolean true if query is a match.{{/i}} + + + {{_i}}sorter{{/i}} + {{_i}}function{{/i}} + no sort + {{_i}}Method used to sort autocomplete results. Accepts a single argument items and has the scope of the typeahead instance. Reference the current query with this.query.{{/i}} + @@ -1313,6 +1325,9 @@ $('.myCarousel').carousel({
 <input type="text" data-provide="typeahead">
 
+

{{_i}}Methods{{/i}}

+

.typeahead({{_i}}options{{/i}})

+

{{_i}}Initializes an input with a typahead.{{/i}}

\ No newline at end of file diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js index b4d839c936..39331816e8 100644 --- a/js/bootstrap-typeahead.js +++ b/js/bootstrap-typeahead.js @@ -24,6 +24,8 @@ var Typeahead = function ( element, options ) { this.$element = $(element) this.options = $.extend({}, $.fn.typeahead.defaults, options) + this.matcher = this.options.matcher + this.sorter = this.options.sorter this.$menu = $(this.options.menu).appendTo('body') this.source = this.options.source this.shown = false @@ -34,11 +36,6 @@ constructor: Typeahead - , matcher: function (item, query) { - // ;_; http://jsperf.com/asdfdfasdfa - return ~item.toLowerCase().indexOf(query) - } - , select: function () { var val = this.$menu.find('.active').attr('data-value') this.$element.val(val) @@ -77,12 +74,12 @@ return this.shown ? this.hide() : this } - q = this.query.toLowerCase() - - items = jQuery.grep(this.source, function (item) { - if (that.matcher(item, q)) return item + items = $.grep(this.source, function (item) { + if (that.matcher(item)) return item }) + items = this.sorter(items) + if (!items.length) { return this.shown ? this.hide() : this } @@ -233,6 +230,12 @@ , items: 8 , menu: '' , item: '
  • ' + , matcher: function (item) { + return ~item.indexOf(this.query) + } + , sorter: function (items) { + return items + } } $.fn.typeahead.Constructor = Typeahead