-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from damnko/custom-view-component
Add custom cell view component
- Loading branch information
Showing
13 changed files
with
114 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
demo/src/app/pages/demo/components/custom-render.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
import { ViewCell } from '../../../../../../ng2-smart-table'; | ||
|
||
@Component({ | ||
template: ` | ||
{{renderValue}} | ||
` | ||
}) | ||
export class CustomRenderComponent implements ViewCell { | ||
|
||
renderValue: string; | ||
@Input() value: string | number; | ||
|
||
ngOnInit() { | ||
this.renderValue = this.value.toString().toUpperCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/ng2-smart-table/components/cell/cell-view-mode/custom-view.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Component, | ||
Input, | ||
ComponentFactoryResolver, | ||
ViewChild, | ||
ViewContainerRef, | ||
OnInit, | ||
OnDestroy } from '@angular/core'; | ||
|
||
import { Cell } from '../../../lib/data-set/cell'; | ||
|
||
@Component({ | ||
selector: 'custom-view-component', | ||
template: ` | ||
<template #dynamicTarget></template> | ||
`, | ||
}) | ||
export class CustomViewComponent implements OnInit { | ||
|
||
customComponent: any; | ||
@Input() cell: Cell; | ||
@ViewChild('dynamicTarget', {read: ViewContainerRef}) dynamicTarget: any; | ||
|
||
constructor(private resolver: ComponentFactoryResolver) { | ||
} | ||
|
||
ngOnInit(): void { | ||
if (this.cell && !this.customComponent){ | ||
let componentFactory = this.resolver.resolveComponentFactory(this.cell.getColumn().renderComponent); | ||
this.customComponent = this.dynamicTarget.createComponent(componentFactory); | ||
|
||
// set @Inputs and @Outputs of custom component | ||
this.customComponent.instance.value = this.cell.getValue(); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this.customComponent) { | ||
this.customComponent.destroy(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './custom-view.component'; | ||
export * from './view-cell.component'; | ||
export * from './view-cell'; |
26 changes: 5 additions & 21 deletions
26
src/ng2-smart-table/components/cell/cell-view-mode/view-cell.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,18 @@ | ||
import { Component, Input, ViewChild, ElementRef, OnChanges, AfterViewInit } from '@angular/core'; | ||
import { Component, Input, ViewChild, ElementRef } from '@angular/core'; | ||
|
||
import { Cell } from '../../../lib/data-set/cell'; | ||
|
||
@Component({ | ||
selector: 'table-cell-view-mode', | ||
template: ` | ||
<div [ngSwitch]="cell.getColumn().type"> | ||
<div *ngSwitchCase="'html'" #cellContainer [innerHTML]="cell.getValue()"></div> | ||
<div *ngSwitchDefault #cellContainer>{{ cell.getValue() }}</div> | ||
<custom-view-component *ngSwitchCase="'custom'" [cell]="cell"></custom-view-component> | ||
<div *ngSwitchCase="'html'" [innerHTML]="cell.getValue()"></div> | ||
<div *ngSwitchDefault>{{ cell.getValue() }}</div> | ||
</div> | ||
` | ||
}) | ||
export class ViewCellComponent implements OnChanges, AfterViewInit { | ||
export class ViewCellComponent { | ||
|
||
@Input() cell: Cell; | ||
@ViewChild('cellContainer') cellRef: ElementRef; | ||
|
||
ngOnChanges(changes): void { | ||
setTimeout(() => this.renderCustomValue()); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.renderCustomValue(); | ||
} | ||
|
||
renderCustomValue(): void { | ||
const cellRenderFunc = this.cell.getColumn().getCellRenderFunction(); | ||
|
||
if (cellRenderFunc && this.cellRef) | ||
cellRenderFunc.call(null, this.cell, this.cellRef.nativeElement); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/ng2-smart-table/components/cell/cell-view-mode/view-cell.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ViewCell { | ||
value: string | number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters