Dojo Data Grid – Part 2: Providing the Data with a REST Service
Data grids need a data store to obtain the information to display in the grid.
An easy way to set one up with XPages by using a REST Service control, which, like the Dojo Data Grid control, is available in the Extension Library and with 8.5.3 Upgrade Pack 1. You can find it in the Data Access section of the Controls view in Domino Designer:
When you add one to the page, you see this little icon on the Design tab:
Configuring the REST service
Follow these steps to set up the REST service to provide the data from a view:
2. Set the contentType (basics > service > contentType) to application/json
3. Set the defaultColumns property (basics > service > defaultColumns) to true. This provides the data from all columns in the underlying view, without you having to define them individually. (You can select individual columns or define your own via the columns property, if you choose, but this is the easiest way to get all columns from the view.)
4. Select the target view in the viewName property (basics > service > viewName)
Verifying the REST service
If you set the pathInfo property of the rest service (All Properties > basics > pathInfo), you will have a way to verify the data being returned by the service. This is a very helpful tool not only for troubleshooting, but for gaining an understanding of the data structure that you’re working with.
To verify the data, load the XPage that contains the REST service and include the pathInfo name in the URL, like this:
Here is an example of what you will see:
The pathInfo is not required when working with the Dojo Data Grid, but it is required when consuming the REST service from another library, like ExtJS.
Creating a Custom Column
In order to read data from the view entry, you will need to set the ‘var’ property of the REST service.
For example, if I wanted to add a column that combines the firstname and lastname columns from underlying view, I would need to take these steps:
- Set the var property of the REST service (All Properties > basics > service > var)
- Add a new column, but clicking the ‘+’ icon in the columns property (All Properties > basics > service > columns)
- Click the diamond to compute the value of the column (All Properties > basics > service > columns > restViewColumn > value)
- Set the name of the column (this will be the way to reference the column in the grid) under All Properties > basics > service > columns > restViewColumn > name
- Enter a script like this:
return restEntry.getColumnValue("firstname") + ' ' + restEntry.getColumnValue("lastname");
There does not appear to be a built-in way to get a handle to the underlying document, but you should be able to reference the unid from the view entry and use that to look up the document, should you need more information.
The REST service also has a systemColumns property, which allows you to select system values to include with each entry. Clicking the button in the property row will bring up a dialog box with checkboxes for the options to select.
Here are the options:
As you can see in the screen shots earlier in this post (with the REST service data), several of these system columns are included by default: unid, noteid, position, siblings, form.
If you select one or more system columns specifically, then only the selected system columns will be included. It appears that @entryid will always be included.
Ready for the grid!
How that you have a REST service providing data, you are ready to surface the grid! Tune in next time to see how to surface your first grid.