【问题标题】:The default behaviour of mat select changes when I add a search bar添加搜索栏时 mat select 的默认行为发生变化
【发布时间】:2020-09-23 09:19:54
【问题描述】:

所以下面是我的代码。我正在做的是在允许多选的下拉列表中显示所有可用设备,并且当设备计数超过 4 时我添加了一个搜索字段。 mat-select 的默认行为是当我选择一个选项并关闭时下拉菜单,当我重新打开它时,它会在所选选项上打开。但在这里它没有发生。它从头开始。所以我希望当我滚动并选择最后一个选项时,当我重新打开下拉菜单时,菜单应该滚动到足以显示所选选项的程度。 这个默认行为是否因为我添加了搜索栏而不起作用?

<form [formGroup]="gatewayForm" autocomplete="off">
                        <mat-form-field style="font-size:0.83em" class="no-arrow activity-log-field">
                            <mat-select formControlName="gatewayCtrl" multiple disableOptionCentering panelClass="form-panel gpanel" name="Gateway" (openedChange)="checkChanged($event,'gatewayList')" (selectionChange)="gatewaySelectedClick($event)">
                                <mat-select-trigger>
                                    {{selectedText}}
                                    <span *ngIf="extratext!=null" class="example-additional-selection">
                                        {{extratext}}
                                    </span>
                                </mat-select-trigger>
                                
                                <mat-form-field *ngIf="gateways && gateways.length>4" class="organistation-searchField" appearance="outline"  [floatLabel]="'never'" aria-autocomplete="off" style="padding: 5px 8px 5px 8px;font-size: 13px">
                                    <input #search autocomplete="off" formControlName="searchTerm" (keyup)="filterDevices($event.target.value,'gatewayList','gpanel');" (keydown)="$event.stopPropagation()" matInput placeholder="Search Device">
                                    <mat-icon matSuffix style="color:#24539f;position: relative;top: 0.1em;">search</mat-icon>
                                </mat-form-field>
                                <div class="panel-optn" style="max-height: 170px;">
                                    <mat-option *ngIf="gateways && gateways.length>1" (click)="toggleAllSelection()" #allSelected class="gatewayList" [value]="allgateways">All devices</mat-option>
                                    <mat-option (click)="tosslePerOne(allSelected!=null?allSelected.viewValue:null)" class="gatewayList" *ngFor="let gateway of gateways" [value]="gateway">
                                        {{ gateway.panel_name }}
                                    </mat-option>
                                </div>
                            </mat-select>
                        </mat-form-field>
                    </form>

【问题讨论】:

    标签: javascript angular angular-material


    【解决方案1】:
    this.yourElementHere.focus(); 
    

    这应该将下拉菜单的焦点放在您选择的元素上。

    包括您选择的选项的值,例如

    this.yourElementHere.focus(selectedOption);
    

    【讨论】:

      【解决方案2】:

      所以问题是由 div 标签内的 mat-option 引起的。我删除了它并通过 panelClass 直接在 mat-select 上应用了样式。

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        相关资源
        最近更新 更多