ADF Faces – Client Side Column Hide and Seek – Expanding/Collapsing Columns in Table

2

Tables are probably the most important ADF Faces component to present data to the end user. A table can present a huge amount of data at once, both in terms of rows and columns. This of course can be overwhelming. Frequently, only some of the columns – some of the fields in the records – are really essential at all times. Other elements – columns – are only required in special situations or in groups at a time. 

When designing the page, there are several approaches to such requirements. One is the Detail Disclosure, where the table columns represent only those key data elements and every row in the table can be disclosed (expanded) to show in line in the table the other data elements. Others include the table overflow right or below – where for the currently selected table row the data elements not shown in the columns are presented in a single record block to the side or bottom of the table. Yet another approach is a popup element that appears for the current table row and presents the additional fields not included in the table as columns. All these approaches share a drive to reduce the complexity of the UI without sacrificing the user’s ability to view all data relevant for a specific record in the table. They also (except for the detail disclosure) have in common that the additional fields are shown for only one record at a time. None of them make it easy or even possible to sort on the fields that are not part of the key set of columns.

In this article, we will discuss yet another way for ADF Tables of making specific information available when the user so desires – this time for all records at once. It is the client side Column Disclosure, a JavaScript based way of expanding and collapsing columns or groups of columns.

For example the following page contains a table that shows Employees and their Allocations on a project. For each employee, we only see a few details (id, first name and last name). There is much more to know about employees – email address, phone number, job title, salary, hiredate – however that information is not frequently required. When it is required, we typically want to compare the values (at least of Job, Hiredate and Salary) of the employees – so we should see them all at once.
 

Press the Expand icon in the Employees header and the Employee Details are expanded – client side, no server roundtrip, really fast (say instantaneously):....

 

In the rest of the article, we will discuss how we can implement this functionality. Another article will discuss a server side solution (using Partial Page Rendering for a semi-instantaneous effect, without the somewhat ugly JavaScript code).

First of all, the purely visual side of things: by using nested af:column elements (as described in the article ADF Faces – Nested Columns), it was simple to create the Employee and Employee Details column clusters. I have added the collapse and expand icons to the Employee Column header:

  &lt;af:column&gt;<br />    &lt;f:facet name=&quot;header&quot;&gt;<br />      &lt;af:panelGroup&gt;<br />        &lt;af:outputText value=&quot;Employee&quot;/&gt;<br />       &lt;af:objectImage id=&quot;expandEmployeeDetails&quot;<br />                        source=&quot;/jheadstart/images/expandCurrent.gif&quot;<br />                        onclick=&quot;collapseOrExpandEmpDetails(this,true);&quot;/&gt;<br />        &lt;af:objectImage id=&quot;collapseEmployeeDetails&quot;<br />                        source=&quot;/jheadstart/images/collapseCurrent.gif&quot;<br />                        onclick=&quot;collapseOrExpandEmpDetails(this,false);&quot;/&gt;<br /><br />      &lt;/af:panelGroup&gt;<br />    &lt;/f:facet&gt;<br />    &lt;!-- DEBUG:BEGIN:TABLE_LOV_ITEM : default/item/table/tableLovItem.vm, nesting level: 6 --&gt;<br />    &lt;af:column id=&quot;AllocationsEmpIdColumn&quot; ...&gt;<br />      &lt;f:facet name=&quot;header&quot;&gt;<br />        &lt;af:outputLabel value=&quot;EmpId&quot;/&gt;<br />      &lt;/f:facet&gt;<br />      ...<br />    &lt;/af:column&gt;<br />    &lt;af:column id=&quot;AllocationsFirstNameColumn&quot; ..&gt;<br />      &lt;f:facet name=&quot;header&quot;&gt;<br />        &lt;af:outputLabel value=&quot;FirstName&quot;/&gt;<br />      &lt;/f:facet&gt;<br />      ...<br />    &lt;/af:column&gt;<br />    &lt;af:column id=&quot;AllocationsLastNameColumn&quot; ... &gt;<br />      &lt;f:facet name=&quot;header&quot;&gt;<br />        &lt;af:outputLabel value=&quot;LastName&quot; .../&gt;<br />      &lt;/f:facet&gt;<br />      ...<br />    &lt;/af:column&gt;<br />    &lt;af:column id=&quot;EmpDetails&quot; ...&gt;<br />      &lt;f:facet name=&quot;header&quot;&gt;<br />        &lt;af:outputLabel value=&quot;Employee Details&quot; .. /&gt;<br />      &lt;/f:facet&gt;<br />      &lt;af:column id=&quot;AllocationsEmailColumn&quot; <br />      ...<br /><br />

 

The Collapse Icon is initially not visible. When the Expand icon is clicked on by the user, the collapseOrExpandEmpDetails() function is invoked, passing the icon itself, along with the value true for the expand parameter. The function is implemented as follows:

  function collapseOrExpandEmpDetails(icon, expand) {<br />    tbl = findParentWithTag(icon, &quot;TABLE&quot;);<br />    trs = tbl.getElementsByTagName(&quot;TBODY&quot;)[0].getElementsByTagName(&quot;TR&quot;);<br />    // in the 1st tr, change COLSPAN of 3rd TH (Employee) from 3 to 9(if expand, we are about to show 6 columns)<br />    // in the 1st tr, change COLSPAN of 3rd TH (Employee) from 9 to 3(if !expand, we are about to hide 6 columns)<br />    trs[0].getElementsByTagName(&quot;TH&quot;)[2].setAttribute(&quot;colspan&quot;,expand?9:3);<br />    // in the 2nd TR, show/hide TH number 3 (Employee Details)<br />    showHideEl(trs[1].getElementsByTagName(&quot;TH&quot;)[3],expand);<br />    // in trs[2], show/hide all th children (the headers of those six columns to be hidden)<br />    tds = trs[2].childNodes;<br />    for( var j = 0; j &amp;lt;  tds.length; j++ ) {<br />      showHideEl(tds[j], expand);<br />    } // for tds in tr<br />    <br />    // from the 3rd TR onwards, loop over the TDs and THs; numbers 6 through 11 should be shown/hidden<br />    for( var i = 3; i &amp;lt;  trs.length; i++ ) {<br />      tds = trs[i].childNodes;<br />      for( var j = 5; j &amp;lt;  11; j++ ) {<br />        showHideEl(tds[j], expand);<br />      } // for tds in tr<br />    }//for trs in table<br />      <br />    // hide icon that was clicked<br />    showHideEl(icon, false); <br />    // show the other icon<br />    if (expand)<br />      showHideEl(document.getElementById(icon.id.replace('expand','collapse')), true);<br />    else<br />      showHideEl(document.getElementById(icon.id.replace('collapse','expand')), true);<br />              <br />  }//collapseOrExpandEmpDetails()<br />&nbsp;

The function has way too much knowledge about the Employee Details column groups to my taste. A more generic implementation would probably only need to know the icon and the TD identifying the column cluster (Employee Details); from its COLSPAN (6 in this case) and its ‘rownumber’ – the third row in this case – it should be able to derive the rest. I will leave that up to the reader…

Another interesting exercise would be to include a second column cluster in the table, for example for allocation details, and make that one collapsible/expandable as well.

It leverages two simple utility functions:

  function findParentWithTag(el, tagName) {<br />    var parent = el.parentNode;< br />    if (parent)<br />      if
 (parent.tagName ==tagName) <br />        return parent;<br />      else<br />        return findParentWithTag(parent, tagName);<br />    else<br />      return null;<br />  }//findParentWithTag<br /><br />  function showHideEl(el, show) {<br />    el.setAttribute('style', show?'':'visibility:collapse;display:none;'); // to cater for both IE and Firefox<br />  }<br /><br />

The function showHideEl is quite crude – it assumes sole possession of the  style attribute, which is quite selfish and not realistic. However, for the purpose of this article, it does the trick.

In order to start the page with the Employee Details collapsed, I added an onLoad trigger to the BODY element:

&lt;afh:body id=&quot;body&quot; onload=&quot;collapseOrExpandEmpDetails(document.getElementById('AllocationsTable:collapseEmployeeDetails'),false);&quot;&gt;<br />

The reference by client id here is a bit of a lazy quick implementation. I am sure you can do better… 

Note: at first I tried manipulating COLGROUP and/or COL elements. However, ADF Faces does not render nested columns (or any table column) using these elements. Dynamically adding COLGROUP elements to the DOM model succeeded; however, manipulation of the style properties of such dynamically added elements was not picked up by the browser (Firefox 2.0), so I gave up on this route.

// some of the experimental COL code<br />var col     = document.createElement('COL');<br />col.setAttribute('width', '210');<br />col.setAttribute('style', 'visibility:collapse;display:none;');<br />tbl.insertBefore(col,tbl.firstChild);<br />col     = document.createElement('COL');<br />col.setAttribute('width', '110');<br />col.setAttribute('style', 'font-weight:bolder;');<br />tbl.insertBefore(col,tbl.firstChild); <br />

The code  I ended up with does the job. However, it can still be made more generic, for example to make it support multiple collapsible/expandable column clusters in a single table.

Note: this implementation has a disagreement with partial page rendering: as soon as the table is resorted or otherwise refreshed using PPR, it is always shown expanded.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

2 Comments

  1. Hi, this is very important to me! i´m dubbie, but , Where i put this function collapseOrExpandEmpDetails ? is javascript ?, is java class ?