Interfacing Overview

The interface to ag-Grid is modelled around standard DOM elements. This gives ag-Grid a consistent feel to already existing DOM elements (nice for everyone). It also has the added benefit of fitting nicely into Web Components, React and Angular.

Each interaction with the grid can be broken down into the following categories:

  • Properties: Properties are for changing the state inside the grid. These are analogous to properties on a DOM object.
  • Events: Events are for handling changes from the grid. These are analogous to events in the DOM.
  • Callbacks: Functions that you provide that the grid calls, as a way of modifying behaviour of the grid. This is the grid asking the application a question such as 'what color should this cell be', it is not a means to pass state into or out of the grid.
  • API: API into the grid, to provide features that are not representable via properties and events.

Grid Options

The gridOptions is a 'one stop shop' for the entire interface into the grid. The grid options can be used regardless of the framework you are using, however if you are using React, Angular or Web Components, you can achieve what grid options provides you with in other ways.

The example below shows the different type of items on the gridOptions.

var gridOptions = {

    // PROPERTIES - object properties, myRowData and myColDefs are created somewhere in your application
    rowData: myRowData,
    columnDefs: myColDefs,

    // PROPERTIES - simple boolean / string / number properties
    enableColResize: true,
    rowHeight: 22,
    rowSelection: 'single',

    // EVENTS - add event callback handlers
    onRowClicked: function(event) { console.log('a row was clicked'); },
    onColumnResized: function(event) { console.log('a column was resized'); },
    onGridReady: function(event) { console.log('the grid is now ready'); },

    // CALLBACKS
    isScrollLag: function() { return false; }
}

Once the grid is initialised, then the gridOptions will also have available the grid's api and columnApi as follows:

// get the grid to refresh
gridOptions.api.refreshView();

// get the grid to space out it's columns
gridOptions.columnApi.sizeColumnsToFit();

Two Ways of Event Listening

In addition to adding event listeners directly onto the gridOptions, it is possible to register for events, similar to registering for events on native DOM elements. This means there are two ways to listen for events, which again aligns with how DOM elements work. The first is to put an onXXX() method (where XXX = the event name) like in the example above, the second is to register for the event like in the following example:

// create handler function
function myRowClickedHandler(event) {
    console.log('the row was clicked');
}

// add the handler function
gridOptions.api.addEventListener('rowClicked', myRowClickedHandler);

Default Boolean Properties

Where the property is a boolean (true or false), then false (or leave blank) is the default value. For this reason, on / off items are presented in such was as the most common usage (which will be the default) is false, eg suppressCellSelection is worded as such as most people will want cell selection to be turned on.

Native Javascript and AngularJS 1.x

If you are using plain Javascript or AngularJS 1.x, then all of your interaction with ag-Grid will be through the gridOptions.

React

The gridOptions are fully available as stated above for React. However you can take advantage of React's properties and events provided by ag-Grids React Component. This is done as follows:

  • Properties: Properties are defined by passing React props down to ag-Grid.
  • Callbacks: Callbacks are also placed as React Props.
  • Event Handlers: Event handlers are again placed as React Props.
  • API: The grid API and column API are provided to you via the onGridReady() event callback.

So in summary, in React, everything is done via React Props. Here is an example:

<ag-grid-react-component

    // these are simple attributes, not bound to any state or prop
    rowHeight="22"
    rowSelection="multiple"

    // these are bound props, so can use anything in React state or props
    columnDefs={this.props.columnDefs}
    showToolPanel={this.state.showToolPanel}

    // this is a callback
    isScrollLag={this.myIsScrollLagFunction.bind(this)}

    // these are registering event callbacks
    onCellClicked={this.onCellClicked.bind(this)}"
    onColumnResized={this.onColumnEvent.bind(this)}"
    onGridReady={this.onGridReady.bind(this)}" // inside onGridReady, you receive the grid API's if you want them
/>

The API's are accessible through the component. This is useful in two situations. The first us by using an Angular ID. In the example above, the ID is given as '#myGrid' which then allows something like this:

<button (click)="agGrid.api.deselectAll()">Clear Selection</button>

Angular

The gridOptions are fully available as stated above for Angular. However you can take advantage of Angular's properties and events provided by ag-Grids Angular Component. This is done as follows:

  • Attributes: Attributes are defined as normal HTML attributes and set non-bound values.
  • Properties: Properties are defined as HTML attributes enclosed in square brackets and are Angular bound values.
  • Callbacks: Callbacks are actually a type of property, but by convention they are always functions and are not for relaying event information. They are bound as properties using square brackets. As callbacks are not events, they do not impact the Angular event cycle, and as such should not change the state of anything.
  • Event Handlers: Event handlers are defined as HTML attributes enclosed in normal brackets and are Angular bound functions.
  • API: The grid API and column API are accessible through the component.

All of the above (attributes, properties, callbacks and event handlers) are registered using their 'dash' syntax and not camelcase. For example, the property enableSorting is bound using enable-sorting. enable-sorting. The following example shows some bindings:

<ag-grid-angular
    // give an AngularJS ID to the grid
    #myGrid

    // these are boolean values, which if included without a value, default to true
    // (which is different to leaving them out, in which case the default is false)
    enable-sorting
    enable-filter

    // these are attributes, not bound, give explicit values here
    row-height="22"
    row-selection="multiple"

    // these are bound properties, bound to the AngularJS current context (that's what a
    // scope is called in Angular JS 2)
    [column-defs]="columnDefs"
    [show-tool-panel]="showToolPanel"

    // this is a callback
    [is-scroll-lag]="myIsScrollLagFunction"

    // these are registering event callbacks
    (cell-clicked)="onCellClicked($event)"
    (column-resized)="onColumnEvent($event)">
</ag-grid-angular>

The API's are accessible through the component. This is useful in two situations. The first us by using an Angular ID. In the example above, the ID is given as '#myGrid' which then allows something like this:

<button (click)="myGrid.api.deselectAll()">Clear Selection</button>

VueJS

The gridOptions are fully available as stated above for VueJS. However you can take advantage of VueJS's properties and events provided by ag-Grids VueJS Component. This is done as follows:

  • Attributes: Attributes are defined as normal HTML attributes and set non-bound values.
  • Properties: Properties are defined as HTML attributes prefixed with a colon : and are VueJS bound values.
  • Callbacks: Callbacks are actually a type of property, but by convention they are always functions and are not for relaying event information. They are bound as properties prefixed with a colon :. As callbacks are not events, they do not impact the VueJS event cycle, and as such should not change the state of anything.
  • Event Handlers: Event handlers are defined as HTML attributes prefixed with a colon : and are VueJS bound functions.
  • API: The grid API and column API are accessible through the component.

All of the above (attributes, properties, callbacks and event handlers) are registered using their 'dash' syntax and not camelcase. For example, the property enableSorting is bound using enable-sorting. enable-sorting. The following example shows some bindings:

<<ag-grid-vue
    // these are attributes, not bound, give explicit values here
    rowHeight="22"
    rowSelection="multiple"

    // these are boolean values
    // (leaving them out will default them to false)
    :enableColResize="true"
    :enableSorting="true"

    // these are bound properties
    :gridOptions="gridOptions"
    :columnDefs="columnDefs"

    // this is a callback
    :isScrollLag="myIsScrollLagFunction"

    // these are registering event callbacks
    :modelUpdated="onModelUpdated"
    :cellClicked="onCellClicked"
</ag-grid-vue>

The API's are accessible through the component. This is useful in two situations. The first us by using an Angular ID. In the example above, the ID is given as '#myGrid' which then allows something like this:

<button @click="myGrid.api.deselectAll()">Clear Selection</button>

Web Components

The gridOptions are fully available as stated above for Web Components. However you can take advantage of Web Components attributes for setting properties and also directly accessing the Web Components DOM object for interacting with the component. This is done as follows:

  • Attributes: Attributes are defined as normal HTML attributes and set non-bound values.
  • Properties: Properties are set directly onto the DOM object using javascript.
  • Callbacks: Callbacks, like properties, are also set directly on the DOM object using javascript.
  • Event Handlers: Event handlers are registered as normal DOM event handlers, using either addEventListener(eventName, handler) or assigning an onXXX method on the DOM object.
  • API: The grid API and column API are accessible through the DOM object.

Bindings are registered using their 'dash' syntax and not camelcase. For example, the property enableSorting is bound using enable-sorting.

Properties and callbacks are set using standard camelcase, no changes are done.

Web Components Events are all done using lowercase. This keeps the event handling consistent with native DOM elements, however breaks away from the camel case of how events are managed by the other frameworks ag-Grid supports.

This example shows setting up as a Web Component in HTML. Notice it's simliar to Angular, however no binding of properties or event handling.

<ag-grid-angular
    // normal id for CSS selector inside Javascript
    id="myGrid"

    // these are boolean values, which if included without a value, default to Yes
    enable-sorting
    enable-filter

    // these are attributes, not bound, give explicit values here
    row-height="22"
    row-selection="multiple"
</ag-grid-angular>

Then the callbacks and event handling are done in the Javascript as follows:


var myGrid = document.querySelector('#myGrid');

// calling a method directly on the ag-Grid DOM element.
// calling setGridOptions starts up the grid and is mandatory (even if gridOptions is empty)
myGrid.setGridOptions(gridOptions);

// add events to grid option 1 - add an event listener
myGrid.addEventListener('columnresized', function(event) {
    console.log('got an event via option 1');
});

// add events to grid option 2 - callback on the element
myGrid.oncolumnresized = function(event) {
    console.log('got an event via option 2');
};

// add events to grid option 3 - callback on the grid options
// remember we can still use everything in gridOptions, the
// Web Components features are all in addition
gridOptions.onColumnResized = function(event) {
    console.log('got an event via option 3');
};

// call something on the API
myGrid.api.refreshView();
myGrid.columnApi.sizeColumnsToFit();

// change a property
myGrid.quickFilterText = 'sandy';
myGrid.showToolPanel = true;

Aurelia

The gridOptions are fully available as stated above for Aurelia. However you can take advantage of Aurelia's properties and events provided by ag-Grids Aurelia Component. This is done as follows:

  • Events: All data out of the grid comes through events. These use Aurelia event bindings eg model-updated.call="onModelUpdated()". As you interact with the grid, the different events are fixed and output text to the console (open the dev tools to see the console).
  • Properties: All the data is provided to the grid as Aurelia bindings. These are bound onto the ag-Grid properties bypassing the elements attributes. The values for the bindings come from the parent controller.
  • Attributes: When the property is just a simple string value, then no binding is necessary, just the value is placed as an attribute eg row-height.bind="22".
  • Grid API via IDs: The grid in the example is created with an id by marking it with #agGrid. This in turn turns into a variable which can be used to access the grid's controller. The buttons Grid API and Column API buttons use this variable to access the grids API (the API's are attributes on the controller).
  • Changing Properties: When a property changes value, Aurelia automatically passes the new value onto the grid. This is used in the following locations:
    a) The 'quickFilter' on the top right updates the quick filter of the grid. b) The 'Show Tool Panel' checkbox has it's value bound to the 'showToolPanel' property of the grid. c) The 'Refresh Data' generates new data for the grid and updates the rowData property.

The example has ag-Grid configured through the template in the following ways:

// notice the grid has an id called agGrid, which can be used to call the API
<g-grid-aurelia #agGrid class="ag-fresh"
    // items bound to properties on the controller
    grid-options.bind="gridOptions"
    column-defs.bind="columnDefs"
    show-tool-panel.bind="showToolPanel"
    row-data.bind="rowData"

    // boolean values 'turned on'
    enable-col-resize
    enable-sorting
    enable-filter
    group-headers
    suppress-row-click-selection
    tool-panel-suppress-groups
    tool-panel-suppress-values
    debug

    // simple values
    row-height.bind="22"
    row-selection="multiple"

    // event callbacks
    model-updated.call="onModelUpdated()"
    cell-clicked.call="onCellClicked($event)"
    cell-double-clicked.call="onCellDoubleClicked($event)"
    cell-context-menu.call="onCellContextMenu($event)"
    cell-value-changed.call="onCellValueChanged($event)"
    cell-focused.call="onCellFocused($event)"
    row-selected.call="onRowSelected($event)"
    selection-changed.call="onSelectionChanged()"
    before-filter-changed.call="onBeforeFilterChanged()"
    after-filter-changed.call="onAfterFilterChanged()"
    filter-modified.call="onFilterModified()"
    before-sort-changed.call="onBeforeSortChanged()"
    after-sort-changed.call="onAfterSortChanged()"
    virtual-row-removed.call="onVirtualRowRemoved($event)"
    row-clicked.call="onRowClicked($event)"
    ready.call="onReady($event)"

    column-everything-changed.call="onColumnEvent($event)"
    column-row-group-changed.call="onColumnEvent($event)"
    column-value-changed.call="onColumnEvent($event)"
    column-moved.call="onColumnEvent($event)"
    column-visible.call="onColumnEvent($event)"
    column-group-opened.call="onColumnEvent($event)"
    column-resized.call="onColumnEvent($event)"
    column-pinned-count-changed.call="onColumnEvent($event)">
</ag-grid-aurelia>

Next Steps...

And that's it Doc, now you know how to interface with the grid. Go now and find out about all the great attributes, properties, callbacks and events you can use. Interact well. Be safe. Don't do drugs.