ag-grid单元渲染错误

时间:2017-01-25 17:55:38

标签: angular typescript angular2-forms ag-grid

我正在使用AG-GRID api在main.ts文件下填充我网站上的表格,我添加了以下代码

import { bootstrap }        from 'angular2/platform/browser';
import { AppComponent }     from './app.component';
import { ROUTER_PROVIDERS } from 'angular2/router';
import { AgGridModule }     from 'ag-grid-ng2/main';

bootstrap(AppComponent, [ROUTER_PROVIDERS],AgGridModule);

我的主要component.ts文件如下

import { Component }   from 'angular2/core';
import { GridOptions } from 'ag-grid/main';

@Component({
    moduleId: module.id,
    templateUrl:"app/grid.component.html";
})

export class gridComponent{

    public gridOptions:GridOptions;

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

        this.gridOptions.columnDefs = this.createColumnDefs();
    }

    private onCellValueChanged($event) {
        this.gridOptions.api.refreshCells([$event.node],["cube"]);
    }

    private createColumnDefs() {
        return [
            {headerName: "Row 1", field: "row", width: 140}
        ];
    }

    public refreshRowData() {
        let rowData = this.createRowData();
        this.gridOptions.api.setRowData(rowData);
    }

    private createRowData() {
        let rowData:any[] = [];

        for (var i = 0; i < 15; i++) {
            rowData.push({
                row: "Name" + i
            });
        }
        console.log(rowData);
        return rowData;
    }
}

当我编译时,我得到一个未找到模块的错误。 谁能帮忙 提前致谢

2 个答案:

答案 0 :(得分:0)

您不应该在main.ts中声明它,在app.module.ts中您需要类似以下内容:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { AgGridModule }  from 'ag-grid-ng2/main';

@NgModule({
    imports:[ 
        BrowserModule, 
        AgGridModule 
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
export class AppModule { }

然后在system.config.js中你需要告诉angular在哪里找到ag-grid文件:

System.config({
    map: {
        ...
        // ag libraries
        'ag-grid-ng2' : 'node_modules/ag-grid-ng2',
        'ag-grid' : 'node_modules/ag-grid',
    },
    packages: {
        'ag-grid-ng2': {
            defaultExtension: "js"
        },
        'ag-grid': {
            defaultExtension: "js"
        },
        ...other packages
    }
}

更多信息可在angular2网站https://www.ag-grid.com/ag-grid-angular-systemjs

上找到

答案 1 :(得分:0)

查看ag-Grid SystemJS Docs以了解您需要做什么的概述,但是对于完整的工作示例,您还可以参考ag-Grid Angular Example Repo获取完整的工作示例(使用SystemJS,Webpack或AngularCLI)。

简而言之,请确保您的模块中包含以下内容:

@NgModule({
    imports: [
        BrowserModule,
        AgGridModule.withComponents([...optional Angular 2 Components to be used in the grid....]]),
        ...
})

在您的SystemJS配置中,您可能需要添加:

System.config({
        defaultJSExtensions: true,
        map: {
            'app': 'app',
            // angular bundles
            '@angular/core': 'node_modules/@angular/core/bundles/core.umd.js',
            '@angular/common': 'node_modules/@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'node_modules/@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'node_modules/@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'node_modules/@angular/http/bundles/http.umd.js',
            '@angular/router': 'node_modules/@angular/router/bundles/router.umd.js',
            '@angular/forms': 'node_modules/@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'node_modules/rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
            // ag libraries
            'ag-grid-ng2': 'node_modules/ag-grid-ng2',
            'ag-grid': 'node_modules/ag-grid',
            'ag-grid-enterprise': 'node_modules/ag-grid-enterprise'
        },
        packages: {
            app: {
                main: './boot.js'
            },
            'ag-grid': {
                main: 'main.js'
            }
        }
    }
);