Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

md-autocomplete clear text entry when item is selected #1829

Closed
kschwidder opened this issue Mar 8, 2015 · 18 comments
Closed

md-autocomplete clear text entry when item is selected #1829

kschwidder opened this issue Mar 8, 2015 · 18 comments
Milestone

Comments

@kschwidder
Copy link

It would be beneficial if the md-autocomplete can be customized to clear the text when an item has been selected. When using the md-autocomplete to search for items and maintaining a list of objects it is cumbersome to always press the clear button.

As a workaround I am accessing the $mdAutocompleteCtrl and triggering the clear() method but this is not a good decision....

@david-gang
Copy link
Contributor

What do you mean by cleared?
Ideally when selecting an item, this item should be selected and the text you specify should appear.
You see it nicely in the demo
https://material.angularjs.org/#/demo/material.components.autocomplete
Please check also if you have set the md-item-text.
If this is not what you want, may you please demonstrate with a plunkr what your intent is?

@mikekr1
Copy link

mikekr1 commented Mar 12, 2015

I'm pretty certain @kschwidder is talking about adding an option that allows for the text field to be cleared once a selection is made from the autocomplete dropdown.

This is useful when using the same autocomplete field to make multiple selections.

@kschwidder
Copy link
Author

indeed !

@robertmesserle
Copy link
Contributor

@kschwidder Wouldn't this be accomplished by binding something to the searchText and clearing that value on select?

@kschwidder
Copy link
Author

It doesn't since after a selection the option is shown with a clearance button. When setting the SearchText the button is still shown. There is currently no way to close the button via an API.
What I am doing so far is this :
$timeout( function() { $scope.$$childHead.$mdAutocompleteCtrl.clear(); },100);

@onyx1337
Copy link

Have same issue.

I solve it like that :
in 'md-selected-item-change' , I assign md-search-text to void and 'md-selected-item' to undefined
like this

 $scope.selectedItemChange= function(){
              // some stuff with selectedItem

            // clear input
            $scope.searchText = '';
            $scope.selectedItem= undefined;

        };
 <md-autocomplete
            md-selected-item="selectedItem"
            md-search-text="searchText"
            md-items="item in getMatches(searchText)"
            md-item-text="item.name"
            md-selected-item-change="selectedItemChange()" >

@robertmesserle
Copy link
Contributor

Is this what you are trying to do?

http://codepen.io/robertmesserle/pen/72185ea178757d282e5862fcc22399dd?editors=101

@kschwidder
Copy link
Author

yes

@kschwidder
Copy link
Author

The difference in my case is that the md-items="item in loadData(md.searchText)" is a $http request.
When I set searchText and selectedItem to null it is showing constantly a loading bar.

@robertmesserle
Copy link
Contributor

Ah, there is already an issue for that: #1798

I'm going to look into a fix for that right now.

@johnSowingo
Copy link

@milad-kh
Copy link

do it:
$scope.$$childTail.searchText = '';

@christianpanea
Copy link

<md-autocomplete md-selected-item="data.page" md-search-text="data.ac" md-items="item in getFiltered(pages, data.ac)" md-item-text="item.name" md-min-length="0" placeholder="Select a Dashboard"> <span md-highlight-text="data.ac" md-highlight-flags="^ig">{{item.name}}</span> </md-autocomplete> <div class="md-actions" layout="row" layout-align="start center"> <md-button class="md-raised md-primary" ng-click="addReport()" ng-disabled="!data.page">Add Report</md-button> </div>

and my js file for the button here
$scope.addReport = function() { $scope.report.export_items.push({ page: $scope.data.page.id, card: ($scope.data.card || {})._id, export_type: $scope.data.export_type }); $scope.cache[$scope.data.page.id] = $scope.data.page.name; if (($scope.data.card || {})._id) { $scope.cache[$scope.data.card._id] = $scope.data.card.title; } };

Question... how can i make the same button to reset to default the selected option in autocomplete?

@stephengardner
Copy link

+1
Wow this needs a fix.

There are thousands of use cases where this is intended.

Ex:
I select a book from the dropdown.
I submit the book.
Success message pops up, thanks for submitting.
Form is cleared.
Continue...

@kikocastro
Copy link

kikocastro commented Aug 28, 2016

+1

This workaround fixed

$scope.selectedOption = undefined;
$scope.searchText = '';
$scope.$$childTail.searchText = '';

but the list with the options keeps opened :(

@enigram
Copy link

enigram commented Jan 23, 2017

To clear the text, use the workaround suggested.

However, with default options, the list will remain open since the search field has the focus. To close the list, ensure that the md-min-length is greater than 0. This will require the user to type in something in the box for the list to open.

@dagogo03
Copy link

$scope.$$childTail.searchText = '';
$scope.$$childTail.selectedOption = undefined;

This work for me!!! #

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

14 participants