Skip to content

Commit 8d6f31c

Browse files
authored
Merge pull request #12197 from IgniteUI/mkirova/expose-grid-sort-template-inputs
Expose grid sorting template inputs
2 parents 015bb9c + 3aed14d commit 8d6f31c

File tree

5 files changed

+149
-7
lines changed

5 files changed

+149
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes for each version of this project will be documented in this
66

77
### New Features
88
- `igxGrid` - exposing new Input properties:
9+
10+
- `sortHeaderIconTemplate` - Gets/Sets a custom template that should be used when rendering a header sorting indicator when columns are not sorted.
11+
- `sortAscendingHeaderIconTemplate` - Gets/Sets a custom template that should be used when rendering a header sorting indicator when columns are sorted in asc order.
12+
- `sortDescendingHeaderIconTemplate` - Gets/Sets a custom template that should be used when rendering a header sorting indicator when columns are sorted in desc order.
913
- `rowEditActionsTemplate` - Gets/Sets the row edit actions template.
1014
- `rowAddTextTemplate` - Gets/Sets the row add text template.
1115
- `rowEditTextTemplate` - Gets/Sets the row edit text template.

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,23 +1474,99 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
14741474
@ContentChild(IgxExcelStyleHeaderIconDirective, { read: TemplateRef })
14751475
public excelStyleHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
14761476

1477+
14771478
/**
1478-
* The custom template, if any, that should be used when rendering a header sorting indicator when columns are sorted in asc order.
1479+
* @hidden
1480+
* @internal
14791481
*/
14801482
@ContentChild(IgxSortAscendingHeaderIconDirective, { read: TemplateRef })
1481-
public sortAscendingHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
1483+
public sortAscendingHeaderIconDirectiveTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
14821484

14831485
/**
1484-
* The custom template, if any, that should be used when rendering a header sorting indicator when columns are sorted in desc order.
1486+
* The custom template, if any, that should be used when rendering a header sorting indicator when columns are sorted in asc order.
1487+
*/
1488+
@Input()
1489+
public get sortAscendingHeaderIconTemplate(): TemplateRef<IgxGridHeaderTemplateContext> {
1490+
return this._sortAscendingHeaderIconTemplate;
1491+
}
1492+
1493+
/**
1494+
* Sets a custom template that should be used when rendering a header sorting indicator when columns are sorted in asc order.
1495+
*```html
1496+
* <ng-template #template igxSortAscendingHeaderIcon>
1497+
* <igx-icon>expand_less</igx-icon>
1498+
* </ng-template>
1499+
* ```
1500+
* ```typescript
1501+
* @ViewChild("'template'", {read: TemplateRef })
1502+
* public template: TemplateRef<any>;
1503+
* this.grid.sortAscendingHeaderIconTemplate = this.template;
1504+
* ```
14851505
*/
1506+
public set sortAscendingHeaderIconTemplate(template: TemplateRef<IgxGridHeaderTemplateContext>) {
1507+
this._sortAscendingHeaderIconTemplate = template;
1508+
}
1509+
1510+
14861511
@ContentChild(IgxSortDescendingHeaderIconDirective, { read: TemplateRef })
1487-
public sortDescendingHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
1512+
public sortDescendingHeaderIconDirectiveTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
14881513

14891514
/**
1490-
* The custom template, if any, that should be used when rendering a header sorting indicator when columns are not sorted.
1515+
* The custom template, if any, that should be used when rendering a header sorting indicator when columns are sorted in desc order.
1516+
*/
1517+
@Input()
1518+
public get sortDescendingHeaderIconTemplate() {
1519+
return this._sortDescendingHeaderIconTemplate;
1520+
}
1521+
1522+
/**
1523+
* Sets a custom template that should be used when rendering a header sorting indicator when columns are sorted in desc order.
1524+
*```html
1525+
* <ng-template #template igxSortDescendingHeaderIcon>
1526+
* <igx-icon>expand_more</igx-icon>
1527+
* </ng-template>
1528+
* ```
1529+
* ```typescript
1530+
* @ViewChild("'template'", {read: TemplateRef })
1531+
* public template: TemplateRef<any>;
1532+
* this.grid.sortDescendingHeaderIconTemplate = this.template;
1533+
* ```
1534+
*/
1535+
public set sortDescendingHeaderIconTemplate(template: TemplateRef<IgxGridHeaderTemplateContext>) {
1536+
this._sortDescendingHeaderIconTemplate = template;
1537+
}
1538+
1539+
/**
1540+
* @hidden
1541+
* @internal
14911542
*/
14921543
@ContentChild(IgxSortHeaderIconDirective, { read: TemplateRef })
1493-
public sortHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
1544+
public sortHeaderIconDirectiveTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
1545+
1546+
/**
1547+
* Gets custom template, if any, that should be used when rendering a header sorting indicator when columns are not sorted.
1548+
*/
1549+
@Input()
1550+
public get sortHeaderIconTemplate(): TemplateRef<IgxGridHeaderTemplateContext> {
1551+
return this._sortHeaderIconTemplate;
1552+
}
1553+
1554+
/**
1555+
* Sets a custom template that should be used when rendering a header sorting indicator when columns are not sorted.
1556+
*```html
1557+
* <ng-template #template igxSortHeaderIcon>
1558+
* <igx-icon>unfold_more</igx-icon>
1559+
* </ng-template>
1560+
* ```
1561+
* ```typescript
1562+
* @ViewChild("'template'", {read: TemplateRef })
1563+
* public template: TemplateRef<any>;
1564+
* this.grid.sortHeaderIconTemplate = this.template;
1565+
* ```
1566+
*/
1567+
public set sortHeaderIconTemplate(template: TemplateRef<IgxGridHeaderTemplateContext>) {
1568+
this._sortHeaderIconTemplate = template;
1569+
}
14941570

14951571
/**
14961572
* @hidden
@@ -3048,6 +3124,9 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
30483124
private readonly DRAG_SCROLL_DELTA = 10;
30493125
private _dataCloneStrategy: IDataCloneStrategy = new DefaultDataCloneStrategy();
30503126
private _autoSize = false;
3127+
private _sortHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
3128+
private _sortAscendingHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
3129+
private _sortDescendingHeaderIconTemplate: TemplateRef<IgxGridHeaderTemplateContext> = null;
30513130

30523131
/**
30533132
* @hidden @internal
@@ -3621,6 +3700,18 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
36213700
* @hidden
36223701
*/
36233702
public ngAfterContentInit() {
3703+
if (this.sortHeaderIconDirectiveTemplate) {
3704+
this.sortHeaderIconTemplate = this.sortHeaderIconDirectiveTemplate;
3705+
}
3706+
3707+
if (this.sortAscendingHeaderIconDirectiveTemplate) {
3708+
this.sortAscendingHeaderIconTemplate = this.sortAscendingHeaderIconDirectiveTemplate;
3709+
}
3710+
3711+
if (this.sortDescendingHeaderIconDirectiveTemplate) {
3712+
this.sortDescendingHeaderIconTemplate = this.sortDescendingHeaderIconDirectiveTemplate;
3713+
}
3714+
36243715
this.setupColumns();
36253716
this.toolbar.changes.pipe(filter(() => !this._init), takeUntil(this.destroy$)).subscribe(() => this.notifyChanges(true));
36263717
this.setUpPaginator();

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,29 @@ describe('IgxGrid - Grid Sorting #grid', () => {
571571
expect(icon.nativeElement.textContent.toLowerCase().trim()).toBe('expand_more');
572572
});
573573

574+
it('Should allow setting custom templates for header sorting none/ascending/descending icons via Input.', () => {
575+
fixture = TestBed.createComponent(SortByParityComponent);
576+
fixture.detectChanges();
577+
grid = fixture.componentInstance.grid;
578+
grid.sortHeaderIconTemplate = fixture.componentInstance.sortIconTemplate;
579+
grid.sortAscendingHeaderIconTemplate = fixture.componentInstance.sortAscIconTemplate;
580+
grid.sortDescendingHeaderIconTemplate = fixture.componentInstance.sortDescIconTemplate;
581+
fixture.detectChanges();
582+
const header = GridFunctions.getColumnHeader('Name', fixture, grid);
583+
let icon = GridFunctions.getHeaderSortIcon(header);
584+
expect(icon.nativeElement.textContent.toLowerCase().trim()).toBe('arrow_right');
585+
586+
grid.sort({ fieldName: 'Name', dir: SortingDirection.Asc, ignoreCase: false });
587+
fixture.detectChanges();
588+
icon = GridFunctions.getHeaderSortIcon(header);
589+
expect(icon.nativeElement.textContent.toLowerCase().trim()).toBe('arrow_drop_up');
590+
591+
grid.sort({ fieldName: 'Name', dir: SortingDirection.Desc, ignoreCase: false });
592+
fixture.detectChanges();
593+
icon = GridFunctions.getHeaderSortIcon(header);
594+
expect(icon.nativeElement.textContent.toLowerCase().trim()).toBe('arrow_drop_down');
595+
});
596+
574597
it('Should be able to set single sorting mode and sort one column at a time', fakeAsync(() => {
575598
fixture = TestBed.createComponent(SortByParityComponent);
576599
fixture.detectChanges();

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './grid-base-components.spec';
88
import { IGridSelection } from './grid-interfaces.spec';
99
import { SampleTestData, DataParent } from './sample-test-data.spec';
10-
import { ColumnDefinitions, GridTemplateStrings, EventSubscriptions, TemplateDefinitions } from './template-strings.spec';
10+
import { ColumnDefinitions, GridTemplateStrings, EventSubscriptions, TemplateDefinitions, ExternalTemplateDefinitions } from './template-strings.spec';
1111
import { IgxColumnComponent } from '../grids/columns/column.component';
1212
import { IgxFilteringOperand, IgxNumberFilteringOperand } from '../data-operations/filtering-condition';
1313
import { IFilteringExpressionsTree, FilteringExpressionsTree } from '../data-operations/filtering-expressions-tree';
@@ -2326,8 +2326,18 @@ export class NoColumnWidthGridComponent extends BasicGridComponent {
23262326
'',
23272327
'',
23282328
TemplateDefinitions.sortIconTemplates)
2329+
+ ExternalTemplateDefinitions.sortIconTemplates
23292330
})
23302331
export class SortByParityComponent extends GridDeclaredColumnsComponent implements ISortingStrategy {
2332+
@ViewChild('sortIcon', {read: TemplateRef })
2333+
public sortIconTemplate: TemplateRef<any>;
2334+
2335+
@ViewChild('sortAscIcon', {read: TemplateRef })
2336+
public sortAscIconTemplate: TemplateRef<any>;
2337+
2338+
@ViewChild('sortDescIcon', {read: TemplateRef })
2339+
public sortDescIconTemplate: TemplateRef<any>;
2340+
23312341
public sort(data: any[], fieldName: string, dir: SortingDirection) {
23322342
const key = fieldName;
23332343
const reverse = (dir === SortingDirection.Desc ? -1 : 1);

projects/igniteui-angular/src/lib/test-utils/template-strings.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,20 @@ export class TemplateDefinitions {
514514
`;
515515
}
516516

517+
export class ExternalTemplateDefinitions {
518+
public static sortIconTemplates = `
519+
<ng-template #sortIcon igxSortHeaderIcon>
520+
<igx-icon>arrow_right</igx-icon>
521+
</ng-template>
522+
<ng-template #sortAscIcon igxSortAscendingHeaderIcon>
523+
<igx-icon>arrow_drop_up</igx-icon>
524+
</ng-template>
525+
<ng-template #sortDescIcon igxSortDescendingHeaderIcon>
526+
<igx-icon>arrow_drop_down</igx-icon>
527+
</ng-template>
528+
`;
529+
}
530+
517531
export class EventSubscriptions {
518532

519533
public static columnInit = ` (columnInit)="columnInit($event)"`;

0 commit comments

Comments
 (0)