简体   繁体   中英

md-contact-chips return search filter not working

I does not seem to filter on query search. I wanted to query search on any case instead of query searching on lower case. So this is a modified version of the original code from google.

    function querySearch(query) {
      var results = query ?
        self.allContacts.filter(createFilterFor(query)) : [];
      return results;
    }  
    function createFilterFor(query) {
      return function filterFn(contact) {
        return (contact.indexOf(query) != -1);
      };
    }

Please see fiddle for my attempt: https://codepen.io/hamsaya/pen/PWMNNL?editors=1010

i create it in a different way check this out

(function() {
  'use strict';
  angular
    .module('MyApp')
    .controller('ContactChipDemoCtrl', DemoCtrl);

  function DemoCtrl() {
    var self = this;

    self.querySearch = querySearch;
    self.contacts = [];
    self.filterSelected = [];

    function querySearch(query) {
      var results = query ?
        createFilterFor(query) : [];
      console.log(results)
      return results;
    }


    self.allContacts = [{
      id: '1',
      name: 'Oddr Sarno'
    }, {
      id: '2',
      name: 'Hidi Barno'
    }];
    var contact = [];

     function createFilterFor(query) {
       contact = [];
       debugger
       for(var i=0; i<=self.allContacts.length -1; i++){
         if(self.allContacts[i].name.indexOf(query) != -1){
           contact.push(self.allContacts[i]);
         }
       }
       return contact;
    }
  }
})();

<div ng-controller="ContactChipDemoCtrl as ctrl" layout="column" class="chipsdemoContactChips" ng-app="MyApp">
{{ctrl.allContacts}}
  <md-content class="md-padding autocomplete" layout="column">
    <p>Contact Chips.</p>
    <md-contact-chips 
                      ng-model="ctrl.contacts" 
                      md-contacts="ctrl.querySearch($query)" 
                      md-contact-name="name"  
                      md-contact- md-require-match="" 
                      filter-selected="ctrl.allContacts.id" 
                      placeholder="To">
    </md-contact-chips>  

  </md-content>
</div>

in createFilterFor function i push items only according to the search query and return the filter data to html.

Alse in html change md-contact-name to just name

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