如何在AngularJS 1.6上设置Ag-grid

时间:2019-02-20 18:09:49

标签: angularjs typescript ag-grid

我按照该教程轻松设置了AngularJS的agGrid,但事实是我也使用typescript而不是javascript。 我确实做到了:

npm install ag-grid
var AgGrid = require('ag-grid');
AgGrid.initialiseAgGridWithAngular1(angular);
var module = angular.module("example", ["agGrid"]);

然后在我的html中使用它并显示出来:

<div ag-grid="$ctrl.gridOptions" class="ag-theme-balham" style="height: 100%;"></div>

但是我没有得到任何CSS。 enter image description here

所以我尝试添加一些要求:

require("ag-grid/dist/styles/ag-grid.css")
require("ag-grid/dist/styles/ag-theme-balham.css")    
var AgGrid = require('ag-grid');

但是仍然没有任何想法吗?谢谢

2 个答案:

答案 0 :(得分:0)

我正在使用基本主题,它对我有用。下面是相同的代码。

    //**Layout Page**
    <link rel="stylesheet" href="~/Content/ag-grid.css">

    //If you're using the bundled version, you can reference the ag-Grid-Enterprise library via CDN
    <script src="_url_to_your_chosen_cdn_/ag-grid-enterprise.js"></script>

    //Load from Local 
    //<script src="node_modules/ag-grid/dist/ag-grid-enterprise.js"></script>

    <script type="text/javascript">
            // Update angular grid license key. If you are using Enterprise Version
            agGrid.LicenseManager.setLicenseKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            // get ag-Grid to create an Angular module and register the ag-Grid directive
            agGrid.initialiseAgGridWithAngular1(angular);

            var app = angular.module("AppName", ["agGrid"]);            
    </script>

    //**View**
    <div ag-grid="agGridOptions" class="ag-theme-fresh" style="height: 400px;"></div>

    //**ControllerJS**
    $scope.agGridOptions = {
        angularCompileRows: true,
        columnDefs: columnDefs,
        rowData: rowData,
    }

答案 1 :(得分:0)

我终于找到了官方文档中思想丰富的部分。

因此,有一些易于使用的步骤:

  • 安装依赖项npm install ag-grid-community
  • 在您的app.ts中:
    • 添加您的参考var AgGrid = require('ag-grid-community');
    • 初始化AgGrid AgGrid.initialiseAgGridWithAngular1(angular);
    • 将其添加到主模块中

在您的课堂上还是我的情况下,我的组件

  • 引用import { Grid, GridOptions} from "ag-grid-community";
  • 您的样式(您也可以将其添加到vendor.ts中)
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";

然后使用您的网格!

  • 添加您的构造函数
this.gridOptions = {
  columnDefs: this.createColumnDefs(),
  rowData: this.createRowData()
};
let eGridDiv:HTMLElement = <HTMLElement>document.querySelector('#myGrid');
new Grid(eGridDiv, this.gridOptions);
  • 定义您的内容
private createColumnDefs() {
    return [
        {headerName: "Make", field: "make"},
        {headerName: "Model", field: "model"},
        {headerName: "Price", field: "price"}
    ];
}

private createRowData() {
    return [
        {make: "Toyota", model: "Celica", price: 35000},
        {make: "Ford", model: "Mondeo", price: 32000},
        {make: "Porsche", model: "Boxter", price: 72000}
    ];
}