1

In Oracle database there is such parameter "active" with data type "VARCHAR2 (1 byte)". The values ​​of Y and N are used there. I want to make the checkbox active if Y is on and not active N.

I wanted to do so but checkbox everywhere is active:

<table class="table table-hover">
  <thead>
    <tr>
      <th [class.active]="key === 'name'" (click)="setOrder('name')"><b>Name <span [hidden]="reverse">▼</span><span [hidden]="!reverse">▲</span></b></th>
      <th style="text-align: center;" [class.active]="key === 'active'" (click)="setOrder('active')"><b>File <span [hidden]="reverse">▼</span><span [hidden]="!reverse">▲</span> </b></th>
     </tr>
  </thead>
  <tbody>
    <tr *ngFor="let project of projects | orderBy: key:reverse | let projectIndex = index" >
      <td >{{project.name}}</td>
      <td style="text-align: center">
        <input type="checkbox" [checked]="project.active == 'Y'" class="material_checkbox" [(ngModel)]="project.active" name="active_{{projectIndex}}"  disabled />
      </td>
    </tr>
  </tbody>
</table>

How can I set a condition so that if there was a Y then the checkbox is active and if N then inactive?

4
  • 1
    Use value property as well value="{{project.active}}" Commented Jun 29, 2018 at 3:55
  • @Sanoj_V I used, but no changes not Commented Jun 29, 2018 at 4:01
  • In your case you need use boolean value instead of Y and N. Because when i uncheck one checkbox it returns false value here is example: (stackblitz.com/edit/angular-mx9v6p) you can see here. Commented Jun 29, 2018 at 5:28
  • Here is another example : (stackoverflow.com/a/44135418/9775003) even i am using boolean value for multiple checkbox in ngFor. Commented Jun 29, 2018 at 5:32

2 Answers 2

0

I assume this is your basic project type:

interface Project {
  name: string,
  active: string;

  // additional properties.
}

If yes, then:

 projectList: Project[];

  constructor() {
    this.populateProjectList();
  }

  populateProjectList(): void {
    this.projectList = [];
    this.projectList.push({name: 'project1', active: 'Y'});
    this.projectList.push({name: 'project2', active: 'N'});
    this.projectList.push({name: 'project3', active: 'Y'});
    this.projectList.push({name: 'project4', active: 'N'});
  }

// In your HTML

<p *ngFor="let project of projectList">
  <label for="">{{project.name}}</label>
  <input type="checkbox" [checked]="project.active == 'Y'"> 
</p>

You can play here for more options.

Sign up to request clarification or add additional context in comments.

Comments

0

I checked, As your [ngModel] is not boolean, you should remove it. Just use like below solution:-

<input type="checkbox" [checked]="project.active == 'Y'" class="material_checkbox"  name="active_{{projectIndex}}"  disabled />

11 Comments

It did not work, all the same, all checkboxes are active
All checkbox means? Do you have other checkboxes also?
there "name" property should not be the same
You mean the checkboxes of another table?
I mean if two check boxes have same "name" property then they will behave the same. "name" property of all checkboxes should be different from each other.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.