Angular: radio button [checked] not set with template reference variable - html

As the title suggests, I have a scenario where, if the "Select All" radio button is checked, it must check all of the radio buttons in the columns below. Unfortunately, this is not working.
Here is a sample:
<table>
<thead>
<th>Role</th>
<th colspan="3">Access</th>
</thead>
<tbody>
<tr>
<td>Select All</td>
<td>
<input #none type="radio" name="access0" value="allNone"/>
<label>None</label>
</td>
<td>
<input #readOnly type="radio" name="access0" value="allReadOnly"/>
<label>Read Only</label>
</td>
<td>
<input #full type="radio" name="access0" value="AllFull"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Admin</td>
<td>
<input type="radio" name="access1" value="None" [checked]="none.checked"/>
<label>None</label>
</td>
<td>
<input type="radio" name="access1" value="ReadOnly" [checked]="readOnly.checked"/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access1" value="Access" [checked]="full.checked"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Sales Person</td>
<td>
<input type="radio" name="access2" value="None" [checked]="none.checked"/>
<label>None</label>
</td>
<td>
<input type="radio" name="access2" value="ReadOnly" [checked]="readOnly.checked"/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access2" value="Access" [checked]="full.checked"/>
<label>Full</label>
</td>
</tr>
</tbody>
</table>
And here is a link to a sample StackBlitz.
I am not sure why, for instance, all of the 'None' radio buttons are not checked when setting the [checked] as follow:
<input type="radio" name="access1" value="None" [checked]="none.checked"/>

disclamer it's not an answer, is an answer to a comment about use [(ngModel)] into a ReactiveForm.
#monstertjie_za, the doc indicate that you don't use in the same input TOGETHER formControlName and [(ngModel)], Not that you can not use a input into a reactive form. Imagine your example. You has a reactiveForm like
form=new FormGroup({
accessAdmin:new FormControl(),
accessPersonal:new FormControl()
})
But you want allow the user a rapid selection
<form [formGroup]="form">
<!--a input that not belong to the ReactiveForm that use [(ngModel)]-->
<div>
<label><input type="radio" value="None" [ngModelOptions]="{standalone:true}"
[ngModel]="access" (ngModelChange)="change($event)"/>None</label>
<label><input type="radio" value="ReadOnly" [ngModelOptions]="{standalone:true}"
[ngModel]="access" (ngModelChange)="change($event)"/>ReadOnly</label>
<label><input type="radio" value="Access" [ngModelOptions]="{standalone:true}"
[ngModel]="access" (ngModelChange)="change($event)"/>Access</label>
</div>
<!--inputs that belong to our formGroup-->
<div>
<label><input type="radio" value="None" formControlName="accessAdmin"/>None</label>
<label><input type="radio" value="ReadOnly" formControlName="accessAdmin"/>ReadOnly</label>
<label><input type="radio" value="Access" formControlName="accessAdmin"/>Access</label>
</div>
<div>
<label><input type="radio" value="None" formControlName="accessPersonal"/>None</label>
<label><input type="radio" value="ReadOnly" formControlName="accessPersonal"/>ReadOnly</label>
<label><input type="radio" value="Access" formControlName="accessPersonal"/>Access</label>
</div>
</form>
<pre>
{{form?.value|json}}
</pre>
where you has a function like
change(value) {
this.form.get('accessAdmin').setValue(value);
this.form.get('accessPersonal').setValue(value);
}
You can see the stackblitz demo
You see that we use a input with [(ngModel)] to help the user to change the value of form.accessAdmin and form.accessPersonal. It is not related with the link you show me and is perfectly formed -even I'll say that it's good to help the user-

Check for changes:
<table (click)="cd.detectChanges()">
<thead>
<th>Role</th>
<th colspan="3">Access</th>
</thead>
<tbody>
<tr>
<td>Select All</td>
<td>
<input #none type="radio" name="access0" value="allNone"/>
<label>None</label>
</td>
<td>
<input #readOnly type="radio" name="access0" value="allReadOnly"/>
<label>Read Only</label>
</td>
<td>
<input #full type="radio" name="access0" value="AllFull"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Admin</td>
<td>
<input type="radio" name="access1" [value]="None" [checked]="none.checked"/>
<label>None</label>
</td>
<td>
<input type="radio" name="access1" value="ReadOnly" [checked]="readOnly.checked"/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access1" value="Access" [checked]="full.checked"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Sales Person</td>
<td>
<input type="radio" name="access2" value="None" [checked]="none.checked"/>
<label>None</label>
</td>
<td>
<input type="radio" name="access2" value="ReadOnly" [checked]="readOnly.checked"/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access2" value="Access" [checked]="full.checked"/>
<label>Full</label>
</td>
</tr>
</tbody>
</table>
And TS:
import {ChangeDetectorRef, Component} from '#angular/core';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(protected cd: ChangeDetectorRef){}
}

you have to use ngModel to update the radio button values like this -
app.component.html
<table>
<thead>
<th>Role</th>
<th colspan="3">Access</th>
</thead>
<tbody>
<tr>
<td>Select All</td>
<td>
<input type="radio" name="access0" value="allNone"
[(ngModel)]='none'/>
<label>None</label>
</td>
<td>
<input [(ngModel)]='readonly' type="radio" name="access0" value="allReadOnly"/>
<label>Read Only</label>
</td>
<td>
<input [(ngModel)]='full' type="radio" name="access0" value="AllFull"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Admin</td>
<td>
<input type="radio" name="access1" [value]="1" [checked]='none'/>
<label>None</label>
</td>
<td>
<input type="radio" name="access1" value="ReadOnly" [checked]='readonly'/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access1" value="Access" [checked]="full"/>
<label>Full</label>
</td>
</tr>
<tr>
<td>Sales Person</td>
<td>
<input type="radio" name="access2" value="None" [checked]="none"/>
<label>None</label>
</td>
<td>
<input type="radio" name="access2" value="ReadOnly" [checked]="readonly"/>
<label>Read Only</label>
</td>
<td>
<input type="radio" name="access2" value="Access" [checked]="full"/>
<label>Full</label>
</td>
</tr>
</tbody>
</table>
app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
public none=false;
public readonly=false;
public full=false;
}

Related

Seperate radio buttons on table rows

I am dealing with a really weird error, this is my table:
But if i select something in the first row, and if i want to select something in the second row, then my selection in the first row goes away. so basically i need each row to be a separate radio group. Here is my code:
<table class="table table-bordered table-striped infoTable">
<tbody>
<tr>
<td>Ownership</td>
<td>
<input name="Test" type="radio" value="A" id="Same" />
</td>
<td>
<input name="Test" type="radio" value="B" id="Same1" />
</td>
<td>
<input name="Test" type="radio" value="C" id="Same2"/>
</td>
<td>
<input name="Test" type="radio" value="D" id="Same3"/>
</td>
</tr>
<tr>
<td>Management Control - Board Participation
</td>
<td>
<input name="Test" type="radio" value="A" id="Same4"/>
</td>
<td>
<input name="Test" type="radio" value="B" id="Same5"/>
</td>
<td>
<input name="Test" type="radio" value="C" id="Same6"/>
</td>
<td>
<input name="Test" type="radio" value="D" id="Same7"/>
</td>
</tr>
<tr>
<td>Management Control - Employee Structure
</td>
<td>
<label><input name="Test" type="radio" value="A" /></label>
</td>
<td>
<label><input name="Test" type="radio" value="B" /></label>
</td>
<td>
<label><input name="Test" type="radio" value="C" /></label>
</td>
<td>
<label><input name="Test" type="radio" value="D" /></label>
</td>
</tr>
You have only to change the name of inputs, like that.
<tbody>
<tr>
<td>Ownership</td>
<td>
<input name="Test" type="radio" value="A" id="Same" />
</td>
<td>
<input name="Test" type="radio" value="B" id="Same1" />
</td>
<td>
<input name="Test" type="radio" value="C" id="Same2"/>
</td>
<td>
<input name="Test" type="radio" value="D" id="Same3"/>
</td>
</tr>
<tr>
<td>Management Control - Board Participation
</td>
<td>
<input name="Tes" type="radio" value="A" id="Same4"/>
</td>
<td>
<input name="Tes" type="radio" value="B" id="Same5"/>
</td>
<td>
<input name="Tes" type="radio" value="C" id="Same6"/>
</td>
<td>
<input name="Tes" type="radio" value="D" id="Same7"/>
</td>
</tr>
<tr>
<td>Management Control - Employee Structure
</td>
<td>
<label><input name="Te" type="radio" value="A" /></label>
</td>
<td>
<label><input name="Te" type="radio" value="B" /></label>
</td>
<td>
<label><input name="Te" type="radio" value="C" /></label>
</td>
<td>
<label><input name="Te" type="radio" value="D" /></label>
</td>
</tr>
You have to keep only name different for different set of radio button.
<table class="table table-bordered table-striped infoTable">
<tbody>
<tr>
<td>Ownership</td>
<td>
<input name="Test" type="radio" value="A" id="Same" />
</td>
<td>
<input name="Test" type="radio" value="B" id="Same1" />
</td>
<td>
<input name="Test" type="radio" value="C" id="Same2" />
</td>
<td>
<input name="Test" type="radio" value="D" id="Same3" />
</td>
</tr>
<tr>
<td>Management Control - Board Participation
</td>
<td>
<input name="Test_1" type="radio" value="A" id="Same4" />
</td>
<td>
<input name="Test_1" type="radio" value="B" id="Same5" />
</td>
<td>
<input name="Test_1" type="radio" value="C" id="Same6" />
</td>
<td>
<input name="Test_1" type="radio" value="D" id="Same7" />
</td>
</tr>
<tr>
<td>Management Control - Employee Structure
</td>
<td>
<label><input name="Test_2" type="radio" value="A" /></label>
</td>
<td>
<label><input name="Test_2" type="radio" value="B" /></label>
</td>
<td>
<label><input name="Test_2" type="radio" value="C" /></label>
</td>
<td>
<label><input name="Test_2" type="radio" value="D" /></label>
</td>
</tr>

How to create two sets of radio buttons in one line in an html table

I have a table in an angular app where I have 4 radio buttons in one line however I would like to separate them into two groups of two radio buttons, so that a user can choose an option yes/ no in painful section and yes/no in non-painful section. How could I do that? My code is below:
<div>
<table>
<thead>
<th></th>
<th></th>
<th colspan="2">Non Painful</th>
<th colspan="2">Painful</th>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td>No</td>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>1.1</td>
<td>Moaning</td>
<td>
<div class="form-check">
<input class="form-check-input" type="radio" name="q1" id="q1.1-no" value="No" [(ngModel)]="survey.moaningNonPainful">
</div>
</td>
<td>
<div class="form-check">
<input class="form-check-input" type="radio" name="q1" id="q1.1-yes" value="Yes" [(ngModel)]="survey.moaningNonPainful">
</div>
</td>
<td>
<div class="form-check">
<input class="form-check-input" type="radio" name="q1" id="q1.2-No" value="No" [(ngModel)]="survey.moaningPainful">
</div>
</td>
<td>
<div class="form-check">
<input class="form-check-input" type="radio" name="q1" id="q1.2-Yes" value="Yes" [(ngModel)]="survey.moaningPainful">
</div>
</td>
</tr>
</tbody>
</table>
</div>

input radio button vertical and horizontal

I want a form where someone choose a unique value of radio buttons vertical and horizontal (you'll see below what i mean). I know tha i can do it with name. In my code below you see same name in vertical.
Here's my code
<tr>
<td style="text-align:left;">tracking_url</td>
<td><input type="radio" name="tracking_url" value="image_url"></td>
<td><input type="radio" name="pr_image" value="tracking_url"></td>
<td><input type="radio" name="availability" value="tracking_url"></td>
<td><input type="radio" name="pr_price" value="tracking_url"></td>
<td><input type="radio" name="pr_final_price" value="tracking_url"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">image_url</td>
<td><input type="radio" name="tracking_url" value="image_url"></td>
<td><input type="radio" name="pr_image" value="image_url"></td>
<td><input type="radio" name="availability" value="image_url"></td>
<td><input type="radio" name="pr_price" value="image_url"></td>
<td><input type="radio" name="pr_final_price" value="image_url"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">availability</td>
<td><input type="radio" name="tracking_url" value="availability"></td>
<td><input type="radio" name="pr_image" value="availability"></td>
<td><input type="radio" name="availability" value="availability"></td>
<td><input type="radio" name="pr_price" value="availability"></td>
<td><input type="radio" name="pr_final_price" value="availability"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">price</td>
<td><input type="radio" name="tracking_url" value="price"></td>
<td><input type="radio" name="pr_image" value="price"></td>
<td><input type="radio" name="availability" value="price"></td>
<td><input type="radio" name="pr_price" value="price"></td>
<td><input type="radio" name="pr_final_price" value="price"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">full_price</td>
<td><input type="radio" name="tracking_url" value="full_price"></td>
<td><input type="radio" name="pr_image" value="full_price"></td>
<td><input type="radio" name="availability" value="full_price"></td>
<td><input type="radio" name="pr_price" value="full_price"></td>
<td><input type="radio" name="pr_final_price" value="full_price"></td>
<td></td>
</tr>
This is how i want to use radio buttons
but also someone can use radio buttons like
Is there any way to do it with html or javascript??
var col, el;
$("input[type=radio]").click(function() {
el = $(this);
col = el.data("col");
$("input[data-col=" + col + "]").prop("checked", false);
el.prop("checked", true);
});
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #ccc;
padding: 10px;
}
th:empty {
border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<table>
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<tr>
<td>Twix</td>
<td><input type="radio" name="row-1" data-col="1"></td>
<td><input type="radio" name="row-1" data-col="2"></td>
<td><input type="radio" name="row-1" data-col="3"></td>
</tr>
<tr>
<td>Snickers</td>
<td><input type="radio" name="row-2" data-col="1"></td>
<td><input type="radio" name="row-2" data-col="2"></td>
<td><input type="radio" name="row-2" data-col="3"></td>
</tr>
<tr>
<td>Butterfingers</td>
<td><input type="radio" name="row-3" data-col="1"></td>
<td><input type="radio" name="row-3" data-col="2"></td>
<td><input type="radio" name="row-3" data-col="3"></td>
</tr>
</table>
https://css-tricks.com/radio-buttons-with-2-way-exclusivity/
The radio buttons within a row need to have the same name="nameValue" value. The value fields can be different for each radio button.
I think it would be better to use checkboxes. And control if other checkboxes could be checked or not using javascript.
Here I implemented in Angular 7 input radio button vertically and horizontal
The Idea behind this is that with radio button we have name property which makes single select either by row or column but for both row and column. I placed the row in name and handled column with javascript whenever button will click.
This is HTML CODE
<section class="editor">
<strong>Editor</strong>
<div>
<label>Add option</label>
<button (click)="addOption()">+</button>
<div *ngFor="let option of options;let i = index">
<span>{{option.title +(i+1)}}</span><button (click)="deleteOption(i)"
*ngIf="options.length>2">X</button>
</div>
</div>
</section>
<hr>
<section>
<strong>Builder</strong>
<div class="builder">
<label>Question title goes here</label><br>
<div *ngFor="let option of options;let row_index = index">
<span>{{option.title +(row_index+1)}}</span>
<span *ngFor="let rank of options;let column_index=index">
<input type="radio" name="{{row_index}}" class="{{column_index}}"
(click)="check(column_index,$event)">
</span>
</div>
</div>
</section>
And this is my .ts file code where I Implemented with Javascript
export class AppComponent {
options = [{
title: "option",
rank: 0,
}, {
title: "option",
rank: 0
}]
constructor() {
}
ngOnInit(): void { }
addOption() {
this.options.push({
title: "option",
rank: 0
})
}
deleteOption(i) {
this.options.splice(i, 1);
}
check(column_index, event) {
Array.from(document.getElementsByClassName(column_index)).map(x=>x['checked']=false);
event.target.checked=true;
}
}

radio button doesn't show the bullet

I have a page with radio button that doesn't show the bullet only for same row (name='197' and value='_ for example') DEMO
E.g.
this is the code for the line
<tr>
<td class="generic">197</td>
<td class="generic">PARASPRUZZI RUOTE POSTERIORI</td>
<td>
</td>
<td align="center">
<input type="radio" value="S" name="197" userselection="1">
</td>
<td align="center">
<input type="radio" value="_" name="197" checked="">
</td>
<td align="center">
<input type="radio" value="N" name="197" disabled="">
</td>
</tr>
I don't see where is the mistakes, can you help me?
Because further on down your code (some line ~4579) you have another set of <input>s with the same name 197. One of which has the checked attribute:
<tr>
<td class="Option_S">CMBDS</td>
<td></td>
<td class="Option_S">DIESEL</td>
<td align="center">
<input type="radio" value="S" name="197" checked="">
</td>
<td align="center">
<input type="radio" value="_" name="197" disabled="">
</td>
<td align="center">
<input type="radio" value="N" name="197" disabled="">
</td>
</tr>
JSFiddle
You have others radio with name 197 in html:
<tr>
<td class="Option_S">CMBDS</td>
<td></td>
<td class="Option_S">DIESEL</td>
<td align="center">
<input type="radio" value="S" name="197" checked="">
</td>
<td align="center">
<input type="radio" value="_" name="197" disabled="">
</td>
<td align="center">
<input type="radio" value="N" name="197" disabled="">
</td>
</tr>

Why am i able to check more than 1 radio button in this code

<body>
<form name="search_form" id="search_form" method="POST" action="search_user_data.php">
<table border="1">
<tr>
<td colspan="2">
<input type="text" id="search" name="search" />
</td>
<td>
<input type="submit" value="Search"/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="id_radio"/>ID
</td>
<td>
<input type="radio" name="surname_radio"/>Surname
</td>
<td>
<input type="radio" name="dob_radio"/>DoB
</td>
</tr>
</table>
</form>
</body>
Because name attribute in radio button must be the same in radio group.
Try this:
<input type="radio" name="somename" value="id_radio"/>ID
<input type="radio" name="somename" value="surname_radio"/>Surname
<input type="radio" name="somename" value="dob_radio"/>DoB
More info at w3c
The name attribute is what links radio buttons into a group. Use value for the actual value of each button.
<td>
<input type="radio" value="id_radio" name="btn_group"/>ID
</td>
<td>
<input type="radio" value="surname_radio" name="btn_group"/>Surname
</td>
<td>
<input type="radio" value="dob_radio" name="btn_group"/>DoB
</td>
It is because they all have a different name. Give them 1 name and they will act as a group:
<input type="radio" name="radioGroup" value='id'/>ID
<input type="radio" name="radioGroup" value='surname'/>Surname
<input type="radio" name="radioGroup" value='dob'/>DoB
http://www.echoecho.com/htmlforms10.htm
You have to have the same name for all of the radio inputs.
Your problem is that you have different names for each radio button in order for them to group together, they must have the same name, it looks like you are confusing name and value.
<body>
<form name="search_form" id="search_form" method="POST" action="search_user_data.php">
<table border="1">
<tr>
<td colspan="2">
<input type="text" id="search" name="search" />
</td>
<td>
<input type="submit" value="Search"/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="searchType" value="id_radio"/>ID
</td>
<td>
<input type="radio" name="searchType" value="surname_radio"/>Surname
</td>
<td>
<input type="radio" name="searchType" value="dob_radio"/>DoB
</td>
</tr>
</table>
</form>
</body>