Learn the basics of GridX, an open-source JavaScript library that makes it easy to create sophisticated interactive tables for the web.
What is GridX? GridX is a library created to display data stores in a table. It's a great alternative to Dojo's native dojox.grid. It comes with many modules that add additional features to a basic table and is designed to be easily extended by users' modules.
Basic GridX Examples
We'll start out using a memory store and then advance to using JSON REST stores and some of the more interesting features GridX provides.
Example with a Memory Store
The following makes a simple GridX table that displays three entries.
Figure 1: Simple GridX Example with Memory Store
The code that creates it is the following:
// GridX example with memory store
test_func: function() {
this.test = new Grid({
store: new MemoryStore({
data: [
{
id: 1, // unique id used by GridX functions.
name: "Randy",
location: "Raleigh"
},
{
id: 2,
name: "Arun",
location: "Austin"
},
{
id: 3,
name: "Susan",
location: "Rochester"
}
]
}),
cacheClass: "gridx/core/model/cache/Sync", //Cache
structure: [
{
id: "id"/*this.test.column.id*/,
field: "id"/*field in store data*/,
name: "ID" /*displayed in column header*/
},
{
id: "name",
field: "name",
name: "Name"
},
{
id: "location",
field: "location",
name: "Location"
}
],
modules: [ ColumnResizer ],
autoHeight: true
// needed for grids not put in the entire pane. otherwise grid doesn't display
});
this.set('content', this.test); // set the grid in the template
}
Figure 2: Simple GridX Code with Memory Store
Breaking this down, we gave the new GridX five parameters.
- Store: This store contains the data. In this example, we used a memory store, but in the next example, we will use a JSON REST store.
- CacheClass: This can be synchronous or asynchronous. Since we're using a memory store, we use the synchronous cache.
- Structure: This describes how the data will be displayed in the table. Each object in the array describes a column. The three parameters GridX needs are 1) a unique ID, 2) a field name from the data, and 3) a name for the column header. There are other parameters you may specify. We'll talk about them in later examples.
- Modules: I couldn't resist putting at least one in. These are optional, but they provide extended functionalities for the table. In this example, I included the Column Resizer module, which allows the table columns to be resized.
- AutoHeight: This parameter is not necessary, but it tells the table to calculate its height based on the number of rows it has. This creates a variable length table. If "autoHeight" is false (default), then the GridX will fill the space available to it and provide a scrollbar if the space required exceeds the space available.
Lastly, we placed the GridX in the pane. In this example, "this" referred to a content pane. Thus, we filled the content pane with the grid.
Example with a JSON REST Store
In this example, we used a JSON REST store, rather than a memory store, to get the data from a server.
Figure 3: Simple Example with a JSON REST Store
The code for this is as follows:
// GridX example with json store
test_func2: function() {
this.test = new Grid({
store: new JsonRestStore({
root: "images",
target: "ourUrl/images"
}),
cacheClass: "gridx/core/model/cache/Async",
structure: [
{
id: "id",
field: "id"/*field in store data*/,
name: "ID" /*displayed in column header*/
},
{
id: "name",
field: "name",
name: "Name"
},
{
id: "status",
field: "status",
name: "Status",
formatter: function(item) {
return item.status.toUpperCase();
}
}
]
});
this.set('content', this.test); // set the grid in the template
}
Figure 4: Simple Code with a JSON REST Store
Notice a couple of differences. First, since we didn't use auto height, the grid is larger than it needs to be, filling the space available to it. Second, since we're using a JSON REST store, the cache is asynchronous. Third, the status column has been formatted. The formatting is done in the optional structure formatter. The formatter is a function whose input is the data from the store; it returns a string with the formatted data. In this case, the status string was made uppercase.
The JSON REST store only needs two parameters: the root and the target. The data from the server is formatted like this:
{
"images":
[
{"status": "active", "name": "test", "id": "0e305793-5b35-4823-961b-573ac2bf89cd"},
{"status": "active", "name": "test", "id": "d712d2e4-2e18-4802-8e64-82b15a7ec61a"}
],
}
So "images" is the root of the store.
More Fun with GridX
Here are a few examples of things you can do with GridX.
Example with Clickable Rows
Calling a function when a row is clicked on requires only a few modifications to the code in Figure 3.
onRowClick: lang.hitch(this, function(e) {
alert("You clicked row " + e.rowId + "!"); // nothing selected
})
The onRowClick function will trigger the provided function every time a row is clicked on. There are also functions for cell select and column select.
Example with Selectable Rows
GridX provides modules for row selection and extended row selection. Since extended row select allows multi-select by swiping, it's considered more mobile-friendly. The functions under this.test.select.row allow for gathering data from the row selected.
modules: [ExtendedSelectedRow],
selectRowTriggerOnCell: true,
onRowClick: lang.hitch(this, function() {
alert("You clicked row " + this.test.select.row.getSelected()[0] + "!");
})
The getSelected function returns an array of row IDs. You may look up the row data by using the ID. For example:
this.test.model.byId(this.test.select.row.getSelected()[0]);
this.test.select.row.model.byId(this.test.select.row.getSelected()[0]);
Example with Status Icons
A common table design is to have an icon along some status text in a table column. This is simple to do with the GridX decorator and CSS.
{
id: 'status',
field: 'status',
name: 'Status',
decorator: function(formattedValue, rowId, visualIndex, cell) {
return '<div class="imageStatus' + formattedValue + '">'
+ formattedValue + '</div>';
}
}
The cascading style sheet contains classes with the icons. These classes are named according to the statuses returned from the server.
Wrapping It Up
With GridX, you can make sophisticated tables without having to reinvent the wheel. For more information, GridX has a great home page including API documentation and tutorials.
LATEST COMMENTS
MC Press Online