Creating expandable tables with jExpand

Creating expandable tables with jExpand

0 56600
Creating expandable tables with jExpand
Creating expandable tables with jExpand

Creating expandable tables with jExpand

Haven’t you thought about making your tables expandable? Recently I stumbled to a very interesting and incredibly useful (in my opinion) plugin – jExpand, the plugin perfectly reduces the amount of information on the visitor’s page. jExpand is truly ultra lightweight (only 11 lines of code) jQuery plugin that will make your tables expandable. Typically for business applications, this feature can help you organize your tables better. Therefore, tables can contain more information such as videos, images, diagrams, lists and other elements.

The result of our lesson is as follows:

Creating expandable tables with jExpand

Live Demo

Step 1 – HTML

The main idea is to reserve two table rows for every entity: the first row contains some basic information, the second row contains additional details. Details row is toggled by clicking on the main (parent) row. Your table can contain as many columns as you need. There is only one important thing – that the additional details row has to span (using colspan attribute) all the columns that main row contains (in case if you need to have full details row area). In the example below, our table contains 5 columns and additional details rows spans exactly 5 columns.

<table id="example_table">
    <tr>
        <th>Column header 1</th>
        <th>Column header 2</th>
        <th>Column header 3</th>
        <th>Column header 4</th>
        <th>Column header 5</th>
    </tr>
    <tr>
        <td>Record 1</td>
        <td>value 1</td>
        <td>value 2</td>
        <td>value 3</td>
        <td>value 4</td>
    </tr>
    <tr>
        <td colspan="5">
            <!-- additional custom info is here (for Record 1) -->
        </td>
    </tr>
    <tr>
        <td>Record 2</td>
        <td>value 5</td>
        <td>value 6</td>
        <td>value 7</td>
        <td>value 8</td>
    </tr>
    <tr>
        <td colspan="5">
            <!-- additional custom info is here (for Record 2) -->
        </td>
    </tr>
    <tr>
        <td>Record 3</td>
        <td>value 9</td>
        <td>value 10</td>
        <td>value 11</td>
        <td>value 12</td>
    </tr>
    <tr>
        <td colspan="5">
            <!-- additional custom info is here (for Record 3) -->
        </td>
    </tr>
</table>

Step 2 – Javascript

In order to turn the table into the expandable table, we have to connect the jExpand plugin:

<script type="text/javascript" src="jExpand.js"></script>

And then – initialize the plugin:

 $('#example_table').jExpand();

If you have a look into the plugin sources, you will find that it’s code is rather simple:

(function($){
    $.fn.jExpand = function(){
        var element = this;
        $(element).find("tr:odd").addClass("odd");
        $(element).find("tr:not(.odd)").hide();
        $(element).find("tr:first-child").show();
        $(element).find("tr.odd").click(function() {
            $(this).next("tr").toggle();
        });
    }
})(jQuery);

The logic of work is very simple: Firstly, it adds .odd class to every odd row in the table (for simplier table styling), then it hides all other (not .odd) rows with our additional content. Then it shows the first table row. Finally, it adds onclick event handler to all .odd rows (with basic info). By clicking on the odd row, it toggles the visibility of related details row (even rows). Short and clear.


Live Demo

[sociallocker]

download in package

[/sociallocker]

SIMILAR ARTICLES


NO COMMENTS

Leave a Reply