diff --git a/projects/igniteui-angular/src/lib/grids/grid-public-row.ts b/projects/igniteui-angular/src/lib/grids/grid-public-row.ts index 89a2cabe484..88431321d9d 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-public-row.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-public-row.ts @@ -6,6 +6,8 @@ import { IgxSummaryResult } from './summaries/grid-summary'; import { ITreeGridRecord } from './tree-grid/tree-grid.interfaces'; import { mergeWith } from 'lodash-es'; import { CellType, GridServiceType, GridType, IGridValidationState, RowType, ValidationStatus } from './common/grid.interface'; +import { IgxPivotGridComponent } from './pivot-grid/public_api'; +import { PivotUtil } from './pivot-grid/pivot-util'; abstract class BaseRow implements RowType { public index: number; @@ -791,3 +793,71 @@ export class IgxSummaryRow implements RowType { return row; } } + +export class IgxPivotGridRow implements RowType { + + /** The index of the row within the grid */ + public index: number; + + /** + * The grid that contains the row. + */ + public grid: IgxPivotGridComponent; + private _data?: any; + + constructor(grid: IgxPivotGridComponent, index: number, data?: any) { + this.grid = grid; + this.index = index; + this._data = data && data.addRow && data.recordRef ? data.recordRef : data; + } + + /** + * The data passed to the row component. + */ + public get data(): any { + return this._data ?? this.grid.dataView[this.index]; + } + + /** + * Returns the view index calculated per the grid page. + */ + public get viewIndex(): number { + return this.index + this.grid.page * this.grid.perPage; + } + + /** + * Gets the row key. + * A row in the grid is identified either by: + * - primaryKey data value, + * - the whole rowData, if the primaryKey is omitted. + * + * ```typescript + * let rowKey = row.key; + * ``` + */ + public get key(): any { + const dimension = this.grid.visibleRowDimensions[this.grid.visibleRowDimensions.length - 1]; + const recordKey = PivotUtil.getRecordKey(this.data, dimension); + return recordKey ? recordKey : null; + } + + /** + * Gets whether the row is selected. + * Default value is `false`. + * ```typescript + * row.selected = true; + * ``` + */ + public get selected(): boolean { + return this.grid.selectionService.isRowSelected(this.key); + } + + public set selected(val: boolean) { + if (val) { + this.grid.selectionService.selectRowsWithNoEvent([this.key]); + } else { + this.grid.selectionService.deselectRowsWithNoEvent([this.key]); + } + this.grid.cdr.markForCheck(); + } +} diff --git a/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts b/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts index 042519ef0c8..ace03b6ba78 100644 --- a/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts +++ b/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts @@ -102,6 +102,7 @@ import { IgxTextHighlightService } from '../../directives/text-highlight/text-hi import { IgxPivotRowHeaderGroupComponent } from './pivot-row-header-group.component'; import { IgxPivotDateDimension } from './pivot-grid-dimensions'; import { IgxPivotRowDimensionMrlRowComponent } from './pivot-row-dimension-mrl-row.component'; +import { IgxPivotGridRow } from '../grid-public-row'; let NEXT_ID = 0; const MINIMUM_COLUMN_WIDTH = 200; @@ -2510,4 +2511,20 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni this.regroupTrigger++; } } + + /** + * @hidden @internal + */ + public createRow(index: number, data?: any): RowType { + let row: RowType; + + const dataIndex = this._getDataViewIndex(index); + const rec = data ?? this.dataView[dataIndex]; + + + if (!row && rec) { + row = new IgxPivotGridRow(this, index, rec); + } + return row; + } } diff --git a/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.spec.ts b/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.spec.ts index 21b047ebe44..08febf81720 100644 --- a/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.spec.ts @@ -1,7 +1,7 @@ import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; -import { FilteringExpressionsTree, FilteringLogic, GridColumnDataType, IgxIconComponent, IgxPivotGridComponent, IgxStringFilteringOperand } from 'igniteui-angular'; +import { CellType, FilteringExpressionsTree, FilteringLogic, GridColumnDataType, IGridCellEventArgs, IgxIconComponent, IgxPivotGridComponent, IgxStringFilteringOperand } from 'igniteui-angular'; import { IgxChipComponent } from '../../chips/chip.component'; import { IgxChipsAreaComponent } from '../../chips/chips-area.component'; import { DefaultPivotSortingStrategy } from '../../data-operations/pivot-sort-strategy'; @@ -23,6 +23,7 @@ import { Size } from '../common/enums'; import { setElementSize } from '../../test-utils/helper-utils.spec'; import { IgxPivotRowDimensionMrlRowComponent } from './pivot-row-dimension-mrl-row.component'; import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-content.component'; +import { IgxGridCellComponent } from '../cell.component'; const CSS_CLASS_LIST = 'igx-drop-down__list'; const CSS_CLASS_ITEM = 'igx-drop-down__item'; @@ -2106,8 +2107,23 @@ describe('IgxPivotGrid #pivotGrid', () => { expect(pivotGrid.rowList.length).toBe(10); }); - }); + it('should have the correct IGridCellEventArgs when clicking on a cell', () => { + const pivotGrid = fixture.componentInstance.pivotGrid; + spyOn(pivotGrid.cellClick, 'emit').and.callThrough(); + fixture.detectChanges(); + + const cell = pivotGrid.gridAPI.get_cell_by_index(0, 'Bulgaria-UnitsSold') as CellType; + + pivotGrid.cellClick.emit({ cell, event: null }); + cell.nativeElement.click(); + const cellClickargs: IGridCellEventArgs = { cell, event: new MouseEvent('click') }; + + const gridCell = cellClickargs.cell as IgxGridCellComponent; + const firstEntry = gridCell.rowData.aggregationValues.entries().next().value; + expect(firstEntry).toEqual(['USA-UnitsSold', 829]); + }); + }); }); describe('IgxPivotGrid complex hierarchy #pivotGrid', () => {