How do i implement autocomplete in a component?

后端 未结 3 1560
甜味超标
甜味超标 2020-12-16 00:37

I would like to combine the autocomplete feature with a multiselect mat-select chooser because the optionslist will be quite long.

I\'ve already searched on stackove

3条回答
  •  借酒劲吻你
    2020-12-16 01:06

    One way is to add a manual filter inside mat-select so it will have auto-complete functionality. Find the example code of below given solution here

    add a input controller for filter text

        public filterControl = new FormControl();
    

    add the filter text field inside mat-select

         
            
            
                {{option.name | translate}}
            
        
    

    now add a event listener for filter text field value change

         this.filteredOptions = this.filterControl.valueChanges.pipe(
                startWith(''),
                map((value: string) => {
                    this.optionItems
                        .forEach(option => {
                            option.show = option.name.toLocaleLowerCase().includes(value.toLowerCase());
                        });
                    return this.optionItems;
                })
            );
    

    also to make the whole list to be view in next dropdown open clear the filter text field on panel close, as mentioned in the template code (closed)="onPanelClose()"

     onPanelClose() {
        this.filterControl.setValue('');
    }
    

    Demo

提交回复
热议问题