简体   繁体   中英

md-autocomplete - remove selected items in suggestion

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

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM