CheckboxCheckbox is an extension to standard checkbox element with skinning capabilities.

Checkbox - 图1

Documentation

Import

  1. import {CheckboxModule} from 'primeng/checkbox';
  2.  

Getting Started

Checkbox can either be used in multiple selection with other checkboxes or as a single checkbox to provide a boolean value.

Multiple Values

Multiple mode is enabled by default, ngModel property refers to an array to bind the selected values.

  1. <p-checkbox name="groupname" value="val1" [(ngModel)]="selectedValues"></p-checkbox>
  2. <p-checkbox name="groupname" value="val2" [(ngModel)]="selectedValues"></p-checkbox>
  3.  
  1. export class ModelComponent {
  2. selectedValues: string[] = [];
  3. }
  4.  

As ngModel is two-way binding enabled, prepopulating the model array with values is enough to display the related checkboxes as checked by default.

  1. export class ModelComponent {
  2. selectedValues: string[] = ['val1','val2'];
  3. }
  4.  

Label

The label attribute provides a label text for the checkbox. This label is also clickable and toggles the checked state.

  1. <p-checkbox name="groupname" value="val1" label="Value 1" [(ngModel)]="selectedValues"></p-checkbox>
  2. <p-checkbox name="groupname" value="val2" label="Value 2" [(ngModel)]="selectedValues"></p-checkbox>
  3.  

Boolean Value

A single boolean value can be bound using the ngModel property as well by enabling the binary option.

  1. export class ModelComponent {
  2. value: boolean;
  3. }
  4.  
  1. <p-checkbox [(ngModel)]="value" binary="true"></p-checkbox>
  2.  

Model Driven Forms

Checkbox can be used in a model driven form as well. In this case, due to an issue in Angular bind the formControl instance instead of using formControlName.

  1. <!-- Wrong -->
  2. <p-checkbox formControlName="cities"></p-checkbox>
  3. <!-- Correct -->
  4. <p-checkbox [formControl]="myFormGroup.controls['cities']"></p-checkbox>
  5.  

Properties

NameTypeDefaultDescription
namestringnullName of the checkbox group.
valueanynullValue of the checkbox.
labelstringnullLabel of the checkbox.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
binarybooleanfalseAllows to select a boolean value instead of multiple values.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
styleobjectnullInline style of the component.
styleClassstringnullStyle class of the component.
labelStyleClassstringnullStyle class of the label.
checkboxIconstringpi pi-checkIcon class of the checkbox icon.

Events

NameParametersDescription
onChangechecked: Boolean value to represent new state of checkbox.Callback to invoke on checkbox click.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-chkboxContainer element
ui-chkbox-boxContainer of icon.
ui-chkbox-iconIcon element.
ui-chkbox-labelLabel element.
ui-label-activeLabel element of a checked state.
ui-label-focusLabel element of a focused state.
ui-label-disabledLabel element of a disabled state.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <div class="ui-g" style="width:250px;margin-bottom:10px">
  3. <div class="ui-g-12"><p-checkbox name="group1" value="New York" label="New York" [(ngModel)]="selectedCities" inputId="ny"></p-checkbox></div>
  4. <div class="ui-g-12"><p-checkbox name="group1" value="San Francisco" label="San Francisco" [(ngModel)]="selectedCities" inputId="sf"></p-checkbox></div>
  5. <div class="ui-g-12"><p-checkbox name="group1" value="Los Angeles" label="Los Angeles" [(ngModel)]="selectedCities" inputId="la"></p-checkbox></div>
  6. </div>
  7. Selected Cities: <span *ngFor="let city of selectedCities" style="margin-right:10px">{{city}}</span>
  8. <h3>Preselection</h3>
  9. <div class="ui-g" style="width:250px;margin-bottom:10px">
  10. <div class="ui-g-12"><p-checkbox name="group2" value="Technology" label="Technology" [(ngModel)]="selectedCategories" inputId="technology"></p-checkbox></div>
  11. <div class="ui-g-12"><p-checkbox name="group2" value="Finance" label="Finance" [(ngModel)]="selectedCategories" inputId="finance"></p-checkbox></div>
  12. <div class="ui-g-12"><p-checkbox name="group2" value="Sports" label="Sports" [(ngModel)]="selectedCategories" inputId="sports"></p-checkbox></div>
  13. <div class="ui-g-12"><p-checkbox name="group2" value="Entertainment" label="Entertainment" [(ngModel)]="selectedCategories" inputId="entertainment"></p-checkbox></div>
  14. </div>
  15. Selected Categories: <span *ngFor="let cat of selectedCategories" style="margin-right:10px">{{cat}} </span>
  16. <h3>Boolean - {{checked}}</h3>
  17. <p-checkbox [(ngModel)]="checked" binary="true"></p-checkbox>
  18.  
  1. export class CheckboxDemo {
  2. selectedCities: string[] = [];
  3. selectedCategories: string[] = ['Technology', 'Sports'];
  4. checked: boolean = false;
  5. }
  6.