Use the checkbox component to prompt the user for one or more Boolean (true/false) values.
In Responsive UI, we primarily use checkboxes for editing Boolean fields in a form, where the form is posted with a Save button. The user typically must take a separate action — i.e. pressing the Save button — for the checkbox state to take effect in the system.
Single Checkbox for a Form Field
When you have a single checkbox for a field in a form, place the field label in the left column and the checkbox input in the right column. This keeps all field labels on the left and all field value editors on the right, a standard previously established by the UI Committee. The checkbox displays without a label directly to the right of it since its field label is already displayed in the left column.
A field that displays a single checkbox, with the field label on the left and the checkbox on the right
Multiple Checkboxes for a Form Field
A field that allows the user to select multiple options may display multiple checkboxes in the field value column on the right. The field label displays in the left column, with the checkboxes displaying in the column on the right. In addition, each checkbox displays a separate label for that individual option to the right of the checkbox. ÇThis is the only time a checkbox displays a label to its immediate right in Responsive UI.
A field that displays multiple checkboxes, with the field label on the left, the checkboxes on the right, and a sub-label next to each checkbox
Bind this property to a backing value in your controller to set the initial value for the checkbox.
rui-value-change
event
This event fires when the checkbox’s value changes. The new value and checked status is passed in the event detail. Reference $event.detail.value or $event.detail.checked. Use this event to update the backing value in your controller.
labelText
string
null
If the toggle needs to be labeled, specify its label text with this property.
isChecked
boolean
false
Bind this property to true to initially check the checkbox.
isDisabled
boolean
false
Specifies whether the toggle is disabled.
name
string
null
Specify the name of the input with this property.
inputId
string
null
To apply a specific id to the input, use this property. Otherwise an id will be automatically generated, linking it to the label.
Implementation
Begin by importing the rui-input module into your application.
Import the module
// import into app.module
import'@jkhy/responsive-ui-wc/components/rui-input/rui-input-imports';
Single Checkbox Example
The single checkbox approach is used to set a true/false value for an individual property.
Define a field for the value in the controller, then add a rui-checkbox instance for the field in the HTML. Bind the value property to the appropriate field in the form and set the rui-value-change event to toggle the value when clicked.
Add a rui-checkbox, iterating through the array setting the labelText, value, isChecked and isDisabled statuses, and set the rui-value-change event to a backing function to handle the value change.
Checkbox HTML for multiple options
<rui-checkbox*ngFor="let accountType of accountTypeList"inputId="field-account-type-{{accountType.value}}"name="accountTypes"labelText="{{accountType.label}}"[value]="accountType.value"[isDisabled]="accountType.inactive"(rui-value-change)="accountTypeValChange($event)"[isChecked]="accountTypeCheckVal(accountType.value)"></rui-checkbox>
Back in the TypeScript, create the model value, and set up functions to handle rui-value-change and determine isChecked.
Bind this property to a backing value in your controller to set the initial value for the checkbox.
jhaCheckboxValChange
event
This event fires when the checkbox’s value changes. The new value is returned by the event. Use this event to update the backing value in your controller.
jhaLabel
string
null
If the toggle needs to be labeled, specify its label text with this property.
jhaIsChecked
boolean
false
Bind this property to true to initially check the checkbox.
jhaIsDisabled
boolean
false
Specifies whether the toggle is disabled.
jhaName
string
null
Specify the name of the input with this property.
jhaInputId
string
null
To apply a specific id to the input, use this property. Otherwise an id will be automatically generated, linking it to the label.
Implementation
Begin by importing the jha-input module into your application.
Import the module
// import into app.module
import{JhaFormsModule}from'@jkhy/responsive-ui-angular/jha-forms';@NgModule({imports:[...JhaFormsModule.forRoot(),...]})exportclassAppModule(){}
Single Checkbox Example
The single checkbox approach is used to set a true/false value for an individual property.
Define a field for the value in the controller, then add a jha-checkbox instance for the field in the HTML. Bind the ngModel property to the checkbox value.
Add a jha-checkbox, iterating through the array setting the jhaLabel, jhaCheckboxVal, jhaIsChecked and jhaIsDisabled statuses, and set the jhaCheckboxValChange event to a backing function to handle the value change.
Checkbox HTML for multiple options
<jha-checkbox*ngFor="let accountType of accountTypeList"jhaInputId="field-account-type-{{accountType.value}}"jhaName="accountTypes"[jhaLabel]="accountType.label"[jhaCheckboxVal]="accountType.value"(jhaCheckboxValChange)="accountTypeValChange(accountType.value)"[jhaIsChecked]="accountTypeCheckVal(accountType.value)"></jha-checkbox>
Back in the TypeScript, create the model value, and set up functions to handle jhaCheckboxValChange and determine jhaIsChecked.
If you’re using FormBuilder, send the $event and the element into the jhaCheckboxValChange bound method:
Checkbox HTML for multiple options
<jha-checkbox#jhaCheck*ngFor="let accountType of accountTypeList"jhaInputId="field-account-type-{{accountType.value}}"jhaName="accountTypes"[jhaLabel]="accountType.label"[jhaCheckboxVal]="accountType.value"(jhaCheckboxValChange)="accountTypeValChange(event,jhaCheck)"[jhaIsChecked]="accountTypeCheckVal(jhaCheck)"></jha-checkbox>
Then use the function to update the FormArray value: