亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 7過濾器下拉值

Angular 7過濾器下拉值

桃花長相依 2022-05-26 15:27:39
我有兩個墊選擇下拉菜單。在第一個下拉菜單中,我的值類似于 a,b,c,d 在第二個下拉菜單中,我的值相同,例如 a,b,c,d要求當我選擇第一個下拉列表時,第一個下拉列表中的選定值不應出現在第二個下拉列表中,反之亦然。例如,我在第一個下拉列表中選擇了“b”值,而不是在第二個下拉列表中選擇了“b”不應該出現,只有 a、c、d 應該出現。https://stackblitz.com/edit/mat-select-filter-nayx2k?embed=1&file=src/app/app.component.html從上面的 stackblitz 我可以過濾值,但其他下拉值默認值被刪除。
查看完整描述

2 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

一種方法是根據您的要求構建模型。您目前有 2 個下拉菜單,但只有一個列表同時驅動它們。


相反,我會為每個選擇創建一個數組,并將所選值綁定到每個下拉列表的屬性。然后,當值更改時重建數組變得相當簡單。


<mat-form-field>

  <mat-select [(ngModel)]="selected1" (selectionChange)="change1()" placeholder="Custom placeholder">

    <mat-option *ngFor="let item of filtered1" [value]="item.id">

      {{item.name}}

    </mat-option>

  </mat-select>

</mat-form-field>


<br>


<mat-form-field>

  <mat-select [(ngModel)]="selected2" (selectionChange)="change2()" placeholder="Using array of objects">

    <mat-option *ngFor="let item of filtered2" [value]="item.id">

      {{item.name}}

    </mat-option>

  </mat-select>

</mat-form-field>

filtered1: {}[];

filtered2: {}[];

selected1: number;

selected2: number;


private options = [

  { 

    id: 0, 

    name: 'a' 

  }, 

  { 

    id: 1, 

    name: 'b' 

  },

  { 

    id: 2, 

    name: 'c' 

  },

  { 

    id: 3, 

    name: 'd' 

  }

];


ngOnInit() {

  this.selected1 = this.options[0].id;

  this.selected2 = this.options[1].id;


  this.change1();

  this.change2();

}


change1() {

  this.filtered2 = this.options.filter(x => x.id !== this.selected1);

}    


change2() {

  this.filtered1 = this.options.filter(x => x.id !== this.selected2);

}

演示:https ://stackblitz.com/edit/mat-select-filter-2usqw5


查看完整回答
反對 回復 2022-05-26
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

你可以試試以下


零件


import { Component } from '@angular/core';


@Component({

  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]

})

export class AppComponent  {

    dropdownOne: string;

    dropdownTwo: string;

    public variables2 = [

      { 

       id: 0, 

       name: 'a' 

      }, 

      { 

        id: 1, 

        name: 'b' 

      },

      { 

        id: 2, 

        name: 'c' 

      },

      { 

        id: 3, 

        name: 'd' 

      }

      ];



    public filteredList5 = this.variables2.slice();


    change(e){

    }

}


模板


<mat-form-field>

  <mat-select [(ngModel)]="dropdownOne" (selectionChange)="change($event)" placeholder="Custom placeholder">

    <ng-container *ngFor="let item of filteredList5">

      <ng-container *ngIf="item != dropdownTwo">

        <mat-option [value]="item">

          {{item.name}}

        </mat-option>

      </ng-container>

    </ng-container>

  </mat-select>

</mat-form-field>


<br>


<mat-form-field>

  <mat-select [(ngModel)]="dropdownTwo" (selectionChange)="change($event)" placeholder="Using array of objects">

    <ng-container *ngFor="let item of filteredList5">

      <ng-container *ngIf="item != dropdownOne">

        <mat-option [value]="item">

          {{item.name}}

        </mat-option>

      </ng-container>

    </ng-container>

  </mat-select>

</mat-form-field>

下拉列表中的值綁定到變量dropdownOne和dropdownTwo. 在選項循環中,在顯示選項之前檢查選項的值是否不等于另一個下拉列表的選定值。由于我們在顯示之前檢查項目,因此可以從更改事件處理程序中刪除過濾器。


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號