javascriptangularjsangular-materialangular-formly

How to add a search box inside select element generated by angular-formly


I am trying to add a search box inside select elements. I am using angular-formly library along with angular-formly-material for material design. I am trying to achieve something similar to angular-material select box as shown in pick a vegetable example

ISSUE

I am able to see the search box but I cannot type into that box. Hence filtering does not work. I have no idea where to put some controller code as described in example.

CODE

   formlyConfig.setType({
     name: 'select',
     template: '<md-input-container>'
        +'<label for="{{id + \'_\'+ $index}}">'
        +' {{to.label}}'
        +' </label>'
        + '<md-select ng-model="model[options.key]"'
                  + 'md-on-close="clearSearchTerm()"'
                  + 'data-md-container-class="selectdemoSelectHeader"'
                  + '>'
        +'<md-select-header class="demo-select-header">'
            +'<input ng-model="searchTerm"'
                   +'type="search"'
                   +'placeholder="Search"'
                   +'class="demo-header-searchbox md-text">'
          +'</md-select-header>'
          +'<md-optgroup >'
            +'<md-option ng-value="option[to.valueProp || \'value\']"  ng-repeat="option in to.options |'
              +'filter:searchTerm">{{option[to.labelProp || \'name\'] }}</md-option>'
          +'</md-optgroup>'
        +'</md-select>'
        +'</md-input-container>'

   });

Solution

  • I found the solution. I was trying to add a controller to fix this but that was not working. To make this work, I used the link option in formly. Here is the working code.

       formlyConfig.setType({
         name: 'select',
         template: '<md-input-container>'
            +'<label for="{{id + \'_\'+ $index}}">'
            +' {{to.label}}'
            +' </label>'
            + '<md-select ng-model="model[options.key]"'
                      + 'md-on-close="clearSearchTerm()"'
                      + 'data-md-container-class="selectdemoSelectHeader"'
                      + '>'
            +'<md-select-header class="demo-select-header">'
                +'<input ng-model="searchTerm"'
                       +'type="search"'
                       +'placeholder="Search"'
                       +'class="demo-header-searchbox md-text">'
              +'</md-select-header>'
              +'<md-optgroup >'
                +'<md-option ng-value="option[to.valueProp || \'value\']"  ng-repeat="option in to.options |'
                  +'filter:searchTerm">{{option[to.labelProp || \'name\'] }}</md-option>'
              +'</md-optgroup>'
            +'</md-select>'
            +'</md-input-container>'
            ,
            link: function(scope, el, attrs) {
                el.find('input').on('keydown', function(ev) {
              ev.stopPropagation();
          });
            }
    
       });