Var
Var

Reputation: 270

Angular - Multiple Radio buttons in reactive forms

We have list like below ( in original implementation productList will come from API )

productList = [{
    productId: "104",
    productName: "computer"
  },
  {
    productId: "105",
    productName: "Sprots"
  },
  {
    productId: "106",
    productName: "TV"
  }];

we need to display product name as radio buttons in html, something like this.

[] Computer
[] Sports
[] TV

and when we select one radio button it should give selected item in component ( for example if we select TV radio button then, may be something like this ?

this.myForm.controls['productMethods'].valueChanges.subscribe(value => {
      var a  = value 
      // how can i get a = { productId: "106",productName: "TV"} **?**
    }

for pre selected :-

[] Computer
[x] Sports <== how to make sports as pre-selected on page load also **?**
[] TV

what i have tried is https://stackblitz.com/edit/angular-formarray-example-2-3xq45k but failed to display radio buttons.

Thanks,

Upvotes: 2

Views: 20336

Answers (3)

EMMANUEL OKELLO
EMMANUEL OKELLO

Reputation: 178

A little bit late but let me leave this here. angular material has mat-radio-group, in your module, import {MatRadiomodule} from '@angular/material' also, import it inside @NgModule.

In your component.

this.formGroupVariable = this.fb.group({
    productItem: ['', Validators.required],
    . . .
});

Then in your template component,

<mat-radio-group *ngFor="let productItem of productList">
    <label for="{{productItem.value}}" class="radio-inline">
        <input id="{{productItem.value}}" 
               [value]="productItem.value"
               formControlName="productItem"                                          
               type="radio"
               name="requestType"
               [checked]="productItem.value==='Sports'">
               {{productItem.value}}
    </label>
</mat-radio-group>

I hope this helps.

Upvotes: 3

theblindprophet
theblindprophet

Reputation: 7927

Here is an updated version of your Stackblitz.

First, since you are using radio inputs, you are only going to receive a single value, not an array.

this.productFormGroup = this.formBuilder.group({
  productMethod: new FormControl("105"),
});

Second, your radio inputs need to all have the same name (that name needs to match the formControlName).

<input type="radio"
  formControlName="productMethod"
  id="radio{{i}}"
  name="productMethod"
  [value]=item>
<label class="custom-control-label"
  for="radio{{i}}">
  {{item.productName || '?'}}
</label>

Third, I wouldn't use an object as a form value because you cannot compare objects (easily) to be able to set a default. Use the productId and use a method to retrieve the corresponding product.

getProductById(id): any {
  return this.productList.find(p => p.productId == id);
}

Finally, I made other small changes to make your code a little better.

<div [formGroup]="productFormGroup">
  <div *ngFor="let item of productList; let i = index;">
    <input type="radio"
      formControlName="productMethod"
      id="radio{{i}}"
      name="productMethod"
      [value]=item.productId>
    <label class="custom-control-label"
      for="radio{{i}}">
      {{item.productName || '?'}}
    </label>
  </div>
</div>
{{ getProductById(productFormGroup.controls.productMethod.value) | json }}

Upvotes: 3

Eliseo
Eliseo

Reputation: 57939

1.-You need write "formArrayName"

2.-formControlName is enclosed by []

3.-radio buttons??? If you want radio buttons it's only a formControlName, not an array

<div [formGroup]="productFormGroup">
    <!---you need write "formArrayName"--->
    <div formArrayName="productMethods">
        <div *ngFor="let item of productList; let i = index">
            <div>
                <!--formControlName is enclosed by []-->
                <input type="checkbox" name="{{'acceptable'+i}}" [formControlName]="i">
                <label class="custom-control-label" 
                    for="{{ 'acceptable' + i}}">{{item.productName}}
               </label>
           </div>
        </div>
    </div>
</div>

this.myForm.controls['productMethods'].valueChanges.subscribe(value => {
      //value becomes, e.g. [true,false,false]
      values.forEach((v,index)=>{
         if (v)
           console.log(productList[i]);
      })
    }

NOTE: If you want radio buttons

<div [formGroup]="productFormGroup">
    <div *ngFor="let item of productList; let i = index" >
        <input type="radio" name="product" formControlName="product" [value]="item" >
    <label class="custom-control-label" 
        for="{{ 'acceptable' + i}}">{{item.productName}}</label>
    {{item.productname}}
  </div>
</div>

But your form is like

this.productFormGroup = this.formBuilder.group({
      product:''
    });

Upvotes: 0

Related Questions