Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a collapsing tree table in html/css/js?

I have some data to display that is both tabular and hierarchical. I'd like to let the user be able to expand and collapse the nodes.

Sort of like this, except functional:

http://www.maxdesign.com.au/articles/tree-table/

What would be the best way to approach this? I'm not adverse to using an off-the-shelf plugin.

like image 345
justkevin Avatar asked Apr 12 '11 13:04

justkevin


People also ask

How do you make a collapsible list in HTML?

Close the head portion of the page, with the ending tag for the head of the page (</head>). Create the body of the HTML code. Type the tag for starting the body (<body>). Create the list content, including some HTML styling information for the users browser to use, for them to expand and collapse the list.

How do you create a hierarchy in HTML?

You can use unordered lists to create a hierarchy (the <ul> and <li> tags). This can give you a tree structure which is semantically valid, and on top of that you can do whatever you like to express it in interesting ways, either through CSS or Javascript.

How do you collapse an element in HTML?

The . collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.


1 Answers

SlickGrid has this functionality, see the tree demo.

If you want to build your own, here is an example (jsFiddle demo): Build your table with a data-depth attribute to indicate the depth of the item in the tree (the levelX CSS classes are just for styling indentation): 

<table id="mytable">     <tr data-depth="0" class="collapse level0">         <td><span class="toggle collapse"></span>Item 1</td>         <td>123</td>     </tr>     <tr data-depth="1" class="collapse level1">         <td><span class="toggle"></span>Item 2</td>         <td>123</td>     </tr> </table> 

Then when a toggle link is clicked, use Javascript to hide all <tr> elements until a <tr> of equal or less depth is found (excluding those already collapsed):

$(function() {     $('#mytable').on('click', '.toggle', function () {         //Gets all <tr>'s  of greater depth below element in the table         var findChildren = function (tr) {             var depth = tr.data('depth');             return tr.nextUntil($('tr').filter(function () {                 return $(this).data('depth') <= depth;             }));         };          var el = $(this);         var tr = el.closest('tr'); //Get <tr> parent of toggle button         var children = findChildren(tr);          //Remove already collapsed nodes from children so that we don't         //make them visible.          //(Confused? Remove this code and close Item 2, close Item 1          //then open Item 1 again, then you will understand)         var subnodes = children.filter('.expand');         subnodes.each(function () {             var subnode = $(this);             var subnodeChildren = findChildren(subnode);             children = children.not(subnodeChildren);         });          //Change icon and hide/show children         if (tr.hasClass('collapse')) {             tr.removeClass('collapse').addClass('expand');             children.hide();         } else {             tr.removeClass('expand').addClass('collapse');             children.show();         }         return children;     }); }); 
like image 78
bcoughlan Avatar answered Oct 14 '22 01:10

bcoughlan