Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pivot-grid): added createRow method for grid based events - 19.1.x #15458

Open
wants to merge 6 commits into
base: 19.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions projects/igniteui-angular/src/lib/grids/grid-public-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading