<!DOCTYPE html>
<html>

<head>
    <title>ag-Grid Using Dynamic Components - Richer Example</title>

    <script src="https://unpkg.com/zone.js@0.6.23?main=browser"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3"></script>
    <script src="https://unpkg.com/systemjs@0.19.27/dist/system.src.js"></script>

    <!-- ag-grid CSS -->
    <link href="https://unpkg.com/ag-grid/dist/styles/ag-grid.css" rel="stylesheet"/>
    <link href="https://unpkg.com/ag-grid/dist/styles/theme-fresh.css" rel="stylesheet"/>


    <!-- Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function (err) { console.error(err); });
    </script>
</head>

<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>

</html>
(function (global) {
    System.config({
            transpiler: 'ts',
            typescriptOptions: {
                "target": "es5",
                "module": "commonjs",
                "moduleResolution": "node",
                "sourceMap": true,
                "emitDecoratorMetadata": true,
                "experimentalDecorators": true,
                "removeComments": false,
                "noImplicitAny": true
            },
            meta: {
                'typescript': {
                    "exports": "ts"
                }
            },
            paths: {
                // paths serve as alias
                'npm:': 'https://unpkg.com/'
            },
            map: {
                'app': 'app',
                // angular bundles
                '@angular/core': 'npm:@angular/core@2.4.8/bundles/core.umd.js',
                '@angular/common': 'npm:@angular/common@2.4.8/bundles/common.umd.js',
                '@angular/compiler': 'npm:@angular/compiler@2.4.8/bundles/compiler.umd.js',
                '@angular/platform-browser': 'npm:@angular/platform-browser@2.4.8/bundles/platform-browser.umd.js',
                '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@2.4.8/bundles/platform-browser-dynamic.umd.js',
                '@angular/router': 'npm:@angular/router@2.4.8/bundles/router.umd.js',
                '@angular/forms': 'npm:@angular/forms@2.4.8/bundles/forms.umd.js',
                // other libraries
                'rxjs':                      'npm:rxjs@5.0.0',
                'ts':                        'npm:plugin-typescript@4.0.10/lib/plugin.js',
                'typescript':                'npm:typescript@2.1.1/lib/typescript.js',
                // ag libraries
                'ag-grid-angular': 'npm:ag-grid-angular',
                'ag-grid': 'npm:ag-grid'
            },
            packages: {
                app: {
                    main: './boot.ts',
                    defaultExtension: 'ts'
                },
                'ag-grid': {
                    main: 'main.js'
                }
            }
        }
    );

    if (!global.noBootstrap) {
        bootstrap();
    }

    // Bootstrap the `AppModule`(skip the `app/main.ts` that normally does this)
    function bootstrap() {

        // Stub out `app/main.ts` so System.import('app') doesn't fail if called in the index.html
        System.set(System.normalizeSync('app/boot.ts'), System.newModule({}));

        // bootstrap and launch the app (equivalent to standard main.ts)
        Promise.all([
            System.import('@angular/platform-browser-dynamic'),
            System.import('app/app.module')
        ])
            .then(function (imports) {
                var platform = imports[0];
                var app = imports[1];
                platform.platformBrowserDynamic().bootstrapModule(app.AppModule);
            })
            .catch(function (err) {
                console.error(err);
            });
    }
})(this);
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);
import { Component } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html'
})

export class AppComponent { }
<ag-from-rich-component></ag-from-rich-component>
import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
// ag-grid
import {AgGridModule} from "ag-grid-angular/main";
// application
import {AppComponent} from "./app.component";
// from rich component
import {RichComponent} from "./rich-dynamic-component-example/rich.component";
import {ClickableModule} from "./rich-dynamic-component-example/clickable.module";
import {RatioModule} from "./rich-dynamic-component-example/ratio.module";
import {RatioParentComponent} from "./rich-dynamic-component-example/ratio.parent.component";
import {ClickableParentComponent} from "./rich-dynamic-component-example/clickable.parent.component";

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        AgGridModule.withComponents(
            [
                RatioParentComponent,
                ClickableParentComponent,
            ]),
        RatioModule,
        ClickableModule
    ],
    declarations: [
        AppComponent,
        RichComponent
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}
<div style="width: 800px;">
    <h1>Using Dynamic Components - Richer Example</h1>
    <ag-grid-angular #agGrid style="width: 100%; height: 350px;" class="ag-fresh"
                 [gridOptions]="gridOptions">
    </ag-grid-angular>
</div>
import {Component} from "@angular/core";
import {GridOptions} from "ag-grid/main";
import {RatioParentComponent} from "./ratio.parent.component";
import {ClickableParentComponent} from "./clickable.parent.component";

@Component({
    moduleId: module.id,
    selector: 'ag-from-rich-component',
    templateUrl: 'rich.component.html'
})
export class RichComponent {
    public gridOptions: GridOptions;

    constructor() {
        this.gridOptions = <GridOptions>{};
        this.gridOptions.rowData = this.createRowData();
        this.gridOptions.columnDefs = this.createColumnDefs();
    }

    private createColumnDefs() {
        return [
            {headerName: "Name", field: "name", width: 200},
            {
                headerName: "Ratio Component",
                field: "ratios",
                cellRendererFramework: RatioParentComponent,
                width: 350
            },
            {
                headerName: "Clickable Component",
                field: "name",
                cellRendererFramework: ClickableParentComponent,
                width: 250
            }
        ];
    }

    private createRowData() {
        return [
            {name: 'Homer Simpson', ratios: {top: 0.25, bottom: 0.75}},
            {name: 'Marge Simpson', ratios: {top: 0.67, bottom: 0.39}},
            {name: 'Bart Simpson', ratios: {top: 0.82, bottom: 0.47}},
            {name: 'Lisa Simpson', ratios: {top: 0.39, bottom: 1}},
            {name: 'Barney', ratios: {top: 0.22, bottom: 0.78}},
            {name: 'Sideshow Bob', ratios: {top: 0.13, bottom: 0.87}},
            {name: 'Ned Flanders', ratios: {top: 0.49, bottom: 0.51}},
            {name: 'Milhouse', ratios: {top: 0.69, bottom: 0.31}},
            {name: 'Apu', ratios: {top: 0.89, bottom: 0.11}},
            {name: 'Moe', ratios: {top: 0.64, bottom: 0.36}},
            {name: 'Smithers', ratios: {top: 0.09, bottom: 0.91}},
            {name: 'Edna Krabappel', ratios: {top: 0.39, bottom: 0.61}},
            {name: 'Krusty', ratios: {top: 0.74, bottom: 0.26}}
        ];
    }
}
import { Component, Input } from '@angular/core';

@Component({
  selector: 'ag-ratio',
  template: `
    <svg viewBox="0 0 300 100" preserveAspectRatio="none">
      <rect x="0" y="0" [attr.width]="topRatio * 300" height="50" rx="4" ry="4" class="topBar" />
      <rect x="0" y="50" [attr.width]="bottomRatio * 300" height="50" rx="4" ry="4" class="bottomBar" />
    </svg>
  `,
  styles: [`
    svg {
      width:100%;
      height:100%;
      pointer-events: none;
    }

    .topBar {
      fill: #ff9933;
    }

    .bottomBar {
      fill: #6699ff;
    }
  `]
})
export class RatioComponent {
  @Input() topRatio: number = 0.67;
  @Input() bottomRatio: number = 0.50;
}
import {Component} from "@angular/core";
import {ICellRendererAngularComp} from "ag-grid-angular/main";

// both this and the parent component could be folded into one component as they're both simple, but it illustrates how
// a fuller example could work
@Component({
    selector: 'ratio-cell',
    template: `
    <ag-ratio style="height:20px" [topRatio]="params?.value?.top" [bottomRatio]="params?.value?.bottom">
    </ag-ratio>
    `,
    styles: [`
        ag-ratio {
          display: block;
          overflow:hidden;
          border:1px solid #ccc;
          border-radius:6px;
          background: #fff;
        }
    `]
})
export class RatioParentComponent implements ICellRendererAngularComp {
    public params: any = {
        value: {top: 0.25, bottom: 0.75}
    };

    agInit(params: any): void {
        this.params = params;
    }
}
import {NgModule} from '@angular/core';

import {RatioComponent} from "./ratio.component";
import {RatioParentComponent} from "./ratio.parent.component";

@NgModule({
    imports: [],
    declarations: [
        RatioComponent,
        RatioParentComponent
    ],
    exports: [
        RatioParentComponent
    ]
})
export class RatioModule {
}
import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
    selector: 'ag-clickable',
    template: `
    <button style="height: 21px" (click)="click()">Click Me</button>
  `
})
export class ClickableComponent {
    @Input() cell:any;
    @Output() onClicked = new EventEmitter<boolean>();

    click() : void {
        this.onClicked.emit(this.cell);
    }
}
import {Component} from "@angular/core";
import {ICellRendererAngularComp} from "ag-grid-angular/main";

// both this and the parent component could be folded into one component as they're both simple, but it illustrates how
// a fuller example could work
@Component({
    selector: 'clickable-cell',
    template: `
    <ag-clickable (onClicked)="clicked($event)" [cell]="cell"></ag-clickable>
    `
})
export class ClickableParentComponent implements ICellRendererAngularComp {
    private params: any;
    public cell: any;

    agInit(params: any): void {
        this.params = params;
        this.cell = {row: params.value, col: params.colDef.headerName};
    }

    public clicked(cell: any): void {
        console.log("Child Cell Clicked: " + JSON.stringify(cell));
    }
}

import {NgModule} from '@angular/core';

import {ClickableComponent} from "./clickable.component";
import {ClickableParentComponent} from "./clickable.parent.component";

@NgModule({
    imports: [],
    declarations: [
        ClickableComponent,
        ClickableParentComponent
    ],
    exports: [
        ClickableParentComponent
    ]
})
export class ClickableModule {
}