mirror of
https://github.com/twbs/bootstrap.git
synced 2025-04-06 23:57:36 +02:00
typahead does case insensitive matching with simple sorter + accepts highlighter option
This commit is contained in:
parent
f768242759
commit
6935f693b3
2
docs/assets/css/bootstrap.css
vendored
2
docs/assets/css/bootstrap.css
vendored
@ -1983,7 +1983,7 @@ table .span12 {
|
|||||||
.navbar-search .search-query :-moz-placeholder {
|
.navbar-search .search-query :-moz-placeholder {
|
||||||
color: #eeeeee;
|
color: #eeeeee;
|
||||||
}
|
}
|
||||||
.navbar-search .search-query ::-webkit-input-placeholder {
|
.navbar-search .search-query::-webkit-input-placeholder {
|
||||||
color: #eeeeee;
|
color: #eeeeee;
|
||||||
}
|
}
|
||||||
.navbar-search .search-query:hover {
|
.navbar-search .search-query:hover {
|
||||||
|
@ -1362,7 +1362,7 @@ $('.myCarousel').carousel({
|
|||||||
<tr>
|
<tr>
|
||||||
<th style="width: 100px;">Name</th>
|
<th style="width: 100px;">Name</th>
|
||||||
<th style="width: 50px;">type</th>
|
<th style="width: 50px;">type</th>
|
||||||
<th style="width: 90px;">default</th>
|
<th style="width: 100px;">default</th>
|
||||||
<th>description</th>
|
<th>description</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -1382,15 +1382,21 @@ $('.myCarousel').carousel({
|
|||||||
<tr>
|
<tr>
|
||||||
<td>matcher</td>
|
<td>matcher</td>
|
||||||
<td>function</td>
|
<td>function</td>
|
||||||
<td>case sensitive</td>
|
<td>case insensitive</td>
|
||||||
<td>The method used to determine if a query matches an item. Accepts a single argument, the <code>item</code> against which to test the query. Access the current query with <code>this.query</code>. Return a boolean <code>true</code> if query is a match.</td>
|
<td>The method used to determine if a query matches an item. Accepts a single argument, the <code>item</code> against which to test the query. Access the current query with <code>this.query</code>. Return a boolean <code>true</code> if query is a match.</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>sorter</td>
|
<td>sorter</td>
|
||||||
<td>function</td>
|
<td>function</td>
|
||||||
<td>no sort</td>
|
<td>exact match,<br> case sensitive,<br> case insensitive</td>
|
||||||
<td>Method used to sort autocomplete results. Accepts a single argument <code>items</code> and has the scope of the typeahead instance. Reference the current query with <code>this.query</code>.</td>
|
<td>Method used to sort autocomplete results. Accepts a single argument <code>items</code> and has the scope of the typeahead instance. Reference the current query with <code>this.query</code>.</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>highlighter</td>
|
||||||
|
<td>function</td>
|
||||||
|
<td>highlights all default matches</td>
|
||||||
|
<td>Method used to highlight autocomplete results. Accepts a single argument <code>item</code> and has the scope of the typeahead instance. Should return html.</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
12
docs/templates/pages/javascript.mustache
vendored
12
docs/templates/pages/javascript.mustache
vendored
@ -1297,7 +1297,7 @@ $('.myCarousel').carousel({
|
|||||||
<tr>
|
<tr>
|
||||||
<th style="width: 100px;">{{_i}}Name{{/i}}</th>
|
<th style="width: 100px;">{{_i}}Name{{/i}}</th>
|
||||||
<th style="width: 50px;">{{_i}}type{{/i}}</th>
|
<th style="width: 50px;">{{_i}}type{{/i}}</th>
|
||||||
<th style="width: 90px;">{{_i}}default{{/i}}</th>
|
<th style="width: 100px;">{{_i}}default{{/i}}</th>
|
||||||
<th>{{_i}}description{{/i}}</th>
|
<th>{{_i}}description{{/i}}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -1317,15 +1317,21 @@ $('.myCarousel').carousel({
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{_i}}matcher{{/i}}</td>
|
<td>{{_i}}matcher{{/i}}</td>
|
||||||
<td>{{_i}}function{{/i}}</td>
|
<td>{{_i}}function{{/i}}</td>
|
||||||
<td>case sensitive</td>
|
<td>{{_i}}case insensitive{{/i}}</td>
|
||||||
<td>{{_i}}The method used to determine if a query matches an item. Accepts a single argument, the <code>item</code> against which to test the query. Access the current query with <code>this.query</code>. Return a boolean <code>true</code> if query is a match.{{/i}}</td>
|
<td>{{_i}}The method used to determine if a query matches an item. Accepts a single argument, the <code>item</code> against which to test the query. Access the current query with <code>this.query</code>. Return a boolean <code>true</code> if query is a match.{{/i}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{_i}}sorter{{/i}}</td>
|
<td>{{_i}}sorter{{/i}}</td>
|
||||||
<td>{{_i}}function{{/i}}</td>
|
<td>{{_i}}function{{/i}}</td>
|
||||||
<td>no sort</td>
|
<td>{{_i}}exact match,<br> case sensitive,<br> case insensitive{{/i}}</td>
|
||||||
<td>{{_i}}Method used to sort autocomplete results. Accepts a single argument <code>items</code> and has the scope of the typeahead instance. Reference the current query with <code>this.query</code>.{{/i}}</td>
|
<td>{{_i}}Method used to sort autocomplete results. Accepts a single argument <code>items</code> and has the scope of the typeahead instance. Reference the current query with <code>this.query</code>.{{/i}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>{{_i}}highlighter{{/i}}</td>
|
||||||
|
<td>{{_i}}function{{/i}}</td>
|
||||||
|
<td>{{_i}}highlights all default matches{{/i}}</td>
|
||||||
|
<td>{{_i}}Method used to highlight autocomplete results. Accepts a single argument <code>item</code> and has the scope of the typeahead instance. Should return html.{{/i}}</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
43
js/bootstrap-typeahead.js
vendored
43
js/bootstrap-typeahead.js
vendored
@ -24,8 +24,9 @@
|
|||||||
var Typeahead = function ( element, options ) {
|
var Typeahead = function ( element, options ) {
|
||||||
this.$element = $(element)
|
this.$element = $(element)
|
||||||
this.options = $.extend({}, $.fn.typeahead.defaults, options)
|
this.options = $.extend({}, $.fn.typeahead.defaults, options)
|
||||||
this.matcher = this.options.matcher
|
this.matcher = this.options.matcher || this.matcher
|
||||||
this.sorter = this.options.sorter
|
this.sorter = this.options.sorter || this.sorter
|
||||||
|
this.highlighter = this.options.highlighter || this.highlighter
|
||||||
this.$menu = $(this.options.menu).appendTo('body')
|
this.$menu = $(this.options.menu).appendTo('body')
|
||||||
this.source = this.options.source
|
this.source = this.options.source
|
||||||
this.shown = false
|
this.shown = false
|
||||||
@ -87,17 +88,37 @@
|
|||||||
return this.render(items.slice(0, this.options.items)).show()
|
return this.render(items.slice(0, this.options.items)).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
, matcher: function (item) {
|
||||||
|
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
|
||||||
|
}
|
||||||
|
|
||||||
|
, sorter: function (items) {
|
||||||
|
var beginswith = []
|
||||||
|
, caseSensitive = []
|
||||||
|
, caseInsensitive = []
|
||||||
|
, item
|
||||||
|
|
||||||
|
while (item = items.shift()) {
|
||||||
|
if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
|
||||||
|
else if (~item.indexOf(this.query)) caseSensitive.push(item)
|
||||||
|
else caseInsensitive.push(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
return beginswith.concat(caseSensitive, caseInsensitive)
|
||||||
|
}
|
||||||
|
|
||||||
|
, highlighter: function (item) {
|
||||||
|
return item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) {
|
||||||
|
return '<strong>' + match + '</strong>'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
, render: function (items) {
|
, render: function (items) {
|
||||||
var that = this
|
var that = this
|
||||||
, QUERY = new RegExp('(' + this.query + ')', 'ig')
|
|
||||||
|
|
||||||
items = $(items).map(function (i, item) {
|
items = $(items).map(function (i, item) {
|
||||||
i = $(that.options.item).attr('data-value', item)
|
i = $(that.options.item).attr('data-value', item)
|
||||||
|
i.find('a').html(that.highlighter(item))
|
||||||
i.find('a').html(item.replace(QUERY, function ($1, match) {
|
|
||||||
return '<strong>' + match + '</strong>'
|
|
||||||
}))
|
|
||||||
|
|
||||||
return i[0]
|
return i[0]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -230,12 +251,6 @@
|
|||||||
, items: 8
|
, items: 8
|
||||||
, menu: '<ul class="typeahead dropdown-menu"></ul>'
|
, menu: '<ul class="typeahead dropdown-menu"></ul>'
|
||||||
, item: '<li><a href="#"></a></li>'
|
, item: '<li><a href="#"></a></li>'
|
||||||
, matcher: function (item) {
|
|
||||||
return ~item.indexOf(this.query)
|
|
||||||
}
|
|
||||||
, sorter: function (items) {
|
|
||||||
return items
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$.fn.typeahead.Constructor = Typeahead
|
$.fn.typeahead.Constructor = Typeahead
|
||||||
|
Loading…
x
Reference in New Issue
Block a user