From cf5506be6b1251196a349172b4d3f55dcd90e73c Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Mon, 19 Aug 2019 21:13:24 +0300 Subject: [PATCH] feat(ngrid): get data item from cell reference --- libs/ngrid/src/lib/table/context/api.ts | 24 +++++++++++++++-------- libs/ngrid/src/lib/table/context/types.ts | 3 ++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libs/ngrid/src/lib/table/context/api.ts b/libs/ngrid/src/lib/table/context/api.ts index 665eebc98..c3102f0fa 100644 --- a/libs/ngrid/src/lib/table/context/api.ts +++ b/libs/ngrid/src/lib/table/context/api.ts @@ -351,30 +351,38 @@ export class ContextApi { return this.rowContext(index); } - getCell(cell: HTMLElement): PblNgridCellContext | undefined + getCell(cell: HTMLElement | GridDataPoint): PblNgridCellContext | undefined /** * Return the cell context for the cell at the point specified * @param row * @param col */ getCell(row: number, col: number): PblNgridCellContext | undefined; - getCell(rowOrCellElement: number | HTMLElement, col?: number): PblNgridCellContext | undefined { + getCell(rowOrCellElement: number | HTMLElement | GridDataPoint, col?: number): PblNgridCellContext | undefined { if (typeof rowOrCellElement === 'number') { const rowContext = this.rowContext(rowOrCellElement); if (rowContext) { return rowContext.cell(col); } } else { - const [ r, c ] = findCellRenderedIndex(rowOrCellElement); - const column = this.extApi.table.columnApi.findColumnAt(c); - const columnIndex = this.extApi.table.columnApi.indexOf(column); - const rowContext = this.rowContext(r); - if (rowContext) { - return rowContext.cell(columnIndex); + const ref = resolveCellReference(rowOrCellElement, this as any); + if (ref instanceof PblCellContext) { + return ref; } } } + getDataItem(cell: CellReference): any { + const ref = resolveCellReference(cell, this as any); + if (ref instanceof PblCellContext) { + return ref.col.getValue(ref.rowContext.$implicit); + } else if (ref) { + const row = this.extApi.table.ds.source[ref[0].dataIndex]; + const column = this.extApi.table.columnApi.findColumnAt(ref[1]); + return column.getValue(row); + } + } + createCellContext(renderRowIndex: number, column: PblColumn): PblCellContext { const rowContext = this.rowContext(renderRowIndex); const colIndex = this.columnApi.indexOf(column); diff --git a/libs/ngrid/src/lib/table/context/types.ts b/libs/ngrid/src/lib/table/context/types.ts index 308c13508..8fb29c050 100644 --- a/libs/ngrid/src/lib/table/context/types.ts +++ b/libs/ngrid/src/lib/table/context/types.ts @@ -221,7 +221,7 @@ export interface PblNgridContextApi { */ getRow(rowIndex: number | HTMLElement): PblNgridRowContext | undefined; - getCell(cell: HTMLElement): PblNgridCellContext | undefined + getCell(cell: HTMLElement | GridDataPoint): PblNgridCellContext | undefined /** * Get the cell context in the specific row index and column index * @param rowIndex The index position of the row. @@ -229,4 +229,5 @@ export interface PblNgridContextApi { */ getCell(rowIndex: number, colIndex: number): PblNgridCellContext | undefined; + getDataItem(cell: CellReference): any; }