Skip to content

Commit 7238f2b

Browse files
authored
Merge pull request #9694 from IgniteUI/dTsvetkov/feat-9020-master
feat(grid): expose column input for template context
2 parents 34ef29e + f7d59c9 commit 7238f2b

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

projects/igniteui-angular/src/lib/grids/cell.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
165165
public get context(): any {
166166
return {
167167
$implicit: this.value,
168-
cell: this
168+
cell: this,
169+
additionalTemplateContext: this.column.additionalTemplateContext
169170
};
170171
}
171172

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,22 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
748748
@Input()
749749
public colStart: number;
750750

751+
/**
752+
* Sets/gets custom properties provided in additional template context.
753+
*
754+
* ```html
755+
* <igx-column [additionalTemplateContext]="contextObject">
756+
* <ng-template igxCell let-cell="cell" let-props="additionalTemplateContext">
757+
* {{ props }}
758+
* </ng-template>
759+
* </igx-column>
760+
* ```
761+
*
762+
* @memberof IgxColumnComponent
763+
*/
764+
@Input()
765+
public additionalTemplateContext: any;
766+
751767
/**
752768
* @hidden
753769
*/

projects/igniteui-angular/src/lib/grids/common/column.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ export interface ColumnType {
4949
hasNestedPath: boolean;
5050
defaultTimeFormat: string;
5151
defaultDateTimeFormat: string;
52+
additionalTemplateContext: any;
5253
getGridTemplate(isRow: boolean, isIE: boolean): string;
5354
}

projects/igniteui-angular/src/lib/grids/grid/column.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('IgxGrid - Column properties #grid', () => {
3434
ColumnsFromIterableComponent,
3535
TemplatedColumnsComponent,
3636
TemplatedInputColumnsComponent,
37+
TemplatedContextInputColumnsComponent,
3738
ColumnCellFormatterComponent,
3839
ColumnHaederClassesComponent,
3940
ColumnHiddenFromMarkupComponent,
@@ -308,6 +309,20 @@ describe('IgxGrid - Column properties #grid', () => {
308309

309310
});
310311

312+
it('should support passing properties through the additionalTemplateContext input property', () => {
313+
const fixture = TestBed.createComponent(TemplatedContextInputColumnsComponent);
314+
fixture.detectChanges();
315+
316+
const grid = fixture.componentInstance.instance;
317+
const contextObject = {property1: 'cellContent', property2: 'cellContent1'};
318+
const firstColumn = grid.columns[0];
319+
const secondColumn = grid.columns[1];
320+
321+
expect(firstColumn.additionalTemplateContext).toEqual(contextObject);
322+
expect(firstColumn.cells[0].nativeElement.innerText).toEqual(contextObject.property1);
323+
expect(secondColumn.cells[0].nativeElement.innerText).toEqual(contextObject.property2);
324+
});
325+
311326
it('should apply column\'s formatter programmatically', () => {
312327
const expectedVal = ['Johny', 'Sally', 'Tim'];
313328
const expectedValToLower = ['johny', 'sally', 'tim'];
@@ -1133,6 +1148,31 @@ export class TemplatedInputColumnsComponent {
11331148
public columns = Object.keys(this.data[0]);
11341149
}
11351150

1151+
1152+
@Component({
1153+
template: `
1154+
<igx-grid [data]="data">
1155+
<igx-column [additionalTemplateContext]="contextObject" field="FirstName">
1156+
<ng-template igxCell let-cell="cell">
1157+
{{ cell.column.additionalTemplateContext.property1 }}
1158+
</ng-template>
1159+
</igx-column>
1160+
<igx-column [additionalTemplateContext]="contextObject">
1161+
<ng-template igxCell let-cell="cell" let-props="additionalTemplateContext">
1162+
{{ props.property2 }}
1163+
</ng-template>
1164+
</igx-column>
1165+
</igx-grid>
1166+
`
1167+
})
1168+
export class TemplatedContextInputColumnsComponent {
1169+
@ViewChild(IgxGridComponent, { read: IgxGridComponent, static: true })
1170+
public instance: IgxGridComponent;
1171+
public contextObject = {property1: 'cellContent', property2: 'cellContent1'};
1172+
1173+
public data = SampleTestData.personNameAgeData();
1174+
}
1175+
11361176
@Component({
11371177
template: `
11381178
<igx-grid [data]="data" height="500px" width="400px">

0 commit comments

Comments
 (0)