Skip to content

Commit

Permalink
Merge pull request #16619 from primefaces/issue-16618
Browse files Browse the repository at this point in the history
Fixed #16618 - Table | Bug on table checkboxes
  • Loading branch information
mehmetcetin01140 authored Nov 12, 2024
2 parents 7b32d75 + 92bfbc4 commit 54356ba
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/app/components/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ export class TableService {
private valueSource = new Subject<any>();
private totalRecordsSource = new Subject<any>();
private columnsSource = new Subject();
private isHeaderCheckboxSelection = new Subject<any>();

sortSource$ = this.sortSource.asObservable();
isHeaderCheckboxSelection$ = this.isHeaderCheckboxSelection.asObservable();
selectionSource$ = this.selectionSource.asObservable();
contextMenuSource$ = this.contextMenuSource.asObservable();
valueSource$ = this.valueSource.asObservable();
Expand Down Expand Up @@ -118,6 +120,10 @@ export class TableService {
onColumnsChange(columns: any[]) {
this.columnsSource.next(columns);
}

onHeaderCheckboxSelection(value) {
this.isHeaderCheckboxSelection.next(value);
}
}
/**
* Table displays data in tabular format.
Expand Down Expand Up @@ -4790,15 +4796,26 @@ export class TableCheckbox {

subscription: Subscription;

tableHeaderCheckboxSubscription: Subscription;

isTableHeaderCheckboxSelection: boolean = false;

constructor(
public dt: Table,
public tableService: TableService,
public cd: ChangeDetectorRef
) {
this.subscription = this.dt.tableService.selectionSource$.subscribe(() => {
this.checked = this.dt.isSelected(this.value) && !this.disabled;
setTimeout(() => {
this.checked = this.isTableHeaderCheckboxSelection ? this.dt.isSelected(this.value) && !this.disabled : this.dt.isSelected(this.value);
this.cd.markForCheck();
});

this.ariaLabel = this.ariaLabel || this.dt.config.translation.aria ? (this.checked ? this.dt.config.translation.aria.selectRow : this.dt.config.translation.aria.unselectRow) : undefined;
this.cd.markForCheck();
});

this.tableHeaderCheckboxSubscription = this.dt.tableService.isHeaderCheckboxSelection$.subscribe((val) => {
this.isTableHeaderCheckboxSelection = val;
});
}

Expand All @@ -4815,6 +4832,7 @@ export class TableCheckbox {
},
this.value
);
this.tableService.onHeaderCheckboxSelection(false);
}
DomHandler.clearSelection();
}
Expand All @@ -4831,6 +4849,10 @@ export class TableCheckbox {
if (this.subscription) {
this.subscription.unsubscribe();
}

if (this.tableHeaderCheckboxSubscription) {
this.tableHeaderCheckboxSubscription.unsubscribe();
}
}
}

Expand Down Expand Up @@ -4874,6 +4896,8 @@ export class TableHeaderCheckbox {

valueChangeSubscription: Subscription;

tableHeaderCheckboxSubscription: Subscription;

constructor(
public dt: Table,
public tableService: TableService,
Expand All @@ -4896,6 +4920,7 @@ export class TableHeaderCheckbox {
onClick(event: Event) {
if (!this.disabled) {
if (this.dt.value && this.dt.value.length > 0) {
this.tableService.onHeaderCheckboxSelection(true);
this.dt.toggleRowsWithCheckbox(event, !this.checked);
}
}
Expand Down

0 comments on commit 54356ba

Please sign in to comment.