1

I have used angular material in my application.Trying to disable the particular mat checkbox.I have tried but not working properly. It is disbaling all the checkbox.But i want to disable only Car and Motor checkboxes. How to do it? If anyone konws please help to find the solution.

mautocomponent.html:

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

mautocomponent.ts:

      isDisable(obj:any){
    //Only disable Car and Motor
    var index = this.rawData.indexOf(obj);
    console.log(index)
    if (index == -1) {
      return false;
    }
    else {
      return true;
    }

  }

Demo: https://stackblitz.com/edit/angular-ivy-6bvdt7?file=src%2Fapp%2Fshared%2Fmauto%2Fmauto.component.ts

EMahan K
  • 417
  • 1
  • 19

1 Answers1

1

You are so close. Why don't you just use the obj that you are passing to your isDisable() function.

isDisable(obj: any){
    console.log(obj)
    if (obj.item === 'Car' || obj.item === 'Motor') {
      return true;
    }
    else {
      return false;
    }

}

SB demo

OP asked about passing disable list from parent component (app.component.ts). One of the ways to do it would be, passing the list of disable items from parent to chiild.

app.component.ts:
optionsToHide = ['Car', 'Motor'];

app.component.html:
<app-mauto
  ...
  [optionsToHide]="optionsToHide"
  ...
>

mauto.component.ts:

isDisable(obj: any){

  if (this.optionsToHide.findIndex(item => item === obj.item) > -1) 
  {
    return true;
  }
  else {
    return false;
  }
}
Nehal
  • 13,130
  • 4
  • 43
  • 59
  • Can i do this from app.component.ts? I mean Can i pass the two values from app.component .ts to mauto.component.ts – EMahan K Jul 14 '22 at 12:53
  • Yes you can, but I don't have full context how you are deciding on hiding "Car" and "Motor". I have updated the SB to show you an example how you can pass it from app.component.ts – Nehal Jul 14 '22 at 13:04
  • Please answer this question. I am trying to update the selectOptions value inside post call method but it not working. Why? https://stackblitz.com/edit/angular-ivy-mwk6lp?file=src%2Fapp%2F%C3%A4ssets%2Fdata.json,src%2Fapp%2Fapp.component.ts – EMahan K Jul 14 '22 at 13:19
  • https://stackblitz.com/edit/angular-ivy-mwk6lp?file=src%2Fapp%2F%C3%A4ssets%2Fdata.json,src%2Fapp%2Fapp.module.ts – EMahan K Jul 14 '22 at 13:28
  • Please help to find the solutions. Thanks – EMahan K Jul 14 '22 at 13:29
  • Check the Network call, that API is throwing 403. `403 ERROR The request could not be satisfied.` – Nehal Jul 14 '22 at 13:31
  • How to check in stackblitz? – EMahan K Jul 14 '22 at 13:37
  • Open Dev Tools in Chrome > Click on Network Tab > make a small change in SB to make it re-run the code > Check the api call for https://angular-ivy-mwk6lp.stackblitz.io/assets/data.json (it will be in red with status code 403) – Nehal Jul 14 '22 at 13:44
  • I am asking how to resolve this issue? – EMahan K Jul 14 '22 at 13:49
  • Now i am getting status 200 error: https://stackblitz.com/edit/angular-ivy-gygjww?file=src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fapp.component.ts – EMahan K Jul 14 '22 at 13:53
  • But get method is working here: https://stackblitz.com/edit/angular-read-local-json-file?file=src%2Fapp%2Fread-json-file-using-httpclient%2Fread-json-file-using-httpclient.component.ts – EMahan K Jul 14 '22 at 13:54
  • How to find out that issue? – EMahan K Jul 14 '22 at 13:58
  • Post another question in SO, as this is not in the scope of your original question. – Nehal Jul 14 '22 at 13:59
  • New SO 73141228 – EMahan K Jul 28 '22 at 03:29
  • I think you can answer for that question – EMahan K Jul 28 '22 at 03:30