md-autocomplete - remove selected items in suggestion

1.3k views Asked by At

Im trying to remove an md-item in suggestion when the user selected an item and is marked as favorite. To clarify the question here is the structure below.

Supposing that we have this structure of states.

function loadAll() {

      var allStates = [
          {
             display : Alabama,
             value : alabama,
             is_favorite : false
          },
          {
             display : Alaska,
             value : alaska,
             is_favorite : false
          },
          {
             display : California,
             value : california,
             is_favorite : false
          },
          {
             display : Colorado,
             value : colorado,
             is_favorite : false
          },
          {
             display : Florida,
             value : florida,
             is_favorite : false
          },
          {
             display : Georgia,
             value : georgia,
             is_favorite : false
          },
      ];

      return allStates;
}

This is what i did for marking the state as favorite when the user selected the object i put it on the function md-selected-item-change.

function selectedItemChange(object) {
    if (object) {
        object.is_favorite = true;
    }
}

Now it is possible to alter the createFilterFor() to exclude from the results the states which has is_favorite == true after selecting a particular state?

function createFilterFor(query) {
  var lowercaseQuery = angular.lowercase(query);

  return function filterFn(state) {
    return (state.value.indexOf(lowercaseQuery) === 0);
  };

}

This is what ive'd tried so far but it doesn't render the correct result.

return (state.value.indexOf(lowercaseQuery) === 0 && state.is_favorite == false);

UPDATE

CODEPEN DEMO

2

There are 2 answers

2
Ash On BEST ANSWER

There is a simple fix for this:

md-items="item in ctrl.querySearch(ctrl.searchText)| filter:{ is_favorite:false }"

All you have to do is to put the filter in the source for your control.

Please check the code pen : http://codepen.io/ash972/pen/ZLYNPM

1
Yaser On

Create a function like this:

function filterFavotites(item) {
  return !item.is_favorite;
}

And call it in querySearch before returning results:

function querySearch (query) {
  var results = query ? vm.states.filter( createFilterFor(query) ) : vm.states,
      deferred;

  results = results.filter(filterFavotites);

  return results;
}