Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/primeng/src/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,8 @@ export class Table<RowData = any> extends BaseComponent<TablePassThrough> implem

id: string = UniqueComponentId();

private thScopeObserver: MutationObserver | null = null;

styleElement: any;

responsiveStyleElement: any;
Expand Down Expand Up @@ -1353,6 +1355,7 @@ export class Table<RowData = any> extends BaseComponent<TablePassThrough> implem
if (this.isStateful() && this.resizableColumns) {
this.restoreColumnWidths();
}
this.initThScopeObserver();
}
}

Expand Down Expand Up @@ -3198,6 +3201,36 @@ export class Table<RowData = any> extends BaseComponent<TablePassThrough> implem

this.destroyStyleElement();
this.destroyResponsiveStyle();

if (this.thScopeObserver) {
this.thScopeObserver.disconnect();
this.thScopeObserver = null;
}
}

private initThScopeObserver() {
const thead = this.tableHeaderViewChild?.nativeElement;
if (!thead) {
return;
}

thead.querySelectorAll(':scope > tr > th:not([scope])').forEach((th: HTMLElement) => th.setAttribute('scope', 'col'));

this.thScopeObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as HTMLElement;
if (element.tagName === 'TH' && !element.hasAttribute('scope')) {
element.setAttribute('scope', 'col');
}
element.querySelectorAll?.('th:not([scope])').forEach((th: HTMLElement) => th.setAttribute('scope', 'col'));
}
});
});
});

this.thScopeObserver.observe(thead, { childList: true, subtree: true });
}

get dataP() {
Expand Down
42 changes: 42 additions & 0 deletions packages/primeng/src/treetable/treetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ export class TreeTable extends BaseComponent<TreeTablePassThrough> implements Bl

toggleRowIndex: Nullable<number>;

private thScopeObserver: MutationObserver | null = null;

onInit() {
if (this.lazy && this.lazyLoadOnInit && !this.virtualScroll) {
this.onLazyLoad.emit(this.createLazyLoadMetadata());
Expand Down Expand Up @@ -1033,6 +1035,12 @@ export class TreeTable extends BaseComponent<TreeTablePassThrough> implements Bl
});
}

onAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
this.initThScopeObserver();
}
}

filterService = inject(FilterService);

tableService = inject(TreeTableService);
Expand Down Expand Up @@ -2346,6 +2354,40 @@ export class TreeTable extends BaseComponent<TreeTablePassThrough> implements Bl
this.editingCellField = null;
this.editingCellData = null;
this.initialized = null;

if (this.thScopeObserver) {
this.thScopeObserver.disconnect();
this.thScopeObserver = null;
}
}

private initThScopeObserver() {
const theads = this.el.nativeElement.querySelectorAll(':scope thead');
if (!theads.length) {
return;
}

theads.forEach((thead: HTMLElement) => {
thead.querySelectorAll(':scope > tr > th:not([scope])').forEach((th: HTMLElement) => th.setAttribute('scope', 'col'));
});

this.thScopeObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as HTMLElement;
if (element.tagName === 'TH' && !element.hasAttribute('scope')) {
element.setAttribute('scope', 'col');
}
element.querySelectorAll?.('th:not([scope])').forEach((th: HTMLElement) => th.setAttribute('scope', 'col'));
}
});
});
});

theads.forEach((thead: HTMLElement) => {
this.thScopeObserver!.observe(thead, { childList: true, subtree: true });
});
}

get dataP() {
Expand Down