Ben
Ben

Reputation: 501

How to setup Ag-grid on AngularJS 1.6

I followed the tutorial to easily set up agGrid fot AngularJS, the thing is I also use typescript instead javascript. I actually did :

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

Then use it with in my html and it's showing up:

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

But I don't get any css. enter image description here

So I tried to add some require:

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

But still not working any ideas? Thanks

Upvotes: 1

Views: 3709

Answers (2)

Ben
Ben

Reputation: 501

I finally find out looking thought parts of the official doc.

So there is the steps to use it easily:

  • Install the dependency npm install ag-grid-community
  • In your app.ts:
    • add your reference var AgGrid = require('ag-grid-community');
    • Initialise AgGrid AgGrid.initialiseAgGridWithAngular1(angular);
    • Add it in the main module

In your class or in my case my component

  • Reference import { Grid, GridOptions} from "ag-grid-community";
  • And your styles (You can add it to vendor.ts too)
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";

Then Use your Grid!

  • Add in your constructor
this.gridOptions = {
  columnDefs: this.createColumnDefs(),
  rowData: this.createRowData()
};
let eGridDiv:HTMLElement = <HTMLElement>document.querySelector('#myGrid');
new Grid(eGridDiv, this.gridOptions);
  • Define your content
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}
    ];
}

Upvotes: 1

Idris
Idris

Reputation: 369

I am using basic theme and it works for me. Below is the code for the same.

    //**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,
    }

Upvotes: 1

Related Questions