@@ -162,6 +162,17 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
162
162
163
163
//pop-up element used to display matches
164
164
var popUpEl = angular . element ( '<div uib-typeahead-popup></div>' ) ;
165
+
166
+ var clearBtnEl = angular . element ( '<span></span>' ) ;
167
+ clearBtnEl . attr ( {
168
+ 'class' : 'glyphicon glyphicon-remove' ,
169
+ 'style' : 'position: absolute; cursor: pointer;' ,
170
+ 'ng-style' : '{top: positionClearBtn.top+\'px\', left: positionClearBtn.left +\'px\'}' ,
171
+ 'ng-click' : 'clearModel()' ,
172
+ 'ng-show' : 'showClearBtn'
173
+ } ) ;
174
+
175
+
165
176
popUpEl . attr ( {
166
177
id : popupId ,
167
178
matches : 'matches' ,
@@ -319,6 +330,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
319
330
function recalculatePosition ( ) {
320
331
scope . position = appendToBody ? $position . offset ( element ) : $position . position ( element ) ;
321
332
scope . position . top += element . prop ( 'offsetHeight' ) ;
333
+ scope . positionClearBtn = $position . position ( element ) ;
334
+ scope . positionClearBtn . top += element . prop ( 'offsetHeight' ) / 4 ;
335
+ scope . positionClearBtn . left += element . prop ( 'offsetWidth' ) - 20 ;
322
336
}
323
337
324
338
//we need to propagate user's query so we can higlight matches
@@ -340,7 +354,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
340
354
} ;
341
355
342
356
resetMatches ( ) ;
343
-
357
+ //method for clear blutton
358
+ scope . clearModel = function ( ) {
359
+ $setModelValue ( originalScope , '' ) ;
360
+ scope . showClearBtn = false ;
361
+ } ;
344
362
scope . assignIsOpen = function ( isOpen ) {
345
363
isOpenSetter ( originalScope , isOpen ) ;
346
364
} ;
@@ -351,6 +369,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
351
369
var model , item ;
352
370
353
371
selected = true ;
372
+
373
+ scope . showClearBtn = true ; // Indicator that some choice selected (clear button should appear)
354
374
locals [ parserResult . itemName ] = item = scope . matches [ activeIdx ] . model ;
355
375
model = parserResult . modelMapper ( originalScope , locals ) ;
356
376
$setModelValue ( originalScope , model ) ;
@@ -498,6 +518,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
498
518
} ) ;
499
519
500
520
var $popup = $compile ( popUpEl ) ( scope ) ;
521
+ var $clearBtn = $compile ( clearBtnEl ) ( scope ) ;
501
522
502
523
if ( appendToBody ) {
503
524
$document . find ( 'body' ) . append ( $popup ) ;
@@ -506,6 +527,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
506
527
} else {
507
528
element . after ( $popup ) ;
508
529
}
530
+ element . after ( $clearBtn ) ;
509
531
510
532
this . init = function ( _modelCtrl , _ngModelOptions ) {
511
533
modelCtrl = _modelCtrl ;
@@ -529,6 +551,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
529
551
isLoadingSetter ( originalScope , false ) ;
530
552
cancelPreviousTimeout ( ) ;
531
553
resetMatches ( ) ;
554
+ if ( ! inputValue ) {
555
+ scope . showClearBtn = false ; // Hide clear button if input empty
556
+ }
532
557
}
533
558
534
559
if ( isEditable ) {
0 commit comments