I have the following markup:
<table>
<thead>
<th *ngFor="let column of columnNames">
<ng-container *ngIf="column === 'Column6'; else normalColumns">
{{column}} <input type="checkbox" #chkAll />
</ng-container>
<ng-template #normalColumns>
{{column}}
</ng-template>
</th>
</thead>
<tbody>
<tr>
<td *ngFor="let model of columnValues">
<ng-container *ngIf="model === 'Value6'; else normal">
{{model}} <input type="checkbox" [checked]="chkAll?.checked" />
</ng-container>
<ng-template #normal>
{{model}}
</ng-template>
</td>
</tr>
</tbody>
</table>
I would like to implement a "Select All" feature.
As you can see, I have the condition in the table header, that if the header name is equal to a certain value, add an input on that header. In the table body, I also have a condition, on whether there should be added an checkbox
to the column or not.
When I select the #chkAll
checkbox in the table header, I would like the checkboxes in the rows below, to also be selected. I thought that [checked]="chkAll?.checked"
on the checkboxes
would do the trick, but it does not work.
Here is my Stackblitz
Another way to do this is as follows:
In your template:
In your component:
Create a boolean called checkAll.
Here Stackblitz
Since the
chkAll
variable is defined in a separate template (created by thengFor
loop of the header), it is not available in the markup of the table body.You can call a component method when the header checkbox value changes, to perform the check/uncheck of the checkboxes in the rows:
The
checkAllBoxes
method uses theQueryList
provided byViewChildren
to access the checkboxes:See this stackblitz for a demo.