1
votes

I'm finding away to add mat select on my input field but some reason when I click on the arrow, it's not working and not showing me all the list.

I have autocomplete and usually when a user start typing it show all the list to select but I'm trying add mat-select also so that user can just click on the arrow and select from the list without having to type something first.

I've followed below stackblitz example ( checkbox is not needed for mine) but mine is little bit different and I also have text input and auto complete so I'm not sure why I can't type anymore in my input with current code and also the drop down list are not showing when I click the arrow.

any suggestion or help will be really apprecaited.

https://stackblitz.com/edit/angular-material-v9-mat-select-with-mat-chip-list?file=src%2Fapp%2Fselect-multiple-example.html

    <mat-form-field class="form" appearance="fill">
        <mat-label>Search or Select a Super Tag</mat-label>

        <mat-select [formControl]="superTags" multiple>
            <mat-select-trigger>
                <mat-chip-list #chipList>
                    <div>
                        <mat-chip *ngFor="let superTag of superTags" [selectable]="selectable" [removable]="removable"
                            (removed)="remove(superTag)">
                            <img class="super-icon" src="/assets/images/icons/super-tag-icon.svg">
                            {{superTag.tag}}
                            <mat-icon matChipRemove *ngIf="removable" matTooltip="Remove a super tag">cancel</mat-icon>
                        </mat-chip>
                    </div>
                    <div>
                        <input matInput  #input [(ngModel)]="tagIn" [formControl]="tagCtrl" [matAutocomplete]="auto" [matChipInputFor]="chipList"
                            [matChipInputSeparatorKeyCodes]="separatorKeysCodes" (matChipInputTokenEnd)="addTag()">

                    </div>
                </mat-chip-list>
                <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
                    <mat-option *ngIf="isLoading" class="is-Loading">
                        <mat-spinner diameter="20"></mat-spinner>
                    </mat-option>
                    <ng-container *ngIf="!isLoading">
                        <mat-option *ngFor="let tag of filteredSuperTags | async" [value]="tag">
                            {{tag}}
                        </mat-option>
                    </ng-container>
                </mat-autocomplete>
            </mat-select-trigger>
        </mat-select>
    </mat-form-field>

enter image description here

1

1 Answers

2
votes

In this case I would not use the combination of chip list and autocomplete.

I think, what you really need, is the autocomplete with options which contain checkboxes for the multiselect. Try to do, somethihg like this.

<mat-form-field class="example-full-width">
    <input type="text" placeholder="Select Users" aria-label="Select Users" matInput [matAutocomplete]="auto" [formControl]="userControl">
  <mat-hint>Enter text to find users by name</mat-hint>
</mat-form-field>

<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
    <mat-option *ngFor="let user of filteredUsers | async" [value]="selectedUsers">
        <div (click)="optionClicked($event, user)">
            <mat-checkbox [checked]="user.selected" (change)="toggleSelection(user)" (click)="$event.stopPropagation()">
                {{ user.firstname }} {{ user.lastname }}
            </mat-checkbox>
        </div>
    </mat-option>
</mat-autocomplete>

Full example:

https://stackblitz.com/edit/angular-sx79hu?embed=1&file=app/multiselect-autocomplete-example.html