Enlarging your YUI DataTable in 29 Seconds or Less!

This is an updated version of the Enlarging your YUI DataTable in 30 Seconds or Less! modified for the newly released YUI 2.3.0.

This method adds an extra row to the YUI DataTable when a row is selected, below the selected row. This allows the developer to add additional content that might not be applicable to the column constraints of a typical datatable or grid and allow that content be displayed more fluidly inside a single row spanning all of the viewable columns.

I know you’re anxious for an example, so let’s see some screenshots of a simple YUI DataTable:

Default DataTable

Turns into this when a row is selected:

Row selected

Click any of the above images for a live example.

Any HTML can be added. You can make an AJAX call and put the result into the newly inserted row (that will be left as an exercise for the reader [you]).

I know you’re asking yourself, how the hell do I add this to my YUI DataTable? WHERE IS THE DAMN SOURCE CODE? Calm down, you know I’m getting to it.

How To

1. Include the ymod-tableExtension-2.3.0.js file.
2. Create your DataTable. If you don’t know how to do this, go to the official documentation for help and examples.
3. Make sure your DataTable has the selectionMode parameter set to ‘single’. This can be achieved by passing in { selectionMode: 'single' } in as the 4th argument to the DataTable constructor.
4. Use the following code to setup your table extension:

// myDataTable is your DataTable object
YAHOO.ymod.tableExtension.setup( myDataTable, function( contentDiv )
{
    var myContent = '';
    var selectedRows = this.getSelectedRows();
    if( selectedRows.length > 0 )
    {
        myContent += 'Do something based on<br />the row that is selected!';
    }
    contentDiv.innerHTML = myContent;
} );

The setup function is basically a convenience method to add the event listeners. You can just as easily do this yourself manually:

// myDataTable is your DataTable object.
myDataTable.subscribe( 'headerRowMousedownEvent', YAHOO.ymod.tableExtension.cleanUp );
myDataTable.subscribe( 'rowClickEvent', YAHOO.ymod.tableExtension.selectRow, function( contentDiv ) { /* same as function appears above */ } );

CSS

Here’s some CSS hooks to do some styling. The expanded row will include the yui-dt-selected class by default.

/* The original table row clicked on */
tr.ymod-expanded {}
/* Row containing the expanded content */
tr.ymod-expandedData {}
/* Div containing the expanded content inside the row */
tr.ymod-expandedData div.ymod-expandedDataContent { background-color: navy; padding: 2px 6px; }

Limitations

– Resorting removes the expanded content. Otherwise it was messing with the sort.
– It only works with single row selection mode, which allows only one row to be selected at a time. This is not the default (standard), which allows multiple rows to be selected with SHIFT or CTRL. Feel free to modify this to work with other modes!

Download ymod-tableExtension-2.3.0.js

This entry was posted in JavaScript and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.
  • If you found this article useful, you should subscribe to my feed (or get an e-mail). I'm also on Twitter and GitHub.
  • About the Author

    Zach Leatherman is a Professional Front End Engineer. He loves building for the web, and has been contributing to the community through his blog since February 2007. Despite his propensity for software, he has a Bachelors degree in Computer Engineering and is currently on the User Experience Team at Union Pacific Railroad. The views expressed on this website do not represent the views of his employer.

    He enjoys spending time with his beautiful wife Traci and their two Great Danes, Roxie and Ella. They also have a cat, a rabbit, goldfish, and one or more tarantulas. Read more »

3 Comments

  1. umesh
    Posted October 24, 2007 at 4:43 am | Permalink

    Hi

    WIth regards to YUI Data table expand, i am using your ymod-tableextension

    I hv a query , instead of displaying ” Do something based onthe row that is selected” i want to display one of the fields of the same record .

    How can i do it,

    Thanks in advance

  2. Posted November 14, 2007 at 1:43 pm | Permalink

    Nice tool… but I wanted to use pagination and that just kills it. Tried adding event listeners to close the expansion before changing the page, to no avail. The next page shows a blank line and when you click the blank line it disappears, and then the clicking gets funky for the rest until you just refresh. So I imagine I might still have the problem while using an ajax based pagination as well.

  3. Miguel Angel
    Posted November 6, 2008 at 4:57 am | Permalink

    Hello, great job, but I have a question, I’m trying to add another yui table when the row expands, but I can’t, somebody knows how make this possible?
    thanks

3 Trackbacks

  1. [...] Please not that this post has been updated to the new version of YUI, 2.3.0 in an article called “Enlarging your YUI DataTable in 29 Seconds or Less!” [...]

  2. [...] Leatherman had the initial idea for the dataTable row expansion. var dzone_url = ‘http://weblog.dangertree.net/2008/10/31/grailsui-10-released/'; var dzone_title [...]

  3. [...] zachleat.com » Enlarging your YUI DataTable in 29 Seconds or Less! – This method adds an extra row to the YUI DataTable when a row is selected, below the selected row. This allows the developer to add additional content that might not be applicable to the column constraints of a typical datatable or grid and allow that content be displayed more fluidly inside a single row spanning all of the viewable columns. [...]