如何禁用墊下拉選項從一到二? (How to disable mat drop down option from one to two?)


問題描述

如何禁用墊下拉選項從一到二? (How to disable mat drop down option from one to two?)

TS 代碼:

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

/**
 * @title Basic use of `<table mat‑table>`
 */
@Component({
  selector: 'table‑basic‑example',
  styleUrls: ['table‑basic‑example.css'],
  templateUrl: 'table‑basic‑example.html',
})
export class TableBasicExample {
 timeSelection1='';
 timeSelection2=''; 

  resTimePeriodData= [
    "1",
    "2",
    "3",
    "4"
  ] 
}

HTML 代碼:

<div class="row">
    <div class="col‑md‑2 ">
        <!‑‑ DROP DOWN FOR CURRENT TIME PERIOD ‑‑>
        <mat‑form‑field>
            <mat‑select placeholder="Current Time Period" multiple 
 name="select1" [(ngModel)]="timeSelection1" (ngModelChange)="onchange()" >
                <mat‑option *ngFor="
 let time1 of resTimePeriodData
 " [value]="time1">{{ time1 }}</mat‑option>
            </mat‑select>
        </mat‑form‑field>
    </div>
</div>
<div class="row">
    <div class="col‑md‑2">
        <mat‑form‑field >
            <!‑‑ DROP DOWN FOR PREVIOUS TIME PERIOD ‑‑>
            <mat‑select  placeholder="Previous Time Period" multiple name="select2" [(ngModel)]="timeSelection2" >
                <mat‑option *ngFor="
  let time2 of resTimePeriodData
 " [value]="time2">{{ time2 }}</mat‑option>
            </mat‑select>
        </mat‑form‑field>
    </div>
</div>

如何禁用第一個選定元素以下拉兩個在下拉列表中選擇的元素。如果我在下拉菜單中選擇 3,然後在下拉菜單中選擇 2,如果我想從下拉菜單中選擇 2,我將被選中,並且兩個下拉菜單都具有相同的選項!

我的代碼試過 StackBlitz


參考解法

方法 1:

I would do something like this:

HTML:

<div class="row">
    <div class="col‑md‑2 ">
        <!‑‑ DROP DOWN FOR CURRENT TIME PERIOD ‑‑>
        <mat‑form‑field>
            <mat‑select [(value)]="selected" placeholder="Current Time Period" multiple name="select1" [(ngModel)]="timeSelection1">
                <mat‑option *ngFor="let time1 of resTimePeriodData" [value]="time1">{{ time1 }}</mat‑option>
            </mat‑select>
        </mat‑form‑field>
    </div>
</div>
<div class="row">
    <div class="col‑md‑2">
        <mat‑form‑field>
            <!‑‑ DROP DOWN FOR PREVIOUS TIME PERIOD ‑‑>
            <mat‑select placeholder="Previous Time Period" multiple name="select2" [(ngModel)]="timeSelection2">
                <mat‑option *ngFor="let time2 of resTimePeriodData" [value]="time2" [disabled]="isDisable(time2)">{{ time2 }}</mat‑option>
            </mat‑select>
        </mat‑form‑field>
    </div>
</div>

TS:

isDisable(obj) {
   var index = this.selected.indexOf(obj);
   if (index == ‑1) {
      return false;
   }
   else {
     return true;
   }
}

You can also try to improve the existing code!

Stackblitz

方法 2:

The value is being interpereted as a number. You can compare the time options in the disabled attribute using to string:

<mat‑option [disabled]="time2.toString() === timeSelection1.toString()" *ngFor="
  let time2 of resTimePeriodData
 " [value]="time2">{{ time2 }}</mat‑option>

I would recommend using numbers only though.

(by user10775755Prashant PimpaleDe Wet van As)

參考文件

  1. How to disable mat drop down option from one to two? (CC BY‑SA 2.5/3.0/4.0)

#angular5 #angular-material #TypeScript #Angular






相關問題

有沒有辦法通過在firebase angular 6中搜索數組類型值來查找記錄 (Is there way to find a record by searching in array type value in firebase angular 6)

將變量傳遞給 ng-container 元素 (Passing a variable to ng-container element)

如何減少使用 *ngFor 結構指令在表中顯示列表(從數據庫中獲取的 json 對象)的時間? (how to reduce time to display a list (json object fetched from database) in a table using *ngFor structural directive?)

在 type=text Ionic 的離子輸入中只允許數字 (Allow only number in a ion-input with type=text Ionic)

如何禁用墊下拉選項從一到二? (How to disable mat drop down option from one to two?)

錯誤消息在 ngx-datatable 中無法正常工作 (Error message not working properly in ngx-datatable)

如何讓 cli 為我的應用程序添加 e2e 文件夾? (How do I have the cli add an e2e folder for my app?)

mat-table中的Angular 5 onchange輸入文本字段 (Angular 5 onchange input text field in mat-table)

從 Angular 向 Node js API 發出 HTTP 請求的正確方法是什麼? (What is the correct way to make a HTTP request from Angular to Node js API?)

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

將 Angular 5 升級到 Angular 11 (Upgrade angular 5 to angular 11)

具有多維數組的Angular 2動態形式 (Angular 2 dynamic form with multidimensional array)







留言討論