diff --git a/src/models/gridOption.interface.ts b/src/models/gridOption.interface.ts index cb807977..2d2bfe89 100644 --- a/src/models/gridOption.interface.ts +++ b/src/models/gridOption.interface.ts @@ -25,6 +25,7 @@ export interface CustomDataView { getItem: (index: number) => T; getItemMetadata(row: number, cell?: boolean | number): ItemMetadata | null; getLength: () => number; + getCellValue?: (index: number, field: string) => T[keyof T]; } export interface CssStyleHash { diff --git a/src/slick.grid.ts b/src/slick.grid.ts index 0506b9ce..7d9cef6f 100644 --- a/src/slick.grid.ts +++ b/src/slick.grid.ts @@ -2738,7 +2738,7 @@ export class SlickGrid = Column, O e rowInfo.startIndex = 0; rowInfo.endIndex = rowInfo.rowCount - 1; rowInfo.valueArr = null; - rowInfo.getRowVal = (j: number) => this.getDataItem(j)[columnDef.field as keyof TData]; + rowInfo.getRowVal = (j: number) => this.getCellValue(j, columnDef.field as string); const rowSelectionMode = (isInit ? autoSize.rowSelectionModeOnInit : undefined) || autoSize.rowSelectionMode; @@ -3404,6 +3404,29 @@ export class SlickGrid = Column, O e } } + /** + * Returns the value of a single field for a given row index. + * + * When the databinding source is a `CustomDataView` that implements the optional + * `getCellValue(index, field)` accessor, that method is used directly. This allows + * column-oriented (or otherwise non row-materializing) data sources to return a + * single cell value without first having to build a full row object via `getItem()`, + * which can be expensive when called repeatedly (e.g. during column content auto-sizing). + * + * Falls back to `getDataItem(i)[field]` for plain arrays or data sources that don't + * implement `getCellValue`. + * + * @param {Number} i Item row index. + * @param {String} field Column field name. + */ + getCellValue(i: number, field: string): TData[keyof TData] { + const customDataView = this.data as CustomDataView; + if (customDataView.getCellValue) { + return customDataView.getCellValue(i, field) as TData[keyof TData]; + } + return this.getDataItem(i)[field as keyof TData]; + } + /** Are we using a DataView? */ hasDataView() { return !Array.isArray(this.data);