Skip to content

Commit

Permalink
Merge pull request #12982 from biswasSaumyadip/master
Browse files Browse the repository at this point in the history
P-Table - Column resize not working as expected
  • Loading branch information
cetincakiroglu authored Jul 5, 2023
2 parents f0f93f4 + 34fe4c2 commit 6de04d9
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/app/components/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,8 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
return this.filteredValue || this.value || [];
}

private _initialColWidths: number[];

dataToRender(data: any) {
const _data = data || this.processedData;

Expand Down Expand Up @@ -2474,7 +2476,9 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
this.resizeTableCells(newColumnWidth, nextColumnWidth);
}
} else if (this.columnResizeMode === 'expand') {
this._initialColWidths = this._totalTableWidth();
let tableWidth = this.tableViewChild?.nativeElement.offsetWidth + delta;

this.setResizeTableWidth(tableWidth + 'px');
this.resizeTableCells(newColumnWidth, null);
}
Expand All @@ -2493,18 +2497,25 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
DomHandler.removeClass(this.containerViewChild?.nativeElement, 'p-unselectable-text');
}

resizeTableCells(newColumnWidth: number, nextColumnWidth: number | null) {
let colIndex = DomHandler.index(this.resizeColumnElement);
let widths: any[] = [];
const tableHead = DomHandler.findSingle(this.containerViewChild?.nativeElement, '.p-datatable-thead');
private _totalTableWidth(): number[] {
let widths = [];
const tableHead = DomHandler.findSingle(this.containerViewChild.nativeElement, '.p-datatable-thead');
let headers = DomHandler.find(tableHead, 'tr > th');
headers.forEach((header) => widths.push(DomHandler.getOuterWidth(header)));

return widths;
}

resizeTableCells(newColumnWidth: number, nextColumnWidth: number | null) {
let colIndex = DomHandler.index(this.resizeColumnElement);
let width = this.columnResizeMode === 'expand' ? this._initialColWidths : this._totalTableWidth();

this.destroyStyleElement();
this.createStyleElement();


let innerHTML = '';
widths.forEach((width, index) => {
width.forEach((width, index) => {
let colWidth = index === colIndex ? newColumnWidth : nextColumnWidth && index === colIndex + 1 ? nextColumnWidth : width;
let style = `width: ${colWidth}px !important; max-width: ${colWidth}px !important;`;
innerHTML += `
Expand All @@ -2515,7 +2526,6 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
}
`;
});

this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
}

Expand Down

2 comments on commit 6de04d9

@vercel
Copy link

@vercel vercel bot commented on 6de04d9 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@biswasSaumyadip
Copy link
Contributor

@biswasSaumyadip biswasSaumyadip commented on 6de04d9 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cetincakiroglu Do I have to fix the formatting issues, using Prettier?
image

Please sign in to comment.