Untitled conversation
- User
{ "day": "Tuseday", "hrFrom": "11:59 PM", "hrTo": "12:00 AM", "closed": true, "fullDay": false } i have this returned object and have this <input class="customRadio" type="radio" [name]="'status' + i" [value]="'closed'" [(ngModel)]="workingDays[i].closed" id="closed{{i}}"
/> <label [for]="'closed' + i">Closed</div> <div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'fullDay'" [(ngModel)]="workingDays[i].fullDay" id="fullDay{{i}}" /> <label [for]="'fullDay' + i">Full Day</label> </div> </div> </div> i want if fullday or closed for each day once one is true selected its radio box - Assistant
You can achieve this by adding a conditional check in your HTML template to disable the radio button that is not selected. Here's an example of how you can modify your code:
<div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'closed'" [(ngModel)]="workingDays[i].closed" id="closed{{i}" [disabled]="workingDays[i].fullDay" /> <label [for]="'closed' + i">Closed</label> </div> </div> </div> <div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'fullDay'" [(ngModel)]="workingDays[i].fullDay" id="fullDay{{i}}" [disabled]="workingDays[i].closed" /> <label [for]="'fullDay' + i">Full Day</label> </div> </div> </div>By adding the
[disabled]attribute to each radio button and binding it to the opposite condition, you can ensure that only one option can be selected at a time. IfworkingDays[i].fullDayis true, the "Closed" radio button will be disabled, and vice versa. - User
you cannot understand me now by my code i mentioned no radio is selected i want to select the fullday if returned by true from the mentiod object and same with false
- Assistant
I understand now. If you want to preselect the radio button based on the value returned from the object, you can set the
ngModelvalue for each radio button accordingly. Here's how you can modify your code to preselect the radio button based on the values returned:<div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'closed'" [(ngModel)]="workingDays[i].closed" id="closed{{i}" [checked]="!workingDays[i].fullDay" /> <label [for]="'closed' + i">Closed</label> </div> </div> </div> <div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'fullDay'" [(ngModel)]="workingDays[i].fullDay" id="fullDay{{i}}" [checked]="workingDays[i].fullDay" /> <label [for]="'fullDay' + i">Full Day</label> </div> </div> </div>By using the
[checked]attribute on the radio buttons, you can preselect the radio button based on the values returned from the object. IfworkingDays[i].fullDayis true, the "Full Day" radio button will be preselected, and if it is false, the "Closed" radio button will be preselected. - User
how make fullday checked if testdata = salma
- Assistant
If you want to check the "Full Day" radio button based on a condition where
testdatais equal to "salma", you can add a conditional statement in your component code to set thefullDayproperty ofworkingDays[i]to true if the condition is met. Here's an example:In your component.ts file:
// Assuming testdata is a variable in your component testdata: string = "salma"; if (this.testdata === "salma") { workingDays[i].fullDay = true; } else { workingDays[i].fullDay = false; }And in your HTML template:
<div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'closed'" [(ngModel)]="workingDays[i].closed" id="closed{{i}}" [checked]="!workingDays[i].fullDay" /> <label [for]="'closed' + i">Closed</label> </div> </div> </div> <div class="col-md-2 d-flex align-items-end"> <div class="form-group"> <div class="form-check"> <input class="customRadio" type="radio" [name]="'status' + i" [value]="'fullDay'" [(ngModel)]="workingDays[i].fullDay" id="fullDay{{i}}" [checked]="workingDays[i].fullDay" /> <label [for]="'fullDay' + i">Full Day</label> </div> </div> </div>With this setup, if
testdatais "salma", the "Full Day" radio button will be checked by default. Iftestdatais not "salma", the "Closed" radio button will be checked instead.