Data Table
The Data Table uses a simple table structure to display content. The table is configured to contain three row elements, such as a header, a content row, and a footer. The header and footer typically contain static elements, such as column titles, pagers, or just arbitrary one-off control instances. The content row usually contain a collection of individual controls that are bound to elements of a data source, and this row is then rendered repeatedly for each entry in the data source (once for every row in a view) when the Data Table is invoked as part of a live application.
Unlike a View Panel, however, all the controls contained in the Data Table must be added and bound manually, and certain other capabilities are simply not available, e.g. categorization. In essence, it is like a dumbed-down View Panel control, but it can be useful if you need to display simple nonhierarchical data in a customized fashion. You see an example of a good use case in this section.
To start with, try to present a regular view using a Data Table to get familiar with its features and behaviors. You should create a new XPage, say myDataTable.xsp, and drag-and-drop a Data Table control from the palette. Compared to the View Panel drag-and-drop experience, you might be underwhelmed with results. Basically, a shell of a table is created, and it's pretty much up to you to populate it with controls and bind these in a meaningful way.
Designer prompts you that a data source needs to be created if one does not already exist on the page, so for the purposes of this example, you should create a view data source targeting the xpAllDocuments view. This can be done in a number of ways, such as from the Data property panel on the XPage itself or using the Define Data Source combo box entry on the Data palette data source picker. Whatever your preferred route might be, simply pick the aforementioned view as the data source. Even though you now have a page containing a Data Table and a view data source, they are not connected and know nothing about each other. You can wire these together using the main Data Table property panel, as shown in Figure 9.22.
Figure 9.22 Connecting a Data Table to a view data source in Designer
With the Data Table entry selected in the Outline view, pick the newly created view data source instance ("viewAll") using the Data source combo box, and you also need to enter a Collection name. The collection name, "rowData" in this example, is used as the object to gain programmatic access to each row entry as it is being rendered—just as it was in the View Panel examples earlier. Rather than use server-side JavaScript in this case, however, you could just use simple Expression Language (EL) bindings. First, however, you need some controls to display the row data, so drag-and-drop a Computed Field from the Core Controls palette to the first cell in the middle row and then repeat the process for the adjacent table cell. These Computed Field instances can be selected and bound using EL expressions—or Simple data binding, as it is described in Designer's Value property panel and displayed in Figure 9.23. Bind the first field to the _MainTopicsDate column and the second field to the _Topics column.
Figure 9.23 Binding a Computed Field to a view data source element in Designer
The EL data binding markup generated by Designer has the following form. The name of the column is provided as a key to the row data entry:
#{rowData['_MainTopicsDate']}
You should also drop two Label controls from the palette directly into the two cells in the top row of the Data Table and change their values to Date and Topic, respectively. You can also assign the Data Table a width of 600 pixels for quick aesthetics using the Width and Units controls shown in Figure 9.22. After you complete this step, you are ready to preview or load this Data Table. The results should be just like the page you see displayed in Figure 9.24.
Figure 9.24 Data Table displaying data from xpAllDocuments view
The Data Table could do with a pager to split the rows into manageable chunks. The first step is to set the rows property of the Data Table to smaller number than its default value of 30 (for example, 10). Interestingly, the pager you have worked with up to now in the View Panel is not an intrinsic part of that control, but an independent entity that can be used with any of the view controls. The View Panel just happens to include a pager instance by default. To add a pager to the Data Table, look for the Pager control in the Core Controls palette and drag it into one of the footer cells. Then, activate the Pager property panel and attach it to the Data Table by picking the ID of the Data Table from the Attach to combo box—where Designer kindly enumerates a list of eligible candidate controls for you! At the same time, turn on partial refresh so that paging updates are performed using AJAX. The various property panel selections are shown in Figure 9.25.
Figure 9.25 Pager property panel
Because the Pager is capable of working with any view control, you must nominate a target container. The Partial refresh checkbox selection instructs XPages to update just the targeted view control via an AJAX request when a pager action is executed. This means that only the view data in the Data Table is refreshed when the end user navigates from one page to the next, which is obviously more efficient than refreshing the entire page every time.
The only problem with the pager right now is that it resides in the wrong place. It has been dropped into the footer cell of a column when it really needs to be in the footer of the Data Table itself. Unfortunately, the footer of the Data Table is not an identifiable drag-and-drop target in Designer, so you must go to the Source pane move the markup manually. Simply cut and paste the entire <xp:pager> tag from its current location so that it is a direct child of the Data Table. It should also be wrapped in a <xp:this.facets> tag—see the final markup in Listing 9.7.
To best illustrate the effect of the AJAX partial refresh, however, it is worthwhile adding two more Computed Fields to the XPage. Place the first Computed Field in one of the Data Table footer cells and then the second control can be dropped anywhere else on the page as long as it is outside the Data Table. Then, add the following server-side JavaScript as the computed value for both fields:
@Now().getMilliseconds();
Domino developers no doubt are familiar with the @Now() function, which returns the current data and time. The getMilliseconds() call expresses the time in milliseconds when the page is loaded. When you load or preview the page, both fields should display the same number. If you start navigating through the view data using the navigator, you notice that the Computed Field within the Data Table is updated with the current time milliseconds value while the field external to the Data Table is not. This demonstrates the efficient behavior of the partial refresh feature.
Figure 9.26 shows the updated XPage in action. The full markup is done for you in the dataTable.xsp XPage in Chapter9.nsf and is printed in Listing 9.7.
Figure 9.26 Data Table with partial refresh paging enabled
Listing 9.7. XSP Markup for SampleData Table
<?xml version="1.0" encoding="UTF-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core"> <!-- The data source defined at root level --> <xp:this.data> <xp:dominoView var="viewAll" viewName="xpAllDocuments"></xp:dominoView> </xp:this.data> <!-- The data table finds the data source using value prop --> <xp:dataTable id="dataTable1" rows="10" var="rowData" value="#{viewAll}" style="width:600px"> <xp:column id="column1"> <!-- column header and footer entries --> <xp:this.facets> <xp:label value="Date" id="label1" xp:key="header"></xp:label> <xp:label value="Internal Time Value" id="label3" xp:key="footer"></xp:label> </xp:this.facets> <!-- Bound to the date field using EL --> <xp:text escape="true" id="computedField1" value="#{rowData['_MainTopicsDate']}"> </xp:text> </xp:column> <xp:column id="column2" style="width:300px"> <xp:this.facets> <!-- column header and footer entries --> <xp:text escape="true" id="computedField3" xp:key="footer" value="#{javascript:@Now().getMilliseconds();}"> </xp:text> <xp:label value="Topic" id="label2" xp:key="header"></xp:label> </xp:this.facets> <!-- Bound to the Topic field using EL --> <xp:text escape="true" id="computedField2" value="#{rowData._Topic}"> </xp:text> </xp:column> <xp:this.facets> <xp:pager layout="Previous Group Next" id="pager1" for="dataTable1" xp:key="footer" panelPosition="left" partialRefresh="true"> </xp:pager> </xp:this.facets> </xp:dataTable> <!-- Table only used for layout alignment --> <xp:table style="width:600px;text-align:left"> <xp:tr><xp:td> <xp:label value="External Time Value" id="label4"> </xp:label></xp:td> <!-- external computed field --> <xp:td style="width:300px; text-align:left"> <xp:text escape="true" id="computedField4" value="#{javascript:@Now().getMilliseconds();}" style="text-align:left"></xp:text> </xp:td> </xp:tr> </xp:table> </xp:view>
Although working with the Data Table may be vaguely interesting, it must occur to you that what you have just built could be achieved using a View Panel control in a fraction of the time with just a few point-and-click operations. So, why bother with the Data Panel at all? The answer is that the Data Panel can be useful when you want to build a small bare bones tabular view with a highly customized user interface. Perhaps these use cases are not commonplace but they do occur. The next exercise serves as a good example.
Building a Mini Embedded Profile View using a Data Table
Carry out the following steps, drawing on what you learned in the current section up to this point:
- Create a new XPage called dtProfile.xsp and add a Data Table from the palette.
- Create a view data source targeting the xpAuthorProfiles view.
- Connect the Data Table to the data source and set its Collection name to "rowData" in the Data Table property sheet. This should result in a var="rowData" attribute being created in the underlying <xp:dataTable> tag.
- Append two new columns to the Data Table using the right mouse menu.
- Add a Computed Field to the 1st content cell; that is, first column, middle row.
- Bind this field to the From column in the data source using JavaScript:
rowData.getColumnValue("From")
- Add a link control for the palette to both the 2nd and 3rd cells in the content row.
- For the first link, activate the Link property panel and set the Label and Link type fields. For the label, enter "email" in the edit box, and then for the latter, add some server-side JavaScript to compute a URL. This is a mailto URL, created by simply concatenating a "mailto:" to the Email column value, as follows:
"mailto:" + rowData.getColumnValue("Email")
- Set the label for the second link to "Download" and compute its type in the same way as before, this time building a Domino resource image URL like this:
"/" + rowData.getUniversalID() + "/$FILE/" + rowData.getColumnValue("FileUpFilename")
- Drag-and-drop an image control to the fourth and final content row cell, using the Use an image placeholder radio button for now so that you can compute the image reference.
- In the Image property panel, compute the Image source using exactly the same server-side JavaScript as previously shown.
- For presentation purposes, select the All > Style cell in the property panel for each Data Table column and set this CSS rule:
text-align:center; vertical-align:middle
- In the same way, set the All > Style property for the Data Table itself to this:
width:400px;
You already practiced most of the 13 steps in one way or another when working through View Panel or Data Table examples, so only a few steps need any further explanation.
Step 6 simply returns the name of the author of the document. This is in Notes canonical form, so it would be more natural to present the common user name in this column instead. Experienced Domino developers instinctively know to do this using the @Name @Function, which can reformat Notes names in a number of ways. Although @Functions and other traditional building blocks are covered in more detail in Chapter 11, "Advanced Scripting," in the section, "Working with @Functions, @Commands, and Formula Language," it is no harm to start dabbling with some simple use cases at this stage according as the need arises. To do this, simply wrap the JavaScript binding command in with an @Name() call:
@Name("[CN]", rowData.getColumnValue("From"));
Step 9 uses JavaScript to build a Domino resource URL. The generic form of this URL is
/UNID/$FILE/filename
where the first part is an ID to identify the document to use, the second part indicates that the URL represents a file attachment resource, and the third part is the name of the attachment. This form of URL has been used in classic Domino web development for a long time. Back in Chapter 3, you learned about special IDs that Notes maintains to manage its databases and documents. The universal ID (UNID) is a 32-character hexadecimal representation that uniquely identifies a document. The profile documents in the Discussion template each contain a single image (or placeholder image) of the author and the name of this image file can be obtained from the FileUpFilename column in the xpAuthorProfiles view. Thus, a resource URL can be dynamically constructed for all registered users and this URL resolves the image and retrieves it from the profile documents for display in the Data Table. An example of a real live resource URL is highlighted in the status bar of the browser in Figure 9.27.
Figure 9.27 My Profile Page with Embedded Data Table
You are now ready to preview or load the new XPage. Chapter 9.nsf contains some sample profile documents, so you see these listed in the Data Table. The actual intention, however, is to display this Data Table as an embedded view in the My Profile page. To do this, you need to open the authorProfileForm custom control and copy/paste the markup from dtProfile.xsp to the bottom of the XPage, just before the final </xp:view> tag. Naturally, you do not copy the <xp:view> tag from dtProfile.xsp but just the Data Table and data source markup—everything you see in Listing 9.8. Figure 9.27 shows a snapshot of a My Profile page from Chapter9.nsf.
Listing 9.8. Data Table Displaying Profile Data
<xp:this.data> <xp:dominoView var="view1" viewName="xpAuthorProfiles"> </xp:dominoView> </xp:this.data> <xp:dataTable id="dataTable1" rows="30" value="#{view1}" var="rowData" style="width:400px"> <!-- style each column like this --> <xp:column id="column1" style="text-align:center; vertical-align:middle"> <!-- get the common user name --> <xp:text escape="true" id="computedField1"> <xp:this.value><![CDATA[#{javascript: @Name("[CN]", rowData.getColumnValue("From")); }]]></xp:this.value> </xp:text> </xp:column> <xp:column id="column2" style="text-align:center;vertical-align:middle"> <!-- return a mailto link --> <xp:link escape="true" text="e-mail ..." id="link2"> <xp:this.value><![CDATA[#{javascript:"mailto:" + rowData.getColumnValue("Email");}]]></xp:this.value> </xp:link> </xp:column> <xp:column id="column3" style="text-align:center; vertical-align:middle"> <!-- return Domino resource URL --> <xp:link escape="true" text="download..." id="link1"> <xp:this.value><![CDATA[#{javascript: "/" + rowData.getUniversalID() + "/$FILE/" + rowData.getColumnValue("FileUpFilename")}]]> </xp:this.value> </xp:link> </xp:column> <xp:column id="column4" style="text-align:center; vertical-align:middle"> <!-- use the same Domino resource URL for the image --> <xp:image id="image2" style="height:50px;width:50.0px"> <xp:this.url><![CDATA[#{javascript:"/" + rowData.getUniversalID() + "/$FILE/" + rowData.getColumnValue("FileUpFilename")}]]> </xp:this.url> </xp:image> </xp:column> </xp:dataTable>
Had you used a View Panel for this particular use case, you would have had to undo a lot of the features it gives you for free, such as pagers, column headers, and so on. You would also have had to customize the columns to display HTML and then return link and image HTML elements for three of the four columns. The Data Table actually simplifies the process by allowing you to drag-and-drop and arbitrary control into any content row cell and then just compute its value.
Another good example of Data Table usage is the File Download control. This out-of-the-box control is really a Data Table that has been adapted by the XPages runtime to display a simple table of any attachments contained in a nominated rich text field. Figure 9.28 shows the File Download control displaying some attachments in the Discussion application—it should be easy to see how this was built, given what you have just done to implement the embedded profile Data Table.
Figure 9.28 Example of the File Download control in the Discussion application
That is the Data Table, all done and dusted!