 | Level: Introductory Susan Gallagher (susan.gallagher@ie.ibm.com), Software Engineer, IBM
17 Jan 2006 This article describes the DataGrid component of the JWL (JSF Widget Library) that is available in Rational Application Developer v6.0.0.1. You can use this component and complementary rapid application development tools to build a Web application that allows the user to display and manipulate tabular data. This article explains how to create a rich, interactive, responsive Web application using the DataGrid component.
Introduction
IBM® Faces Client Components are part of the JWL (JavaServer Faces™ Widget Library). They allow you to build a Web application with rich components to display complex arrangements of data. The resulting application is zero footprint, using only HTML, CSS, and JavaScript™. The IBM Faces Client Components are an extension of JavaServer Faces (JSF, JSR 127). The IBM® Rational® Application Developer V6.0.0.1 (IRAD) tooling allows rapid application development with these components using drag and drop operations. Faces Client Components use separate data and components. The application's data is not streamed in place with the markup for the components. A separate set of data and components is defined. The data is streamed as a formal structure to the browser. The data model and instance data can then be updated at runtime on the client. The components are bound to this data at runtime and the content is rendered entirely in the browser. The user can work on the data that is on the client until a roundtrip to the server is really necessary for something like a page refresh. The resulting application will have enhanced performance and interactivity due to a reduced number of roundtrips to the server, longer interaction before page refresh, fast response time, and less bandwidth consumption. This programming model for Web applications is now often referred to as the Ajax pattern and is becoming very popular.
 |
Creating an application
The following steps explain how to create an application to use JWL components.
Create a new dynamic Web project in IRAD
- On the File menu, click New > Dynamic Web Project.
- Enter the project name in the Name box.
- Click Finish to create the dynamic Web project.
Create a new JavaServer Page
- In the new project, right-click the Web Content folder and choose New Faces JSP File.
- Enter a name for the page in the File Name box.
- Choose Basic with client-side data caching in the Model drop-down box.
- Click Finish to add the JavaServer Page™ JSP to the dynamic Web project.
Import bean to use as your data source
- Download SampleModel.zip at the end of this article.
- In the new project, right-click the Java Source folder and choose Import.
- In the Import box, select Zip File from the list of options and click Next.
- Click the Browse button to locate the SampleModel.zip file that you have just downloaded.
- Make sure all the folders that appear in the left pane are selected.
- Click Finish to import the beans into your project.
- The classes available to your application are structured as shown in Figure 1.
Figure 1. UML diagram
Create page data
- In the Page Data view, right-click and select New Java Bean.
- Click on the button beside the Class textfield and type in the name of the root class, Root in this case.
- Type a name for the bean in the Name textfield.
- Click Finish to create the Page Data.
Create client data
- In the Client Data view, right-click and select New Client Data.
- Choose your Page Data from the list and click OK.
- Expand the nodes that appear in the Client Data view to see the beans and the properties available to your application.
Add a DataGrid to the page
- Drag the DataGrid component from the Faces Client Components palette onto your JSP.
- Expand the nodes in the Client Data view.
- Choose the Positions array and drag it onto the DataGrid in the JSP.
- The DataGrid is now bound to the Positions array and will display the records in that array.
Add an Output box to the page
- Drag the Output component from the Faces Components palette and drop onto your JSP.
- Expand the nodes in the Client Data view.
- Choose the Symbol variable from the Positions array and drag and drop onto the DataGrid in the JSP.
- The DataGrid is now bound to the Symbol variable, and will display the Symbol of the first Position Object in that array.
Run the application on a server
- Right-click the JSP and choose Run > Run on Server.
- The JSP page will run in the Web Browser view in IRAD.
- The DataGrid will display a list of the Positions intialised in the Root bean you imported into the project, as can be seen in Figure 2.
- The Output box will display the Symbol variable from the first of the Positions intialised in the same Root bean.
Figure 2. JWL DataGrid
Customising the DataGrid component
The JWL DataGrid is one of the Faces Client Components. It works on the client-side, allowing the user to sort, filter, scroll, page through, and manipulate data without causing any page refreshes.
When developing a JWL application, you can view the source of the components that you have added to the JSP page. This is available by opening the JSP page in IRAD and switching to the source tab of the JSP. The following is a snippet from the JSP that was created in the previous steps, and shows the source for the DataGrid component.
DataGrid Source View
<odc:dataGrid allowRowAddAndDelete="false" styleClass="dataGrid"
showSelectionColumn="false" pageSize="10" id="dataGrid2">
<odc:clientBinder
value="#{pc_Page1.myBean.users[0].portfolios[0].positions}">
</odc:clientBinder>
<odc:dataGridCol
attributeName="price" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="purchaseDate" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="quantity" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="refNum" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="symbol" readOnly="true">
</odc:dataGridCol>
</odc:dataGrid>
|
Not all the features available in the DataGrid have been used in the example above. The following is a table of all the attributes in the odc:dataGrid tag that are available to customise the DataGrid. They can be changed by editing the Source view, or by using the tools in the Properties view in IRAD.
odc:dataGrid Attributes
| Attribute Name | Description |
|---|
| id | You can override the ID assigned to the DataGrid when it was dragged onto the page | | rowFilter | Use rowFilter to specify a JavaScript expression used to filter out rows in the DataGrid. This expression should return a boolean value to tell the DataGrid if a row should be shown. For example, this.eGet('symbol')=='IBM' | | styleClass | Use styleClass to specify a style class for the DataGrid. Specify the style class name as a String. This name is used as the prefix of each style class used by data grid component. For instance, if you enter dataGrid2 in the field, .dataGrid2_wpsTableHighlightedRow is used as the style of the selected row in the DataGrid | | width | Use width to specify the DataGrid width in pixels or percentage | | navBarPosition | no navBarPosition attribute or navBarPosition = 0 - footer navBarPosition = 1 - header navBarPosition = 2 - header and footer | | allowRowAddAndDelete | Set to TRUE to allow the user to create or delete table rows | | showRowIndex | Set to TRUE to display row index numbers in a column | | showSelectionColumn | Set to TRUE to display the column that allows the user to select rows | | selectionColName | Enter a String as the Label for the selection column | | selectionColAlignment | Enter LEFT, CENTER, or RIGHT as the selection column alignment | | selectionColWidth | Specify the selection column width in pixels | | multiSelection | Set to TRUE to allow multiple selection in the selection column using checkboxes. Set to FALSE to allow single selection using radio buttons | | pageSize | Turns on paging in the DataGrid. It is the number of records displayed on each page in the DataGrid | | scrollSize | Turns on scrolling in the DataGrid. Set equal to the size of the scrollable area in any of the CSS units of measurement | | allowColumnHide | Set to TRUE to turn on column hide and show functionality, which allows the user to hide individual columns and to show all columns again |
 |
Customising the DataGrid Columns
You can use the Properties view in IRAD to specify the attributes for each column in the DataGrid. Switch to a column by selecting it from the Columns view, or by clicking on a column in the Design view. You can also change the attributes for each column by changing them in the Source view of the JSP in IRAD.
The following is a code snippet from the JSP that was created in the previous steps, and shows the tags created for all the DataGrid columns.
DataGrid Columns Source view
<odc:dataGridCol
attributeName="price" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="purchaseDate" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="quantity" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="refNum" readOnly="true">
</odc:dataGridCol>
<odc:dataGridCol
attributeName="symbol" readOnly="true">
</odc:dataGridCol>
|
Not all the features available for the DataGrid Columns have been used in the example above. The following is a table of all the attributes in the odc:dataGridCol tag that are available to customise the DataGrid columns. They can be changed by editing the Source view, or by using the tools in the Properties view in IRAD.
odc:dataGridCol Attributes
| Attribute Name | Description |
|---|
| id | Used to assign an IDto a column | | attributeName | Attribute used for this column. If a data object is already bound to this component, the drop-down list contains the attributes available for this column | | width | Width in pixels of this column | | colHead | Label used for this column | | alignment | Enter LEFT, CENTER, or RIGHT as the column alignment | | underline | Underlines the content of the cell | | readOnly | Set to FALSE to make content editable |
DataGrid events
The JWL allows you to create various handlers for component events. There are ten events defined for the DataGrid. There are two types of event handler that can be used directly through the tooling, or you can write custom handlers to manipulate the controls. I will explain the two handlers that are available, and also how to create a custom handler.
SelectAndActivate handler
All the controls on a page are automatically linked if they have been bound to the same client data. An update to data in one control will immediately be reflected in the other controls without a page refresh or roundtrip to the server. With this handler, however, you can configure the DataGrid so an event can alter to which data another control is bound. For example, highlighting a row in a DataGrid of Position Objects can change which Position Object an Output box is bound to and displays. The SelectAndActivate handler allows you to set a selected Object as the root in a target component. In this case, the Position Object was selected and the Output box is the target component.
Add a SelectAndActivate Event Handler to the DataGrid using IRAD tooling
- In the JSP Design view, select the DataGrid and then open the QuickEdit view.
- Click on the Highlight event, which will trigger the SelectAndActivate Event Handler when the user highlights a row in the DataGrid.
- In the Code Snippet area, right-click and choose Insert Snippet > Set selected object as root in target component.
- In the Source Object dropdown box, choose the Position Object.
- In the Target components dropdown box, choose the Output box which you added to your application earlier and bound to Symbol from the Position Object.
- The Output box will display the Symbol from the Position Object in a DataGrid row when that row is highlighted.
SelectAndSet handler
The other standard event handler binds the selected Object to another Object. This may seem similar to SelectAndActivate, but with SelectAndSet the selected Object in the source control is bound to an Object in the data model, rather than a specific control. Any control which is bound to this target Object will have its contents changed. This has the benefit that controls are not coupled. The downside is that a conduit Object is needed in the model. The conduit Object is a placeholder Object which is added to the model for this purpose only. The result of a event triggers the SelectAndSet handler to populate the conduit Object. Other components on the page are bound to the conduit Object and the values they display change when the value of the conduit Object changes.
Add a SelectAndSet Event Handler to the DataGrid using IRAD tooling
- In the JSP Design view, select the DataGrid and then open the QuickEdit view.
- Click the Select event that will trigger the SelectAndSet Event Handler.
- In the Code Snippet area, right-click and choose Insert Snippet > Bind selected object to another object.
- In the Source Object dropdown box, choose the Position Object.
- Click on the icon beside the Bind To box to choose the Object you wish to bind the source Object to when the event is triggered.
- The display will be updated for every component that is bound to this target Object when the event is triggered.
Custom handler
You can write your own custom handler that can also be used for any of the ten events defined for the DataGrid. Any piece of JavaScript™ code can be used. The most common types of handler written for JWL components use the EObject APIs to gain more information about the component that has had an event triggered.
Add a custom event handler to the DataGrid using IRAD tooling
- In the JSP Design view select the DataGrid and then open the QuickEdit view.
- Click the Select event that will trigger your Custom Event Handler.
- In the Code Snippet area, enter the JavaScript you want executed when the event is triggered.
- This example uses JavaScript code to display an alert box containing details of the selected row when the event is triggered.
This will appear as follows in the Source view of the JSP. The odc:dataGrid tag is updated to include an attribute for the new event that calls func_1 when the event is triggered.
odc:dataGrid Tag
<odc:dataGrid allowRowAddAndDelete="false" styleClass="dataGrid"
showSelectionColumn="false" pageSize="10" id="dataGrid1"
onhighlight="return func_1(this, event);">
|
The code snippet that the user wrote is added as a function within a SCRIPT tag in the HEAD tag of the JSP.
Event Function
<SCRIPT type="text/javascript">
function func_1(thisObj, thisEvent) {
//use 'thisObj' to refer directly to this component instead of keyword this
//use 'thisEvent' to refer to the event generated instead of keyword event
//thisEvent.eobject is the object mapped to the currently highlighted row.
alert("We highlighted the row:"+ thisEvent.eobject.eGet('price')+
thisEvent.eobject.eGet('purchaseDate')+ thisEvent.eobject.eGet('quantity')
+ thisEvent.eobject.eGet('symbol') + thisEvent.eobject.eGet('refNum'));
}
</SCRIPT>
|
Figure 3 shows the results of this custom event handler being triggered in your application.
Figure 3. Custom event handler
The following is a table of event tags available to customise the DataGrid.
odc:dataGrid Events
| Event Name | Description |
|---|
| onhighlight | This event is triggered when a Datagrid row is highlighted, allowing the user to write JavaScript code in response to the event | | onfilter | This event is called when a new filter is set for the DataGrid. It allows the developer to add custom JavaScript code Fore example, to refresh the page) | | onpage | Triggered when a user pages through the DataGrid | | onselect | This event is called when the user selects a row in the DataGrid | | onselectall | This event is called when the user selects all the rows in the DataGrid | | onsort | This is triggered when the user sorts a column in the DataGrid, allowing the user to write JavaScript code to refresh the page or cause an alert box to appear | | onunselect | Triggered when the user unselects a row | | onunselectall | This event is called when all rows are unselected | | onRowAdd | This event is called when a row is successfully added to the DataGrid after the initial DataGrid has been created | | onRowRemove | Triggered when a row is successfully removed from the DataGrid |
 |
Summary
This article described the JWL DataGrid component. The article explains how to build a dynamic Web application using the JWL DataGrid, and how to customise the DataGrid. It also explains how to add events to the DataGrid component.
Download | Description | Name | Size | Download method |
|---|
| Sample code for this article | jwl_samplemodel.zip | 5KB | HTTP |
|---|
Resources Learn
Get products and technologies
Discuss
About the author  | |  | Susan Gallagher works at the IBM Dublin Software Lab. She graduated with a BSc. in Computer Applications from Dublin City University in 2002, and is a Sun-Certified Java Developer. She is currently a member of the JWL (JSF Widget Library) team, developing the DataGrid component. You can reach Susan at susan.gallagher@ie.ibm.com |
Rate this page
|  |