Setting up Ag-grid on AngularJS version 1.6 - A Comprehensive Guide

I went through the tutorial to set up agGrid for AngularJS with TypeScript instead of JavaScript. Here's what I did:

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

After setting it up in my HTML, the grid was visible:

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

However, I noticed that the CSS wasn't showing up. https://i.sstatic.net/ZYzoe.png

To fix this, I tried adding some required CSS files:

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

Unfortunately, it still didn't work. Any suggestions? Thank you

Answer №1

The basic theme has been a perfect fit for my needs. Here is the code snippet that I used:

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

Answer №2

After thorough research through various sections of the official documentation, I have finally discovered how to effectively use it.

Here are the simple steps for easy implementation:

  • First, install the necessary dependency by running npm install ag-grid-community
  • In your app.ts file:
    • Include the reference
      var AgGrid = require('ag-grid-community');
    • Initialize AgGrid with Angular 1
      AgGrid.initializeAgGridWithAngular1(angular);
    • Add it to the main module

In your class or component:

  • Import the Grid and GridOptions from "ag-grid-community":
    import { Grid, GridOptions} from "ag-grid-community";
  • Also, don't forget to include the necessary styles (You can also add them to vendor.ts)
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-balham.css";

Now, you're ready to utilize your Grid!

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the significance of the #/ symbol in a URL?

Currently, I am developing Ruby on Rails web applications. The URL of my webpage appears as follows: http://dev.ibiza.jp:3000/facebook/report?advertiser_id=2102#/dashboard From this link, I have identified that the advertiser_id is 2102 but I am unsure a ...

TypeScript: safely reassigning object properties for type safety

What I aim to achieve I am looking to create a JavaScript function that can remap property names of the first argument using the second argument. The goal is to utilize this remap function to generate query string parameters. For instance, transforming { ...

Is it possible to use line breaks to differentiate properties?

The handbook addresses The handbook states that you can separate properties using , or ;, and the last separator is optional in either case. Is it possible to use line breaks to separate object properties like this? If so, where is this information docu ...

Issue: "contains method is not supported" in Ionic 2

I'm currently working on a code to validate the contents of my input field, but I've encountered an issue with using the contains function. Here's the TypeScript function I have written: checkFnameFunction(name){ if(name.contains("[a-z ...

Exploring the functionality of the `super()` method in TypeScript

I'm trying to enhance the standard JavaScript Error class by adding another property called code, but for some reason, TypeScript is not allowing me to do so. Here is the code snippet: export class HttpError extends Error { public message: string ...

Error: Oops! The super expression can't be anything other than null or a function in JavaScript/TypeScript

I am facing an issue with class inheritance in my code. I have a class A that extends class B, which in turn extends class C. Whenever I try to create a new instance of class A within a function, I encounter the following error message: Uncaught TypeError: ...

Ways to utilize the useRef method within the useContext hook while implementing Typescript

Struggling to incorporate useRef into my global Next.js useContext function while utilizing TypeScript. Attempted approach and encountered errors: interface tripAttributes{ tripTitle: string } const initialTripState: tripAttributes = { tripTitle ...

Retrieve JSON data upon refreshing the page

In my blogging application, each post has its own unique permalink structure, such as /post/Dh3hdjs* where Dh3hdjs* represents the specific permalink. However, I am facing an issue where after successfully creating a post and being redirected to the specif ...

Implementing a directive within the compile function of another directive

I am seeking a way to dynamically insert the <confirmation> element into the DOM using the updater directive. In my actual application, I have set it up to listen for an event and trigger accordingly. All I require is for this element to be inserted ...

Utilizing ng-grid to pass variables within cellTemplate

My ng-grid is connected to a SharePoint list, but I'm facing an issue where the list returns an ID number instead of the user name when populating a field with a user name. To solve this issue, I have created a function that converts the ID to a user ...

Utilizing Sharepoint Online SPFX Web parts with React: A guide to selecting scripts dynamically based on environment requirements

Would it be possible for me to dynamically choose which script to utilize in my web component? This is how I currently have my imports set up: import * as jQuery from 'jquery'; import 'jqueryui'; Here's what I am aiming to achie ...

Using Angular and Laravel to display JSON data extracted from a MySQL database

I am currently retrieving data from MySQL using Laravel query builder, converting it to JSON format as per the suggestion of W3schools. However, when trying to display the fetched data using AngularJS, I end up with a blank page. Laravel route Route::ge ...

Enhancing your AngularJs fullstack setup with ng-grid and compass integration

I have successfully installed AngularJS using the fullstack installer available at https://github.com/DaftMonk/generator-angular-fullstack Now, I am looking to include ng-grid in my project. After running npm install ng-grid and having the files copied to ...

I successfully installed Angular CLI, but I'm encountering an error indicating that it is not recognized in the command prompt

Recently, I installed Nodejs, followed by npm, and then angular cli as per instructions. However, when I run the command "ng --version" in Windows cmd or git bash, it returns: ng is not recognized as an internal or external command, operable program o ...

Testing the existence of Angular routes: Verifying the presence of a specific route

When setting up navigation, I am looking to include a button only if a certain route is accessible or a specific permission exists. I have sorted out the permissions aspect, but now I want to exclude pages based on configuration settings. It would be helpf ...

Updating the view manually is necessary when using AngularJS $routeProvider and resolve feature

I am facing issues with $routeProvider in updating the view automatically. Currently, I am working on converting a chapter 22 example from Adam Freeman's Pro Angular book, originally based on Deployd, to Express-Mongo. Strangely, the automatic refres ...

Challenges with CSRF tokens in Express

I am struggling to set up CSRF protection using csurf and express. My application utilizes Angular on the front end, so I believed that adding the following to my app would suffice: app.use(cookieParser('test secret')); app.use(cookieSession({ ...

Struggling to fetch information with Angular HttpClient from an API that sends back a JSON response with an array

As a beginner in Ionic and Angular, I am attempting to call an API and then showcase the team names within the template of my project. Despite following numerous tutorials and videos, I seem to be stuck as the JSON response returns an object with results f ...

AngularJS case-insensitivity match not functioning as expected

I came across a discussion about AngularJS and the case sensitivity of base href in this thread. Despite following the advice to use version 1.1.5, I am still facing the same issue. Angular continues to return the error message. This is what I have been t ...

Steps for retrieving the output of a sequelize query that is enclosed within a function

I am currently in the process of developing a platform that generates a distinct URL for each user. The functionality is as follows: Firstly, I check the database to determine if the user already possesses a URL, and if so, return it. If the user ...