Guide applies to: classic

Grid Configurations

Ext.grid.Panel is one of the centerpieces of ExtJS. It’s an incredibly versatile component that provides an easy way to display, sort, group, and edit data.

Basic Grid Panel

Basic Grid Panel

Let’s get started by creating a basic Ext.grid.Panel. Here’s all you need to know to get a simple grid up and running:

Model and Store

Ext.grid.Panel is simply a component that displays data contained in a Ext.data.Store. Ext.data.Store can be thought of as a collection of records, or Ext.data.Model instances.

The benefit of this setup is separating our concerns. Ext.grid.Panel is only concerned with displaying the data, while Ext.data.Store takes care of fetching and saving the data using Ext.data.proxy.Proxy.

First, we need to define a Ext.data.Model. A model is just a collection of fields that represents a type of data. Let’s define a model that represents a “User”:

  1. Ext.define('User', {
  2. extend: 'Ext.data.Model',
  3. fields: [ 'name', 'email', 'phone' ]
  4. });

Next let’s create a Ext.data.Store that contains several “User” instances.

  1. var userStore = Ext.create('Ext.data.Store', {
  2. model: 'User',
  3. data: [
  4. { name: 'Lisa', email: '[email protected]', phone: '555-111-1224' },
  5. { name: 'Bart', email: '[email protected]', phone: '555-222-1234' },
  6. { name: 'Homer', email: '[email protected]', phone: '555-222-1244' },
  7. { name: 'Marge', email: '[email protected]', phone: '555-222-1254' }
  8. ]
  9. });

For sake of ease, we configured Ext.data.Store to load its data inline. In a real world application, you would most likely configure the Ext.data.Store to use an Ext.data.proxy.Proxy to load data from the server.

Grid Panel

Now, we have a model, which defines our data structure. We have also loaded several model instances into an Ext.data.Store. Now we’re ready to display the data using Ext.grid.Panel.

In this example, we configured the Grid with renderTo to immediately render the Grid into the HTML document.

In many situations, the grid will be a descendant of Ext.container.Viewport, which means rendering is already handled.

  1. Ext.create('Ext.grid.Panel', {
  2. renderTo: document.body,
  3. store: userStore,
  4. width: 400,
  5. height: 200,
  6. title: 'Application Users',
  7. columns: [
  8. {
  9. text: 'Name',
  10. width: 100,
  11. sortable: false,
  12. hideable: false,
  13. dataIndex: 'name'
  14. },
  15. {
  16. text: 'Email Address',
  17. width: 150,
  18. dataIndex: 'email',
  19. hidden: true
  20. },
  21. {
  22. text: 'Phone Number',
  23. flex: 1,
  24. dataIndex: 'phone'
  25. }
  26. ]
  27. });

And that’s all there is to it.

We just created an Ext.grid.Panel that renders itself to the body element. We also told the Grid panel to get its data from the userStore that we previously created.

Finally, we defined the Grid panel’s columns and gave them a dataIndex property. This dataIndex associates a field from our model to a column.

The “Name” column has a fixed width of “100px” and has sorting and hiding disabled. The “Email Address” column is hidden by default (it can be shown again by using the menu on any other column header). Finally, the “Phone Number” column flexes to fit the remainder of the Grid panel’s total width.

For a larger example, see the Basic Grid Example.

Renderers

You can use the renderer property of the column config to change the way in which data is displayed. A renderer is a function that modifies the underlying value and returns a new value for display. Some of the most common renderers are included in Ext.util.Format, but you can write your own as well:

  1. columns: [
  2. {
  3. text: 'Birth Date',
  4. dataIndex: 'birthDate',
  5. // format the date using a named method from the ViewController
  6. renderer: 'renderDate',
  7. },
  8. {
  9. text: 'Birth Date',
  10. dataIndex: 'birthDate',
  11. // format the date using a formatter from the Ext.util.Format class
  12. formatter: 'date("m/d/Y")',
  13. },
  14. {
  15. text: 'Email Address',
  16. dataIndex: 'email',
  17. // format the email address using a custom renderer
  18. renderer: function(value) {
  19. return Ext.String.format('<a href="mailto:{0}">{1}</a>', value, value);
  20. }
  21. }
  22. ]

See the Kitchen Sink’s Basic Grid for a live demo that uses custom renderers.

Navigation

In accordance with accessibility guidelines, grid cells accept focus, and the focus rendition my be specified in the theme.

Arrow keys navigate the focus position in two dimensions. TAB tabs out of the grid into the following focusable element.

This is known as Navigable Mode, and is the default mode for handling focus within a grid.

If there are focusable elements within a cell (Such as [[Ext.grid.column.Action action columns]] then the actionable items may be accessed by using the ENTER or F2 key to enter Actionable Mode wherein focus navigation takes place within cells using the TAB key.

Cell editing which is discussed below is a special case of Actionable Mode.

ESC or F2 exits actionable mode, and focus pops up to the encapsulating cell of the recently focused actionable item.

See https://www.w3.org/TR/wai-aria-practices/#grid for details.

Selection Models

Grid panels can be used to simply display data. However, it is often necessary to interact with the Grid’s data. All Grid panels have an Ext.selection.Model, which determines how data is selected. The most versatile Selection Model is Ext.grid.selection.SpreadsheetModel, which may be configured to select cells, rows, or columns, and to optionally display a selection checkbox if selecting rows.

See the Kitchen Sink’s Spreadsheet Model for an example.

Other selection models include Ext.selection.RowModel, where entire rows are selected, and Ext.selection.CellModel, where individual cells are selected. These are less flexible than the SpreadsheetModel.

Grid panels use Ext.selection.RowModel by default, but it’s easy to switch to an Ext.grid.selection.SpreadsheetModel:

  1. Ext.create('Ext.grid.Panel', {
  2. selectionModel: 'spreadsheet',
  3. store: ...
  4. });

Editing

Grid panel has built-in support for editing. Let’s look at the two main editing modes - row editing and cell editing.

Cell Editing

Cell editing allows you to edit the data in a Grid panel one cell at a time. The first step in implementing cell editing is to configure an editor for each Ext.grid.column.Column in your Grid Panel that should be editable. This is done using the Ext.grid.column.Column#cfg-editor config. The simplest way is to specify just the xtype of the field you want to use as an editor:

  1. Ext.create('Ext.grid.Panel', {
  2. ...
  3. columns: [
  4. {
  5. text: 'Email Address',
  6. dataIndex: 'email',
  7. editor: 'textfield'
  8. }
  9. ]
  10. });

If you need more control over how the editor field behaves, the Ext.grid.column.Column#cfg-editor config can also take a config object for a Field. For example, if we are using a Ext.form.field.Text and we want to require a value:

  1. columns: [
  2. text: 'Name',
  3. dataIndex: 'name',
  4. editor: {
  5. xtype: 'textfield',
  6. allowBlank: false
  7. }
  8. [

You can use any class in the “Ext.form.field.*“ package as an editor field. Lets suppose we want to edit a column that contains dates. We can use a Ext.form.field.Date editor:

  1. columns: [
  2. {
  3. text: 'Birth Date',
  4. dataIndex: 'birthDate',
  5. editor: 'datefield'
  6. }
  7. ]

Any Ext.grid.column.Column in a Ext.grid.Panel that do not have a ] configured will not be editable.

Now that we’ve configured which columns we want to be editable, to enable editing we need to configure the Ext.grid.Panel with a Ext.grid.plugin.CellEditing:

  1. Ext.create('Ext.grid.Panel', {
  2. ...
  3. plugins: [{
  4. ptype: 'cellediting ',
  5. clicksToEdit: 1
  6. }]
  7. });

And that’s all it takes to create an editable Grid using cell editing. See Cell Editing for a working example.

Grid Configurations - 图2

Row Editing

Row editing enables you to edit an entire row at a time, rather than editing cell by cell. Row editing works in exactly the same way as cell editing - all we need to do is change the plugin type to Ext.grid.plugin.RowEditing.

  1. Ext.create('Ext.grid.Panel', {
  2. ...
  3. plugins: [{
  4. ptype: 'rowediting',
  5. clicksToEdit: 1
  6. }]
  7. });

Grid Configurations - 图3

See Row Editing for a working example.

Grouping

Grid Configurations - 图4

Organizing the rows into groups is easy. First we specify a groupField property on our store:

  1. Ext.create('Ext.data.Store', {
  2. model: 'Employee',
  3. data: ...,
  4. groupField: 'department'
  5. });

Next, we configure a Grid with Ext.grid.feature.Grouping that will handle displaying the rows in groups:

  1. Ext.create('Ext.grid.Panel', {
  2. ...
  3. features: [{ ftype: 'grouping' }]
  4. });

See the Kitchen Sink’s Grouping Grid Panel for a live example.

Paging

Sometimes your data set is too large to display all on one page. Ext.grid.Panel supports displaying individual pages from the dataset using a Ext.toolbar.Paging, which loads pages using previous/next buttons.

Store Setup

Before we can set up paging on a Ext.grid.Panel, we have to configure the Ext.data.Store to support paging. In the below example we add Ext.data.Store#cfg-pageSize to the Ext.data.Store, and we configure our Ext.data.reader.Reader with a Ext.data.reader.Reader#cfg-totalProperty:

  1. Ext.create('Ext.data.Store', {
  2. model: 'User',
  3. autoLoad: true,
  4. pageSize: 100,
  5. proxy: {
  6. type: 'ajax',
  7. url : 'data/users.json',
  8. reader: {
  9. type: 'json',
  10. rootProperty: 'users',
  11. totalProperty: 'total'
  12. }
  13. }
  14. });

The Ext.data.reader.Reader#totalProperty config tells Ext.data.reader.Json where to get the total number of results in the JSON response. This Ext.data.Store is configured to consume a JSON response that looks something like this:

  1. {
  2. "success": true,
  3. "total": 4,
  4. "users": [
  5. { "name": "Lisa", "email": "[email protected]", "phone": "555-111-1224" },
  6. { "name": "Bart", "email": "[email protected]", "phone": "555-222-1234" },
  7. { "name": "Homer", "email": "[email protected]", "phone": "555-222-1244" },
  8. { "name": "Marge", "email": "[email protected]", "phone": "555-222-1254" }
  9. ]
  10. }

Paging Toolbar

Now that we’ve set up our Ext.data.Store to support paging, all that’s left is to configure a Ext.toolbar.Paging. You could put the Ext.toolbar.Paging anywhere in your application layout, but typically it is docked to the Ext.grid.Panel:

  1. Ext.create('Ext.grid.Panel', {
  2. store: userStore,
  3. columns: ...,
  4. dockedItems: [{
  5. xtype: 'pagingtoolbar',
  6. store: userStore, // same store GridPanel is using
  7. dock: 'bottom',
  8. displayInfo: true
  9. }]
  10. });

Grid Configurations - 图5

See the Paging Grid for a working example.

Buffered Rendering

Grids and Trees enable buffered rendering of extremely large datasets as an alternative to using a paging toolbar. Your users can scroll through thousands of records without the performance penalties of rendering all the records on screen at once.

Only enough rows are rendered to fill the visible area of the Grid with a little Ext.grid.Panel#cfg-leadingBufferZone overflow either side to allow scrolling. As scrolling proceeds, new rows are rendered in the direction of scroll, and rows are removed from the receding side of the table.

Grids use buffered rendering by default, so you no longer need to add the plugin to your Grid component.

See Big Data of Filtered Tree for working examples.

Embedded components.

Since ExtJS 5.0, developers have had the ability to embed components within grid cells using the Widget Column class.

In versions prior to 6.2.0, components embedded in this way had no access to the grid’s Ext.app.ViewModel. The field referenced by the column’s dataIndex was bound to the component’s defaultBindProperty.

In 6.2.0, components embedded in grids have access to the ViewModel and all the data within it. The ViewModel contains two row-specific properties:

  1. record
  2. recordIndex

Since ExtJS 6.2.0, developers have had the ability to configure a component to be displayed in an expansion row below (or, configurably, above) the data row.

The embedded component has access to the grid’s ViewModel.

See Using Components in Grids guide for more details.