Skip to content

Commit

Permalink
feat(table): add custom render component on init function hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tibing-old-email committed May 4, 2017
1 parent fd98bfc commit e06fa09
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

import { ViewCell } from '../../../../ng2-smart-table';

@Component({
selector: 'button-view',
template: `
<button (click)="showAlert()">{{ renderValue }}</button>
<button (click)="onClick()">{{ renderValue }}</button>
`,
})
export class ButtonViewComponent implements ViewCell, OnInit {
renderValue: string;

@Input() value: string | number;
@Input() rowData: any;

constructor() { }
@Output() save: EventEmitter<any> = new EventEmitter();

ngOnInit() {
this.renderValue = this.value.toString().toUpperCase();
}

showAlert() {
alert(this.renderValue);
onClick() {
this.save.emit(this.rowData);
}
}

Expand Down Expand Up @@ -50,6 +51,11 @@ export class BasicExampleButtonViewComponent implements OnInit {
title: 'Button',
type: 'custom',
renderComponent: ButtonViewComponent,
onComponentInitFunction(instance) {
instance.save.subscribe(row => {
alert(`${row.name} saved!`)
});
}
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class CustomRenderComponent implements ViewCell, OnInit {
renderValue: string;

@Input() value: string | number;
@Input() rowData: any;

ngOnInit() {
this.renderValue = this.value.toString().toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@angular/core';

import { Cell } from '../../../lib/data-set/cell';
import { ViewCell } from './view-cell';

@Component({
selector: 'custom-view-component',
Expand All @@ -27,11 +28,9 @@ export class CustomViewComponent implements OnInit, OnDestroy {

ngOnInit() {
if (this.cell && !this.customComponent) {
const 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();
this.createCustomComponent();
this.decorateNgOnInit();
this.patchInstance();
}
}

Expand All @@ -40,4 +39,38 @@ export class CustomViewComponent implements OnInit, OnDestroy {
this.customComponent.destroy();
}
}

protected createCustomComponent() {
const componentFactory = this.resolver.resolveComponentFactory(this.cell.getColumn().renderComponent);
this.customComponent = this.dynamicTarget.createComponent(componentFactory);
}

protected decorateNgOnInit() {
const baseInit = this.customComponent.instance.ngOnInit;

this.customComponent.instance.ngOnInit = () => {
this.callOnComponentInit();
this.callBaseInit(baseInit);
};
}

protected callOnComponentInit() {
const onComponentInitFunction = this.cell.getColumn().getOnComponentInitFunction();
onComponentInitFunction && onComponentInitFunction(this.customComponent.instance);
}

protected callBaseInit(baseInit: Function) {
baseInit && baseInit.call(this.customComponent.instance);
}

protected patchInstance() {
Object.assign(this.customComponent.instance, this.getPatch());
}

protected getPatch(): ViewCell {
return {
value: this.cell.getValue(),
rowData: this.cell.getRow().getData()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface ViewCell {
value: string | number;
rowData: any;
}
6 changes: 6 additions & 0 deletions src/ng2-smart-table/lib/data-set/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ export class Column {
compareFunction: Function;
valuePrepareFunction: Function;
filterFunction: Function;
onComponentInitFunction: Function;

constructor(public id: string, protected settings: any, protected dataSet: DataSet) {
this.process();
}

getOnComponentInitFunction(): Function {
return this.onComponentInitFunction;
}

getCompareFunction(): Function {
return this.compareFunction;
}
Expand Down Expand Up @@ -67,6 +72,7 @@ export class Column {
this.compareFunction = this.settings['compareFunction'];
this.valuePrepareFunction = this.settings['valuePrepareFunction'];
this.filterFunction = this.settings['filterFunction'];
this.onComponentInitFunction = this.settings['onComponentInitFunction'];
}

prepareType(): string {
Expand Down

1 comment on commit e06fa09

@praveenbm9321
Copy link

Choose a reason for hiding this comment

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

in BasicExampleButtonViewComponent onComponentInitFunction() function in not working properly instead of row data only button value is showing

Please sign in to comment.