Skip to content

Commit 870f99f

Browse files
committed
feat(grid): Added style bindings for headers
1 parent 5659a2e commit 870f99f

File tree

7 files changed

+116
-3
lines changed

7 files changed

+116
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes for each version of this project will be documented in this file.
44

5+
## 12.1.3
6+
7+
### New Features
8+
- `igxGrid`
9+
- Added `headerStyles` and `headerGroupStyles` inputs to the column component.
10+
Similar to `cellStyles` is exposes a way to bind CSS properties and style the grid headers.
11+
512
## 12.1.2
613
- `igxGrid`
714
- The column formatter callback signature now accepts the row data as an additional argument:

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,27 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
517517
@Input()
518518
public headerClasses = '';
519519

520+
/**
521+
* Sets conditional style properties on the column header.
522+
* Similar to `ngStyle` it accepts an object literal where the keys are
523+
* the style properties and the value is the expression to be evaluated.
524+
* ```typescript
525+
* styles = {
526+
* background: 'royalblue',
527+
* color: (column) => column.pinned : 'red': 'inherit'
528+
* }
529+
* ```
530+
* ```html
531+
* <igx-column [headerStyles]="styles"></igx-column>
532+
* ```
533+
*
534+
* @memberof IgxColumnComponent
535+
*/
536+
@notifyChanges()
537+
@WatchColumnChanges()
538+
@Input()
539+
public headerStyles = null;
540+
520541
/**
521542
* Sets/gets the class selector of the column group header.
522543
* ```typescript
@@ -532,6 +553,28 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
532553
@WatchColumnChanges()
533554
@Input()
534555
public headerGroupClasses = '';
556+
557+
/**
558+
* Sets conditional style properties on the column header group wrapper.
559+
* Similar to `ngStyle` it accepts an object literal where the keys are
560+
* the style properties and the value is the expression to be evaluated.
561+
* ```typescript
562+
* styles = {
563+
* background: 'royalblue',
564+
* color: (column) => column.pinned : 'red': 'inherit'
565+
* }
566+
* ```
567+
* ```html
568+
* <igx-column [headerGroupStyles]="styles"></igx-column>
569+
* ```
570+
*
571+
* @memberof IgxColumnComponent
572+
*/
573+
@notifyChanges()
574+
@WatchColumnChanges()
575+
@Input()
576+
public headerGroupStyles = null;
577+
535578
/**
536579
* Sets a conditional class selector of the column cells.
537580
* Accepts an object literal, containing key-value pairs,

projects/igniteui-angular/src/lib/grids/common/grid-pipes.module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
IgxColumnFormatterPipe,
2020
IgxSummaryFormatterPipe,
2121
IgxGridAddRowPipe,
22-
IgxHeaderGroupWidthPipe
22+
IgxHeaderGroupWidthPipe,
23+
IgxHeaderGroupStylePipe
2324
} from './pipes';
2425

2526
@NgModule({
@@ -42,7 +43,8 @@ import {
4243
IgxGridAddRowPipe,
4344
IgxColumnFormatterPipe,
4445
IgxSummaryFormatterPipe,
45-
IgxHeaderGroupWidthPipe
46+
IgxHeaderGroupWidthPipe,
47+
IgxHeaderGroupStylePipe
4648
],
4749
exports: [
4850
IgxGridFilterConditionPipe,
@@ -63,7 +65,8 @@ import {
6365
IgxGridAddRowPipe,
6466
IgxColumnFormatterPipe,
6567
IgxSummaryFormatterPipe,
66-
IgxHeaderGroupWidthPipe
68+
IgxHeaderGroupWidthPipe,
69+
IgxHeaderGroupStylePipe
6770
],
6871
imports: [
6972
CommonModule

projects/igniteui-angular/src/lib/grids/common/pipes.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,22 @@ export class IgxHeaderGroupWidthPipe implements PipeTransform {
345345
return hasLayout ? '' : `${Math.max(parseFloat(width), minWidth)}px`;
346346
}
347347
}
348+
349+
@Pipe({ name: 'igxHeaderGroupStyle' })
350+
export class IgxHeaderGroupStylePipe implements PipeTransform {
351+
352+
public transform(styles: { [prop: string]: any }, column: IgxColumnComponent, _: number): { [prop: string]: any } {
353+
const css = {};
354+
355+
if (!styles) {
356+
return css;
357+
}
358+
359+
for (const prop of Object.keys(styles)) {
360+
const res = styles[prop];
361+
css[prop] = typeof res === 'function' ? res(column) : res;
362+
}
363+
364+
return css;
365+
}
366+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,41 @@ describe('IgxGrid - Column properties #grid', () => {
443443
row.cells.forEach(cell => expect(cell.nativeElement.getAttribute('style')).toMatch('background: black'));
444444
});
445445

446+
it('should apply custom CSS bindings to grid headers', () => {
447+
const fix = TestBed.createComponent(ColumnHaederClassesComponent);
448+
fix.detectChanges();
449+
const grid = fix.componentInstance.grid;
450+
451+
452+
const styles = {
453+
background: 'rebeccapurple',
454+
color: 'white'
455+
};
456+
457+
grid.columns.forEach(col => col.headerStyles = styles);
458+
fix.detectChanges();
459+
460+
grid.headerCellList.forEach(header => expect(header.nativeElement.getAttribute('style')).toMatch('background: rebeccapurple'));
461+
462+
});
463+
464+
it('should apply custom CSS bindings to grid header groups', () => {
465+
const fix = TestBed.createComponent(ColumnHaederClassesComponent);
466+
fix.detectChanges();
467+
const grid = fix.componentInstance.grid;
468+
469+
470+
const styles = {
471+
background: 'rebeccapurple',
472+
color: 'white'
473+
};
474+
475+
grid.columns.forEach(col => col.headerGroupStyles = styles);
476+
fix.detectChanges();
477+
478+
grid.headerGroupsList.forEach(hGroup => expect(hGroup.nativeElement.getAttribute('style')).toMatch('background: rebeccapurple'));
479+
});
480+
446481
it('should set title attribute on column header spans', () => {
447482
const fix = TestBed.createComponent(ColumnsFromIterableComponent);
448483
fix.detectChanges();

projects/igniteui-angular/src/lib/grids/headers/grid-header-group.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ng-container *ngFor="let child of column.children" >
1313
<igx-grid-header-group *ngIf="!child.hidden" class="igx-grid-thead__subgroup"
1414
[ngClass]="child.headerGroupClasses"
15+
[ngStyle]="child.headerGroupStyles | igxHeaderGroupStyle:child:grid.pipeTrigger"
1516
[column]="child"
1617
[igxColumnMovingDrag]="child"
1718
[ghostHost]="grid.outlet.nativeElement"
@@ -70,6 +71,7 @@
7071
<ng-container *ngFor="let child of column.children">
7172
<igx-grid-header-group *ngIf="!child.hidden" class="igx-grid-thead__subgroup"
7273
[ngClass]="child.headerGroupClasses"
74+
[ngStyle]="child.headerGroupStyles | igxHeaderGroupStyle:child:grid.pipeTrigger"
7375
[column]="child"
7476
[style.min-width]="child.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:grid.hasColumnLayouts"
7577
[style.flex-basis]="child.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:grid.hasColumnLayouts">
@@ -86,6 +88,7 @@
8688
class="igx-grid-th--fw"
8789
[id]="grid.id + '_' + column.field"
8890
[ngClass]="column.headerClasses"
91+
[ngStyle]="column.headerStyles | igxHeaderGroupStyle:column:grid.pipeTrigger"
8992
[igxColumnMovingDrag]="column"
9093
[ghostHost]="grid.outlet.nativeElement"
9194
[attr.droppable]="true"

projects/igniteui-angular/src/lib/grids/headers/grid-header-row.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<ng-container *ngFor="let column of pinnedColumnCollection | igxTopLevel">
6666
<igx-grid-header-group
6767
[ngClass]="column.headerGroupClasses"
68+
[ngStyle]="column.headerGroupStyles | igxHeaderGroupStyle:column:grid.pipeTrigger"
6869
[column]="column"
6970
[style.min-width]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL"
7071
[style.flex-basis]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL">
@@ -83,6 +84,7 @@
8384
>
8485
<igx-grid-header-group
8586
[ngClass]="column.headerGroupClasses"
87+
[ngStyle]="column.headerGroupStyles |igxHeaderGroupStyle:column:grid.pipeTrigger"
8688
[column]="column"
8789
[style.min-width]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL"
8890
[style.flex-basis]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL">
@@ -94,6 +96,7 @@
9496
<ng-container *ngFor="let column of pinnedColumnCollection | igxTopLevel">
9597
<igx-grid-header-group
9698
[ngClass]="column.headerGroupClasses"
99+
[ngStyle]="column.headerGroupStyles |igxHeaderGroupStyle:column:grid.pipeTrigger"
97100
[column]="column"
98101
[style.min-width]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL"
99102
[style.flex-basis]="column.calcWidth | igxHeaderGroupWidth:grid.defaultHeaderGroupMinWidth:hasMRL"

0 commit comments

Comments
 (0)