Skip to content

Commit

Permalink
Issue #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Vohmjanin committed Aug 2, 2016
1 parent d8ecf0a commit 52c203d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
32 changes: 32 additions & 0 deletions demo/index.html
Expand Up @@ -58,6 +58,7 @@ <h1>Twitter Bootstrap Ajax Typeahead Plugin Demo</h1>
<li><a href="#tab-demo5" data-toggle="tab">Demo #5</a></li>
<li><a href="#tab-demo6" data-toggle="tab">Demo #6</a></li>
<li><a href="#tab-demo7" data-toggle="tab">Demo #7</a></li>
<li><a href="#tab-demo8" data-toggle="tab">Demo #8</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
Expand Down Expand Up @@ -224,6 +225,27 @@ <h3>Demo #7</h3>
'Seattle',
'Los Angeles'],
scrollBar:true
});
</pre>
</div>
</div>
<div class="tab-pane" id="tab-demo8">
<h3>Demo #8</h3>
<div class="well col-md-5">
<input id="demo8" type="text" class="col-md-12 form-control" placeholder="Search cities..." autocomplete="off" />
</div>
<div class="col-md-7">
<pre class="prettyprint">

$('#demo8').typeahead({
autoSelect: false,
source: [
{ ID: 1, Name: 'Toronto' },
{ ID: 2, Name: 'Montreal' },
{ ID: 3, Name: 'New York' },
],
display: 'Name',
val: 'ID'
});
</pre>
</div>
Expand Down Expand Up @@ -359,6 +381,16 @@ <h3>Demo #7</h3>
scrollBar:true,
onSelect: displayResult
});
$('#demo8').typeahead({
autoSelect:false,
source: [
{ID: 1, Name: 'Toronto'},
{ID: 2, Name: 'Montreal'},
{ID: 3, Name: 'New York'}
],
displayField: 'Name',
valueField: 'ID'
});
});
</script>
</body>
Expand Down
34 changes: 20 additions & 14 deletions js/bootstrap-typeahead.js
@@ -1,9 +1,9 @@
/*!
* bootstrap-typeahead.js v0.0.5 (http://www.upbootstrap.com)
* Copyright 2012-2015 Twitter Inc.
* Copyright 2012-2016 Twitter Inc.
* Licensed under MIT (https://github.com/biggora/bootstrap-ajax-typeahead/blob/master/LICENSE)
* See Demo: http://plugins.upbootstrap.com/bootstrap-ajax-typeahead
* Updated: 2015-04-05 11:43:56
* Updated: 2016-08-02 03:16:15
*
* Modifications by Paul Warelis and Alexey Gordeyev
*/
Expand Down Expand Up @@ -40,6 +40,7 @@
that.source = that.options.source || that.source;
that.displayField = that.options.displayField || that.displayField;
that.valueField = that.options.valueField || that.valueField;
that.autoSelect = that.options.autoSelect || that.autoSelect;

if (that.options.ajax) {
var ajax = that.options.ajax;
Expand Down Expand Up @@ -90,18 +91,20 @@
},
select: function () {
var $selectedItem = this.$menu.find('.active');
var value = $selectedItem.attr('data-value');
var text = this.$menu.find('.active a').text();

if (this.options.onSelect) {
this.options.onSelect({
value: value,
text: text
});
if($selectedItem.length) {
var value = $selectedItem.attr('data-value');
var text = this.$menu.find('.active a').text();

if (this.options.onSelect) {
this.options.onSelect({
value: value,
text: text
});
}
this.$element
.val(this.updater(text))
.change();
}
this.$element
.val(this.updater(text))
.change();
return this.hide();
},
updater: function (item) {
Expand Down Expand Up @@ -281,7 +284,9 @@
return i[0];
});

items.first().addClass('active');
if(that.autoSelect){
items.first().addClass('active');
}

this.$menu.html(items);
return this;
Expand Down Expand Up @@ -499,6 +504,7 @@
item: '<li><a href="#"></a></li>',
valueField: 'id',
displayField: 'name',
autoSelect: true,
onSelect: function () {
},
ajax: {
Expand Down

0 comments on commit 52c203d

Please sign in to comment.