MOBILE

jQuery 1.3 : Modifying table appearance (part 3) - Collapsing and expanding sections

2/24/2011 3:51:04 PM

Collapsing and expanding sections

When a large set of data is divided into sections, it can be useful to hide information that we aren't interested in at the moment. In our table of news articles, rows are grouped by year; collapsing, or hiding, a year of articles can be a convenient way to get a broad view of all of the table's data without having to scroll so much.

To make the sections of the news article table collapsible, we first need to create a page element that will be used to trigger the behavior. One standard interface for collapsible items is a minus sign, with a corresponding plus sign for expandable items. We'll insert the icon with JavaScript, following standard progressive enhancement techniques.

$(document).ready(function() {
var collapseIcon = '../images/bullet_toggle_minus.png';
var collapseText = 'Collapse this section';
var expandIcon = '../images/bullet_toggle_plus.png';
var expandText = 'Expand this section';
$('table.collapsible tbody').each(function() {
var $section = $(this);
$('<img />').attr('src', collapseIcon)
.attr('alt', collapseText)
.prependTo($section.find('th'));
});
});

We have stored the locations of the icons, as well as their alternate textual representations, in variables at the beginning of the function. This allows us to refer to them easily, and provides a simple way to make changes if necessary. We've done the image injection in an .each() loop, which will prove convenient later as we will need to refer to the enclosing<tbody> element again; it will be available to us through the $section variable we've defined here.

Next, we'll need to make the icons trigger the collapsing and expanding of rows. The addition of a clickable class provides the necessary user feedback, and a class on the<tbody> element helps us keep track of whether the rows are currently visible or not.

$(document).ready(function() {
var collapseIcon = '../images/bullet_toggle_minus.png';
var collapseText = 'Collapse this section';
var expandIcon = '../images/bullet_toggle_plus.png';
var expandText = 'Expand this section';
$('table.collapsible tbody').each(function() {
var $section = $(this);
$('<img />').attr('src', collapseIcon)
.attr('alt', collapseText)
.prependTo($section.find('th'))
.addClass('clickable')
.click(function() {
if ($section.is('.collapsed')) {
$section.removeClass('collapsed')
.find('tr:not(:has(th))').fadeIn('fast');
$(this).attr('src', collapseIcon)
.attr('alt', collapseText);
}
else {
$section.addClass('collapsed')
.find('tr:not(:has(th))').fadeOut('fast');
$(this).attr('src', expandIcon)
.attr('alt', expandText);
}
});
});
});


When a click occurs, we do the following:

  1. 1. Add or remove the collapsed class on the<tbody> element, to keep track of the current state of the table section.

  2. 2. Locate all rows in the section that do not contain headings, and show or hide them using a fading transition.

  3. 3. Toggle the current state of the icon, changing its src and alt attributes to reflect the action it will now trigger when clicked.

With this code in place, clicking on the Collapse this section icon next to 2007 makes the table look like this:

The 2007 news articles aren't removed; they are just hidden until we click the Expand this section icon that now appears in that row.

Table rows present particular obstacles to animation, since browsers use different values (table-row and block) for their visible display property. The .hide() and methods, without animation, are always safe to use with table rows. If animation is desired, .fadeIn() and .fadeOut() can be used as well. .show()

Other  
  •  Windows Phone 7 Development : Using Culture Settings with ToString to Display Dates, Times, and Text
  •  Mobile Application Security : SymbianOS Security - Persistent Data Storage
  •  Mobile Application Security : SymbianOS Security - Interprocess Communication
  •  Mobile Application Security : SymbianOS Security - Permissions and User Controls
  •  Windows Phone 7 Development : Building a Trial Application (part 3) - Verifying Trial and Full Mode & Adding Finishing Touches
  •  Windows Phone 7 Development : Building a Trial Application (part 2) - Connecting to a Web Service & Adding Page-to-Page Navigation
  •  Windows Phone 7 Development : Building a Trial Application (part 1) - Building the User Interface
  •  jQuery 1.3 : Table Manipulation - Sorting and paging (part 2) : Server-side pagination & JavaScript pagination
  •  jQuery 1.3 : Table Manipulation - Sorting and paging (part 1) : Server-side sorting & JavaScript sorting
  •  Windows Phone 7 Development : Understanding Trial and Full Modes (part 3) - Simulating Application Trial and Full Modes
  •  Windows Phone 7 Development : Understanding Trial and Full Modes (part 2) - Using the Marketplace APIs
  •  Windows Phone 7 Development : Understanding Trial and Full Modes (part 1) - Using the IsTrial Method
  •  Mobile Application Security : SymbianOS Security - Application Packaging
  •  Mobile Application Security : SymbianOS Security - Code Security
  •  iPhone Application Development : Getting the User’s Attention - Generating Alerts
  •  iPhone Application Development : Getting the User’s Attention - Exploring User Alert Methods
  •  iPhone Application Development : Using Advanced Interface Objects and Views - Using Scrolling Views
  •  Working with the Windows Phone 7 Application Life Cycle (part 2) - Managing Application State
  •  Working with the Windows Phone 7 Application Life Cycle (part 1) - Observing Application Life Cycle Events
  •  Mobile Application Security : SymbianOS Security - Development and Security Testing
  •  
    Most View
    Information Theory
    Apple Store Insider Guide (Part 3)
    Microsoft SQL Server 2008 R2 : Hierarchyid Data Type (part 2) - Modifying the Hierarchy
    SQL Server 2008: Monitoring Resource Governor
    Motorola Xoom 2 - General Tablet Use
    Visual Studio 2010 : Introducing the Visual Studio Extensibility - Building a Visual Studio Package
    Windows Server 2008 : The Discovery Phase - Understanding the Existing Environment
    SQL Server 2008 Command-Line Utilities : The tablediff Command-Line Utility
    Web Security : Attacking AJAX - Intercepting and Modifying Server Responses, Subverting AJAX with Injected Data
    Sharepoint 2007: Upload a File - Upload a File from the Web Interface
    Top 10
    ASP.NET 4 in VB 2010 : The Data Controls - Sorting and Paging the GridView
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 2)
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 1)
    Microsoft Content Management Server Development : Building SharePoint Web Parts - Configuring the Web Part, Debugging the Web Part
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 4) - Monitoring and troubleshooting DNS
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 3) - Setting up DNS zones
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 2) - Installing the DNS Server role, Configuring DNS Servers
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 1) - Designing a DNS infrastructure
    Windows Server 2008 R2 networking : Routing and Remote Access
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets