Skip to content

Commit 11389bc

Browse files
chintankavathiatimowolf
authored andcommitted
fix: update group checkbox state on child row selection state change
1 parent 730d024 commit 11389bc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

projects/ngx-datatable/src/lib/components/body/body-row-wrapper.component.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChangeDetectionStrategy,
44
ChangeDetectorRef,
55
Component,
6+
computed,
67
DoCheck,
78
ElementRef,
89
EventEmitter,
@@ -16,6 +17,7 @@ import {
1617
OnChanges,
1718
OnInit,
1819
Output,
20+
signal,
1921
SimpleChanges,
2022
ViewChild
2123
} from '@angular/core';
@@ -43,7 +45,7 @@ import { DatatableRowDetailDirective } from '../row-detail/row-detail.directive'
4345
<input
4446
#select
4547
type="checkbox"
46-
[checked]="selectedGroupRows.length === group.value.length"
48+
[checked]="selectedGroupRows().length === group().value.length"
4749
(change)="onCheckboxChange(select.checked)"
4850
/>
4951
</label>
@@ -92,7 +94,7 @@ export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit
9294

9395
@Input() rowIndex?: number;
9496

95-
selectedGroupRows: TRow[] = [];
97+
selectedGroupRows = signal<TRow[]>([]);
9698

9799
@Input({ transform: booleanAttribute }) expanded = false;
98100

@@ -108,13 +110,11 @@ export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit
108110
private tableComponent = inject(DatatableComponentToken);
109111
private cd = inject(ChangeDetectorRef);
110112

111-
get group(): Group<TRow> {
113+
protected group = computed(() => {
112114
if (typeof this.row === 'object' && 'value' in this.row) {
113115
return this.row;
114-
} else {
115-
throw new Error('Row is not a group');
116116
}
117-
}
117+
});
118118

119119
ngOnInit(): void {
120120
if (this.disableCheck) {
@@ -176,15 +176,17 @@ export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit
176176
// if any of the row of this group is not present in `selected` rows array
177177
// mark group header checkbox state as indeterminate
178178
if (this.groupHeader?.checkboxable && this.selectedRowsDiffer.diff(this.selected)) {
179-
const selectedRows = this.selected.filter(row => this.group.value.find(item => item === row));
179+
const selectedRows = this.selected.filter(row =>
180+
this.group()?.value.find(item => item === row)
181+
);
180182
if (this.checkBoxInput) {
181-
if (selectedRows.length && selectedRows.length !== this.group.value.length) {
183+
if (selectedRows.length && selectedRows.length !== this.group()?.value.length) {
182184
this.checkBoxInput.nativeElement.indeterminate = true;
183185
} else {
184186
this.checkBoxInput.nativeElement.indeterminate = false;
185187
}
186188
}
187-
this.selectedGroupRows = selectedRows;
189+
this.selectedGroupRows.set(selectedRows);
188190
}
189191
}
190192

@@ -195,10 +197,12 @@ export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit
195197

196198
onCheckboxChange(groupSelected: boolean): void {
197199
// First remove all rows of this group from `selected`
198-
this.selected = [...this.selected.filter(row => !this.group.value.find(item => item === row))];
200+
this.selected = [
201+
...this.selected.filter(row => !this.group().value.find(item => item === row))
202+
];
199203
// If checkbox is checked then add all rows of this group in `selected`
200204
if (groupSelected) {
201-
this.selected = [...this.selected, ...this.group.value];
205+
this.selected = [...this.selected, ...this.group().value];
202206
}
203207
// Update `selected` of DatatableComponent with newly evaluated `selected`
204208
this.tableComponent.selected = [...this.selected];

0 commit comments

Comments
 (0)