Skip to content

Commit fc9e7bc

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 fc9e7bc

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/common/src/Utility.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,14 @@ export function faCode(key: string): number {
395395
return faChar(key).charCodeAt(0);
396396
}
397397

398+
let _removeHTMLFromStringDiv: HTMLDivElement;
398399
export function removeHTMLFromString(str: string, div?: HTMLDivElement) {
399-
div = div ? div : document.createElement("div");
400+
if (div === undefined && _removeHTMLFromStringDiv === undefined) {
401+
_removeHTMLFromStringDiv = document.createElement("div");
402+
}
403+
div = div ?? _removeHTMLFromStringDiv;
400404
div.innerHTML = str;
401-
return div.textContent || div.innerText || "";
405+
return div.textContent ?? div.innerText ?? "";
402406
}
403407

404408
// Template ---

packages/dgrid/src/Table.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Palette, PropertyExt, Field } from "@hpcc-js/common";
1+
import { Palette, PropertyExt, Field, Utility } from "@hpcc-js/common";
22
import { hashSum } from "@hpcc-js/util";
33
import { format as d3Format } from "d3-format";
44
import { select as d3Select } from "d3-selection";
@@ -155,9 +155,12 @@ export class Table extends Common {
155155
this.guessWidth(column.children, sampleData);
156156
} else {
157157
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.
158+
let cell = ("" + row[column.idx]).trim();
159+
if (this.renderHtml() && cell[0] === "<") {
160+
cell = Utility.removeHTMLFromString(cell);
161+
}
162+
return Math.max(prevVal, this.textSize(cell, this.columnWidthAutoFontName(), this.columnWidthAutoFontSize()).width);
163+
}, this.textSize("" + column.label, this.columnWidthAutoFontName(), this.columnWidthAutoFontSize(), true).width + sortablePadding) + 8; // +12 for the sort icon, +8 for the cell padding.
161164
}
162165
}
163166
}
@@ -174,8 +177,8 @@ export class Table extends Common {
174177
this._columns = this._store.columns(this.sortable(), this.formatterFunc(), this.renderCellFunc());
175178
switch (this.columnWidth()) {
176179
case "auto":
177-
const tenRows = this.data().filter((row, idx) => idx < 10);
178-
this.guessWidth(this._columns, tenRows);
180+
const sampleRows = this.data().filter((row, idx) => idx < this.columnWidthAutoSampleSize());
181+
this.guessWidth(this._columns, sampleRows);
179182
break;
180183
}
181184
const columns = this.columns();
@@ -238,9 +241,18 @@ Table.prototype._class += " dgrid_Table";
238241
export interface Table {
239242
columnWidth(): "auto" | "none";
240243
columnWidth(_: "auto" | "none"): this;
244+
columnWidthAutoSampleSize(): number;
245+
columnWidthAutoSampleSize(_: number): this;
246+
columnWidthAutoFontName(): string;
247+
columnWidthAutoFontName(_: string): this;
248+
columnWidthAutoFontSize(): number;
249+
columnWidthAutoFontSize(_: number): this;
241250
columnFormats(): ColumnFormat[];
242251
columnFormats(_: ColumnFormat[]): this;
243252
}
244253

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

0 commit comments

Comments
 (0)