The JSP code fragment provided in this section can be used to build a sortable table from any collection of objects. This code example includes all of the functionality described in each of the previous sections about TableInfo.

<%-- Include this fragment to generate a sortable table.           --%>
<%-- tableItems is the array of collection of items to display.    --%>
<%-- tableInfo is the TableInfo object that describes the columns. --%>
<%-- tablePage is the jsp that contains the table.          --%>

<%@ taglib uri="http://www.atg.com/taglibs/daf/dspjspTaglib1_0" prefix="dsp" %>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %>

<dsp:page>
<HTML>
<BODY>

<dsp:importbean bean="/atg/dynamo/droplet/ForEach"/>
<dsp:importbean bean="/atg/dynamo/droplet/Format"/>
<dsp:importbean bean="/atg/dynamo/droplet/IsNull"/>
<dsp:importbean bean="/atg/dynamo/droplet/Switch"/>

<%--
  Handle updating sort directives if you were passed a sortColumn parameter
  and an optional sort direction in a sortDir parameter.
--%>

<dsp:droplet name="IsNull">
  <dsp:param name="value" param="sortColumn"/>

  <%-- If you don't have a sortColumn parameter, reset the sort info --%>

  <dsp:oparam name="true">
    <dsp:setvalue param="tableInfo.reset" value="${null}"/>
  </dsp:oparam>

  <%-- If you have a sortColumn parameter, it will be the index of a  --%>
  <%-- column in the TableInfo object.  Set its sort direction now. --%>

  <dsp:oparam name="false">

  <%-- Look to see if you got the optional sortDir parameter too. --%>
  <%-- If so, use it, else default to cycle_up_if_primary.       --%>

    <dsp:droplet name="IsNull">
      <dsp:param name="value" param="sortDir"/>

      <dsp:oparam name="true">
        <dsp:setvalue
        param="tableInfo.tableColumns[param:sortColumn].sortDirection"
        value="cycle_up_if_primary"/>
      </dsp:oparam>

      <dsp:oparam name="false">
        <dsp:setvalue
        param="tableInfo.tableColumns[param:sortColumn].sortDirection"
        paramvalue="sortDir"/>
      </dsp:oparam>
    </dsp:droplet>

  </dsp:oparam>
</dsp:droplet>

<%--
  Now you've dealt with the case where you got to this page by clicking on a
column heading to re-sort and redisplay the table, so you can go ahead and build
the table display using the updated sort parameters.
--%>

<dsp:droplet name="ForEach">
  <dsp:param param="tableItems" name="array"/>
  <dsp:param name="sortProperties" param="tableInfo.sortString"/>

  <dsp:oparam name="empty">
    There are no items in your list.
  </dsp:oparam>

  <%-- Generate column headings. Each heading is a link. Clicking      --%>
  <%-- on the link makes that column the primary sort column, cycling  --%>
  <%-- through sort directions if it was already the primary sort      --%>
  <%—column.                                                          --%>

  <dsp:oparam name="outputStart">
    <TABLE border=1 cellspacing=2 cellpadding=2>
    <TR>
    <dsp:droplet name="ForEach">
      <dsp:param name="array" param="tableInfo.tableColumns"/>
      <dsp:param name="sortProperties" value=""/>

      <dsp:oparam name="output">
     <%-- Extract useful properties from the current Column descriptor. --%>
        <dsp:setvalue paramvalue="element.heading" param="columnHeading"/>
        <dsp:setvalue paramvalue="element.sortPosition" param="sortPosition"/>
        <dsp:setvalue paramvalue="element.sortDirection" param="sortDirection"/>
        <TD align="center"><B>
           <dsp:getvalueof var="a92" param="tablePage"
           idtype="java.lang.String">
             <dsp:a href="${a92}">
               <dsp:param name="sortColumn" param="index"/>

    <%-- If the current column is the primary sort column, --%>
    <%-- you want to display + or - as a directional indicator to show --%>
    <%-- the sort direction for that column. --%>

               <dsp:droplet name="Switch">
                 <dsp:param name="value" param="sortPosition"/>

                 <%-- Primary sort column gets special treatment --%>
                 <dsp:oparam name="1">
                   <dsp:droplet name="Switch">
                     <dsp:param name="value" param="sortDirection"/>
                     <dsp:oparam name="ascending">
                       <dsp:valueof param="columnHeading"/>&nbsp[+]
                     </dsp:oparam>
                     <dsp:oparam name="descending">
                       <dsp:valueof param="columnHeading"/>&nbsp[-]
                     </dsp:oparam>
                     <dsp:oparam name="null">
                       <dsp:valueof param="columnHeading"/>
                     </dsp:oparam>
                   </dsp:droplet>
                 </dsp:oparam>

                 <%-- Others just display the column name --%>
                 <dsp:oparam name="default">
                <dsp:valueof param="columnHeading"/>
                </dsp:oparam>
               </dsp:droplet>
             </dsp:a>
           </dsp:getvalueof>
        </TD>
      </dsp:oparam>
    </dsp:droplet>
    </TR>
  </dsp:oparam>

  <%-- Generate a row per item --%>

  <dsp:oparam name="output">
    <dsp:setvalue paramvalue="element" param="currentItem"/>
    <TR>
    <%-- Here you are going to iterate over all of the property names
      you want to display in this row, using the bean Property
      servlet bean to display each value.
     --%>

    <dsp:droplet name="ForEach">
      <dsp:param name="array" param="tableInfo.tableColumns"/>
      <dsp:param name="sortProperties" value=""/>
      <dsp:oparam name="output">
        <TD>
        <dsp:droplet name="BeanProperty">
          <dsp:param name="bean" param="currentItem"/>
          <dsp:param name="propertyName" param="element.property"/>
          <dsp:oparam name="output">
            <dsp:valueof param="propertyValue"/>
          </dsp:oparam>
        </dsp:droplet>
        </TD>
      </dsp:oparam>
    </dsp:droplet>

    </TR>
  </dsp:oparam>

  <%-- Close the table --%>

  <dsp:oparam name="outputEnd">
    </TABLE>
    <p>
    <dsp:getvalueof var="a197" param="tablePage" vartype="java.lang.String">
      <dsp:a href="${a197}">Reset sort properties</dsp:a>
    </dsp:getvalueof>
  </dsp:oparam>
</dsp:droplet>
</BODY>
</HTML>
</dsp:page>
 
loading table of contents...