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

feat(grid): expose column input for template context #9694

Merged
merged 12 commits into from
Jul 2, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,37 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
}
}

/**
* Sets custom properties provided in additional template context.
*
* ```html
* <igx-column [templateContext]="contextObject">
* <ng-template igxCell let-cell="cell">
* {{ cell.column.contextObject.prop }}
* </ng-template>
* </igx-column>
* ```
*
* @memberof IgxColumnComponent
*/
@Input()
public set templateContext(value: any){
this._contextObject = value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. templateContext suggests that the use can overwrite the context object in the column templates making it a little dubious.
    Maybe additionalTemplateContext or something along these lines.

  2. I don't think you need to separate it between a setter and a getter. Just leave it as simple property on the class.

  3. It will be nice to access the additional context directly from the template binding:

<ng-template igxCell let-props="additionalContext">
...
</ng-template>

}

/**
* Gets the column `contextObject`.
* ```typescript
* let columncontextObject = this.column.contextObject;
* ```
*
* @memberof IgxColumnComponent
*/
public get contextObject(): any {
return this._contextObject;
}


/**
* Gets the column `summaries`.
* ```typescript
Expand Down Expand Up @@ -1567,6 +1598,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
private _field: string;
private _calcWidth = null;
private _columnPipeArgs: IColumnPipeArgs = { digitsInfo: DEFAULT_DIGITS_INFO };
private _contextObject: any;

constructor(
public gridAPI: GridBaseAPIService<IgxGridBaseDirective & GridType>,
Expand Down
40 changes: 40 additions & 0 deletions projects/igniteui-angular/src/lib/grids/grid/column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('IgxGrid - Column properties #grid', () => {
ColumnsFromIterableComponent,
TemplatedColumnsComponent,
TemplatedInputColumnsComponent,
TemplatedContextInputColumnsComponent,
ColumnCellFormatterComponent,
ColumnHaederClassesComponent,
ColumnHiddenFromMarkupComponent,
Expand Down Expand Up @@ -308,6 +309,20 @@ describe('IgxGrid - Column properties #grid', () => {

});

it('should support passing properties through the templateContext input property', () => {
const fixture = TestBed.createComponent(TemplatedContextInputColumnsComponent);
fixture.detectChanges();

const grid = fixture.componentInstance.instance;
const contextObject = {property1: 'cellContent', property2: 'header'};
const firstColumn = grid.columns[0];
const secondColumn = grid.getColumnByName('LastName');

expect(firstColumn.contextObject).toEqual(contextObject);
expect(firstColumn.cells[0].nativeElement.innerText).toEqual(contextObject.property1);
expect(secondColumn.headerCell.elementRef.nativeElement.innerText).toEqual(contextObject.property2);
});

it('should apply column\'s formatter programmatically', () => {
const expectedVal = ['Johny', 'Sally', 'Tim'];
const expectedValToLower = ['johny', 'sally', 'tim'];
Expand Down Expand Up @@ -1133,6 +1148,31 @@ export class TemplatedInputColumnsComponent {
public columns = Object.keys(this.data[0]);
}


@Component({
template: `
<igx-grid [data]="data">
<igx-column [templateContext]="contextObject" field="FirstName">
<ng-template igxCell let-cell="cell">
{{ cell.column.contextObject.property1 }}
</ng-template>
</igx-column>
<igx-column [templateContext]="contextObject" field="LastName">
<ng-template igxHeader let-column>
{{ column.contextObject.property2 }}
</ng-template>
</igx-column>
</igx-grid>
`
})
export class TemplatedContextInputColumnsComponent {
@ViewChild(IgxGridComponent, { read: IgxGridComponent, static: true })
public instance: IgxGridComponent;
public contextObject = {property1: 'cellContent', property2: 'header'};

public data = SampleTestData.personNameAgeData();
}

@Component({
template: `
<igx-grid [data]="data" height="500px" width="400px">
Expand Down