Is there a way to check all checkboxes in a table when the checkbox in the table header is clicked? I am currently utilizing angular 12 and primeNG table for this functionality.
<p-table styleClass="text-sm" [value]="value" [loading]="loading" [rowHover]="true" [scrollable]="true" scrollDirection="both" class="p-element p-datatable-gridlines" (onLazyLoad)="onLazyLoad.emit($event)" [ngClass]="{'p-datatable-is-loading': loading}"
[paginator]="true" [rows]="10" [showCurrentPageReport]="true" responsiveLayout="scroll"
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} entries" [rowsPerPageOptions]="[10,20,30]"
onPage="handlePageChange($event)">
<ng-template pTemplate="header">
<tr>
///////////////////// THIS IS THE CHECKBOX IN TABLE HEADER///////////////////////////////
<th *ngIf="status!='all'" style="width: 40px" pFrozenColumn class="border-right-none">
<p-checkbox value="{{value.id}}" [(ngModel)]="selectedLeaves" (onChange)="allSelect(value)"></p-checkbox>
</th>
<th *ngIf="state!=_componentState.VIEW" style="width: 30px" pFrozenColumn class="border-right-none"></th>
<th style="width: calc(358px / 2)" pFrozenColumn class="border-left-none">From</th>
<th style="width: calc(358px / 2)" pFrozenColumn class="p-frozen-column-last border-right-none">To</th>
<th style="width: 180px">Department</th>
<th pSortableColumn="created_at" style="width: 100px">Date Field<p-sortIcon field="created_at"></p-sortIcon></th>
<th style="width: 100px" class="border-left-none">Day</th>
<th style="width: 180px">Employee Name</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-leave let-rowIndex="rowIndex">
<tr [class]="{'passed-date': helper.isPassedCurrentDate(leave.leave_from)}" (dblclick)="handleDblClickRow(leave)">
///////////////////// HERE I HAVE THE CHECKBOXES IN TABLE DETAILS///////////////////////////////
<td *ngIf="status!='all'" style="width: 40px" pFrozenColumn class="border-right-none">
<div *ngIf="status!='all'">
<p-checkbox name="group1" value="{{leave.id}}" [(ngModel)]="selectedLeaves" inputId="ch"></p-checkbox>
</div>
</td>
<td *ngIf="state!=_componentState.VIEW" style="width: 30px" pFrozenColumn class="border-right-none">
<div *ngIf="state==_componentState.ALL">
<div class="{{leave.status}}-dot"></div>
</div>
</td>
<td style="width: calc(358px / 2)" pFrozenColumn class="border-left-none">{{helper.formatDate(leave.leave_from, 'MMM D, YYYY HH:mm', false)}}</td>
<td style="width: calc(358px / 2)" pFrozenColumn class="p-frozen-column-last border-right-none">{{helper.formatDate(leave.leave_to, 'MMM D, YYYY HH:mm', false)}}</td>
<td style="width: 180px"></td>
<td style="width: 100px" class="border-left-none">{{helper.formatDate(leave.created_at, 'MM/DD')}}</td>
<td style="width: 100px">{{helper.formatDate(leave.created_at, 'ddd')}}</td>
<td style="width: 180px" class="text-capitalize">{{leave.employee_name}}</td>
</tr>
</ng-template>
I would like to implement the allSelect(value)
method where clicking on the main checkbox would automatically select all checkboxes. I prefer not to rely on primeNG table
selection for this feature.
https://stackblitz.com/edit/primeng-tableselection-demo You can refer to this example for more details.