Skip to content

Commit 7f5f2d4

Browse files
committed
feat: enhance Table class with dynamic column width sampling
Fixes issue when calculating "auto" width with HTMLcontent Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 53104e0 commit 7f5f2d4

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

packages/dgrid/src/Table.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ ColumnFormat.prototype.publish("valueColumn", null, "set", "Column", function (t
9595
export class Table extends Common {
9696
private _prevColsHash;
9797
private _prevFieldsHash;
98+
protected _fakeCell: HTMLDivElement;
9899
_colsRefresh = false;
99100
_dataRefresh = false;
100101

@@ -142,6 +143,7 @@ export class Table extends Common {
142143

143144
enter(domNode, element) {
144145
super.enter(domNode, element);
146+
this._fakeCell = document.createElement("div");
145147
}
146148

147149
guessWidth(columns, data) {
@@ -155,9 +157,13 @@ export class Table extends Common {
155157
this.guessWidth(column.children, sampleData);
156158
} else {
157159
column.width = data.reduce((prevVal: number, row) => {
158-
const cell = ("" + row[column.idx]).trim();
159-
return Math.max(prevVal, this.textSize(cell).width);
160-
}, this.textSize("" + column.label, undefined, undefined, true).width + sortablePadding) + 8; // +12 for the sort icon, +8 for the cell padding.
160+
let cell = ("" + row[column.idx]).trim();
161+
if (this.renderHtml() && cell[0] === "<") {
162+
this._fakeCell.innerHTML = cell;
163+
cell = this._fakeCell.textContent ?? cell;
164+
}
165+
return Math.max(prevVal, this.textSize(cell, this.columnWidthAutoFontName(), this.columnWidthAutoFontSize()).width);
166+
}, this.textSize("" + column.label, this.columnWidthAutoFontName(), this.columnWidthAutoFontSize(), true).width + sortablePadding) + 8; // +12 for the sort icon, +8 for the cell padding.
161167
}
162168
}
163169
}
@@ -174,8 +180,8 @@ export class Table extends Common {
174180
this._columns = this._store.columns(this.sortable(), this.formatterFunc(), this.renderCellFunc());
175181
switch (this.columnWidth()) {
176182
case "auto":
177-
const tenRows = this.data().filter((row, idx) => idx < 10);
178-
this.guessWidth(this._columns, tenRows);
183+
const sampleRows = this.data().filter((row, idx) => idx < this.columnWidthAutoSampleSize());
184+
this.guessWidth(this._columns, sampleRows);
179185
break;
180186
}
181187
const columns = this.columns();
@@ -205,6 +211,8 @@ export class Table extends Common {
205211
}
206212

207213
exit(domNode, element) {
214+
this._fakeCell.remove();
215+
delete this._fakeCell;
208216
delete this._prevColsHash;
209217
delete this._prevFieldsHash;
210218
super.exit(domNode, element);
@@ -238,9 +246,18 @@ Table.prototype._class += " dgrid_Table";
238246
export interface Table {
239247
columnWidth(): "auto" | "none";
240248
columnWidth(_: "auto" | "none"): this;
249+
columnWidthAutoSampleSize(): number;
250+
columnWidthAutoSampleSize(_: number): this;
251+
columnWidthAutoFontName(): string;
252+
columnWidthAutoFontName(_: string): this;
253+
columnWidthAutoFontSize(): number;
254+
columnWidthAutoFontSize(_: number): this;
241255
columnFormats(): ColumnFormat[];
242256
columnFormats(_: ColumnFormat[]): this;
243257
}
244258

245259
Table.prototype.publish("columnWidth", "auto", "set", "Default column width", ["auto", "none"]);
260+
Table.prototype.publish("columnWidthAutoSampleSize", 10, "number", "Number of rows to sample for auto column width");
261+
Table.prototype.publish("columnWidthAutoFontName", "Verdana", "string", "Font name for auto column width calculation");
262+
Table.prototype.publish("columnWidthAutoFontSize", 12, "number", "Font size for auto column width calculation");
246263
Table.prototype.publish("columnFormats", [], "propertyArray", "Source Columns", null, { autoExpand: ColumnFormat });

0 commit comments

Comments
 (0)