In single mode, a row is selected on click event of a row. If the row is already selected then the row gets unselected.
In multiple mode, selection binding should be an array. For touch enabled devices, selection is managed by tapping and for other devices metakey or shiftkey are required.
DataTable provides onRowSelect and onRowUnselect as optional callbacks.
Single selection can also be handled using radio buttons by enabling the selectionMode property of column as "single".
Multiple selection can also be handled using checkboxes by enabling the selectionMode property of column as "multiple".
When using checkboxes for multiple selection with paging and clicking the header checkbox, by default, items on all other pages will be deselected and only the current page's items will be toggled. Often, the more desired approach is to toggle all items across all pages. This can be accomplished by setting the "headerCheckboxToggleAllPages" property to "true" on your paged DataTable. Notice how the header checkbox only stays checked when all items across all pages are selected.
export class DataTableSelectionDemo implements OnInit {
msgs: Message[];
cars: Car[];
selectedCar1: Car;
selectedCar2: Car;
selectedCar3: Car;
selectedCars1: Car[];
selectedCars2: Car[];
selectedCars3: Car[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsSmall().then(cars => this.cars = cars);
}
onRowSelect(event) {
this.msgs = [];
this.msgs.push({severity: 'info', summary: 'Car Selected', detail: event.data.vin + ' - ' + event.data.brand});
}
onRowUnselect(event) {
this.msgs = [];
this.msgs.push({severity: 'info', summary: 'Car Unselected', detail: event.data.vin + ' - ' + event.data.brand});
}
}
<p-growl [value]="msgs"></p-growl>
<h3 class="first">Single</h3>
<p>In single mode, a row is selected on click event of a row. If the row is already selected then the row gets unselected.</p>
<p-dataTable [value]="cars" selectionMode="single" [(selection)]="selectedCar1" dataKey="vin">
<p-header>Single Selection</p-header>
<p-footer><div style="text-align: left">Selected Car: {{selectedCar1 ? selectedCar1.vin + ' - ' + selectedCar1.brand + ' - ' + selectedCar1.year + ' - ' + selectedCar1.color: 'none'}}</div></p-footer>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
</p-dataTable>
<h3>Multiple</h3>
<p></p>
<p-dataTable [value]="cars" selectionMode="multiple" [(selection)]="selectedCars" dataKey="vin">
<p-header>Multiple Selection</p-header>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
<p-footer>
<ul>
<li *ngFor="let car of selectedCars" style="text-align: left">{{car.vin + ' - ' + car.brand + ' - ' + car.year + ' - ' + car.color}}</li>
</ul>
</p-footer>
</p-dataTable>
<h3>Events</h3>
<p>DataTable provides onRowSelect and onRowUnselect as optional callbacks.</p>
<p-dataTable [value]="cars" selectionMode="single" [(selection)]="selectedCar2" (onRowSelect)="onRowSelect($event)" (onRowUnselect)="onRowUnselect($event)" dataKey="vin">
<p-header>Selection with Events</p-header>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
</p-dataTable>
<h3>RadioButton</h3>
<p>Single selection can also be handled using radio buttons by enabling the selectionMode property of column as "single".</p>
<p-dataTable [value]="cars" [(selection)]="selectedCar3" dataKey="vin">
<p-header>Single Selection</p-header>
<p-footer><div style="text-align: left">Selected Car: {{selectedCar3 ? selectedCar3.vin + ' - ' + selectedCar3.brand + ' - ' + selectedCar3.year + ' - ' + selectedCar3.color: 'none'}}</div></p-footer>
<p-column [style]="{'width':'38px'}" selectionMode="single"></p-column>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
</p-dataTable>
<h3>Checkbox</h3>
<p>Multiple selection can also be handled using checkboxes by enabling the selectionMode property of column as "multiple".</p>
<p-dataTable [value]="cars" [(selection)]="selectedCars2" dataKey="vin">
<p-header>Checkbox Selection</p-header>
<p-column [style]="{'width':'38px'}" selectionMode="multiple"></p-column>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
<p-footer>
<ul>
<li *ngFor="let car of selectedCars2" style="text-align: left">{{car.vin + ' - ' + car.brand + ' - ' + car.year + ' - ' + car.color}}</li>
</ul>
</p-footer>
</p-dataTable>
<h3>Checkbox with Paging</h3>
<p>
When using checkboxes for multiple selection with paging and clicking the header checkbox, by default, items on all
other pages will be deselected and only the current page's items will be toggled. Often, the more desired approach
is to toggle all items across all pages. This can be accomplished by setting the
"headerCheckboxToggleAllPages" property to "true" on your paged DataTable. Notice how the header
checkbox only stays checked when all items across all pages are selected.
</p>
<p-dataTable [value]="cars" [(selection)]="selectedCars3" dataKey="vin" [paginator]="true" [rows]="5" [headerCheckboxToggleAllPages]="true">
<p-header>Checkbox Multiple Selection with Paging</p-header>
<p-column [style]="{'width':'38px'}" selectionMode="multiple"></p-column>
<p-column field="vin" header="Vin"></p-column>
<p-column field="year" header="Year"></p-column>
<p-column field="brand" header="Brand"></p-column>
<p-column field="color" header="Color"></p-column>
<p-footer>
<ul>
<li *ngFor="let car of selectedCars3" style="text-align: left">{{car.vin + ' - ' + car.brand + ' - ' + car.year + ' - ' + car.color}}</li>
</ul>
</p-footer>
</p-dataTable>