Grid Interface
This section details the public interface that your application can use to interact with the grid, including methods, properties and events.
The grid interface is the combination of the following items:
- Grid Properties: properties used to configure the grid, e.g.
pagination = true
. - Grid API: methods used to interact with the grid after it's created, e.g.
api.getSelectedRows()
. - Grid Events: events published by the grid to inform applications of changes in state, e.g.
rowSelected
. - Grid Callbacks: callbacks are used by the grid to retrieve required information from your application, e.g.
getRowHeight()
. - Row Node: each row in the grid is represented by a Row Node object, which in turn has a reference to the piece of row data provided by the application. The Row Node wraps the row data item. The Row Node has attributes, methods and events for interacting with the specific row e.g.
rowNode.setSelected(true)
.
Grid Options
The gridOptions
object 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, but if you are using a framework you might find it easier to use your framework's bindings. How to configure for a particular framework is explained further down this page.
The example below shows the different types of items available on gridOptions
.
var gridOptions = {
// PROPERTIES
// Objects like myRowData and myColDefs would be created in your application
rowData: myRowData,
columnDefs: myColDefs,
pagination: true,
rowSelection: 'single',
// EVENTS
// Add event 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, you will also have access to the grid API (api
) and column API (columnApi
) on the gridOptions
object as shown:
// refresh the grid
gridOptions.api.refreshView();
// resize columns in the grid to fit the available space
gridOptions.columnApi.sizeColumnsToFit();
Listening to Events
In addition to adding event listeners directly via the gridOptions
object, 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: either to use the onXXX()
method on the API (where XXX is replaced with the event name), or to register for the event. The latter option allows you to add multiple handlers for the same event. The following example demonstrates the two options:
// create handler function
function myRowClickedHandler(event) {
console.log('The row was clicked');
}
// option 1: use the API
gridOptions.onRowClicked = myRowClickedHandler;
// option 2: register the handler
gridOptions.api.addEventListener('rowClicked', myRowClickedHandler);
Events Are Asynchronous
Grid events are asynchronous so that the state of the grid will be settled by the time your event callback gets invoked.
Default Boolean Properties
Where the property is a boolean (true
or false
), then false
(or left blank) is the default value. For this reason, on / off items are presented in a way that causes the most common behaviour
to be used when the value is false
. For example, suppressCellSelection
is named as such because most people will want cell selection to be enabled.
React
The gridOptions
are fully available for React. However, you can also take advantage of the properties and events provided by ag-Grid's React Component. This is done as follows:
- Properties: properties are defined by passing React props down to ag-Grid.
- Callbacks: callbacks are also defined using React Props.
- Event Handlers: event handlers are also defined using 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:
<AgGridReact
ref="agGrid" // useful for accessing the component directly via ref
rowSelection="multiple" // simple attributes, not bound to any state or prop
// 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)}
// inside onGridReady, you receive the grid APIs if you want them
onGridReady={this.onGridReady.bind(this)}
/>
The APIs are also accessible through the component itself. For example, above the ref
is given as 'myGrid'
which then allows the API to be accessed like this:
<button onClick={() => this.refs.agGrid.api.deselectAll()}>Clear Selection</button>
Next Steps
That's it, Doc! Now you know how to interface with the grid. Go now and find out about all the great properties, methods, callbacks and events you can use.
- Grid Options
- Listening to Events
- Events Are Asynchronous
- Default Boolean Properties
- Vanilla JavaScript
- Polymer
- Properties
- Properties on ag-grid-polymer
- Events
- Events on ag-grid-polymer
- Events via the gridOptions property
- Events via Event Listeners on an instance of ag-grid-polymer
- Events via direct property access on an instance of ag-grid-polymer
- Grid API
- Web Components
- React
- Angular
- VueJS
- Next Steps