Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass all checked checkbox values on Form Submit in angular 4/6/7

I want to fetch all checked items of a form in component without using change() or click() function as it is unable to fetch already checked items.

Here is my Array in TS:

  PartyRoles = [
    {
     Id: 1,
     Name: "Vendor",
     Checked: true
    },
    {
      Id: 2,
      Name: "Client",
      Checked: true
    },
    {
      Id: 3,
      Name: "Consigner",
      Checked: false
    }       
  ]

My HTML Form:

<form (ngSubmit)="editPartyRolesSubmit()">
    <div *ngFor="let item of PartyRoles">
        <label>
        <input type="checkbox" value="{{item.Id}}" [attr.checked]="item.Checked==true ? true : null" [attr.disabled]="item.Id==1 ? true : null" />
        <span innerHTML="{{item.Name}}"></span>
       </label>
    </div>
</form>

My onSubmit function in which I want to get all the checked values:

  editPartyRolesSubmit= function () {
    // Please suggest how to fetch checked items
  }
like image 258
Adrita Sharma Avatar asked Aug 31 '17 07:08

Adrita Sharma


People also ask

How do I check if a checkbox is checked in angular 7?

Just define an ng-model directive in the checkbox and to find checkbox checked or not check model return value (TRUE or FALSE). If it has TRUE means checkbox is been checked.

How do you checked the checkbox in angular?

AngularJS ng-checked Directive The ng-checked directive sets the checked attribute of a checkbox or a radiobutton. The checkbox, or radiobutton, will be checked if the expression inside the ng-checked attribute returns true.


2 Answers

You could use Form and NgModel

<form #checkboxForm="ngForm" (submit)="editPartyRolesSubmit()">
    <div *ngFor="let item of PartyRoles">
        <label>
        <input type="checkbox" value="{{item.Id}}" [(ngModel)]="item.Checked" [name]="item.Name" [attr.disabled]="item.Id==1 ? true : null" />
        <span innerHTML="{{item.Name}}"></span>
    </label>
    </div>
    <input type="submit" value="Submit">
</form>

function can be

editPartyRolesSubmit() {
  console.log(this.PartyRoles);
}
like image 67
Eswar Avatar answered Nov 11 '22 01:11

Eswar


You can use this:

<form (ngSubmit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles; let i = index">
    <label>
    <input type="checkbox"
           [attr.checked]="item.Checked == true ? true : null"
           (change)="item.Checked = !item.Checked"
           [attr.disabled]="item.Id==1 ? true : null" />
    <span innerHTML="{{item.Name}}"></span>
   </label>
</div>
<button type="submit">Click</button>
</form>

Or:

<form (submit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles">
    <label>
    <input type="checkbox" value="{{item.Id}}" [(ngModel)]="item.Checked" [name]="item.Name" [attr.disabled]="item.Id==1 ? true : null" />
    <span innerHTML="{{item.Name}}"></span>
</label>
</div>
<button type="submit">Click</button>
</form>
like image 27
Aman Jain Avatar answered Nov 11 '22 02:11

Aman Jain