HTML Treetable
HTML Treetable javascript allows creating a components capable of expanding and contracting rows as well as showing multiple columns of data. It can be useful in presenting hierarchical tabular data.
Let’s look at some examples first:
Example 2 – icon that shows expanded / collaped row state is added, as well as some style to table.
How HTML Treetable works
Every table row has an id attribute set and the id is assebled of table id, eventual parent rows indexes and current row index in parent row delimitated with underscore (). If we give id to the table in examples table1, the first row would have id of table1_0, it’s child record would have id of table1_0_0, second row table1_0_1 and so on. Anchor that would expande or collapse specific row would call javascript function treetable_toggleRow(row_id);, where row_id is id of specific row.
Functions treetable_collapseAll(tableId) and treetable_expandAll(tableId) allows all rows in table to be expanded or collapsed.
Callback javascript function that would be called every time row is collapsed or expanded can be defined by setting value of treetable_callbacks['eventRowStateChanged'] to callback function name.
Additional info
HTML table is meant to be created from server side script. If there is interest for a PHP script that would create HTML code for the table from an array please drop a comment.
Script has been tested on WIN XP with Firefox 1.06, IE6 and Opera 8.
I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in
42 comments
function togglecol(colIndex,colIndexEnd) { for (colIndex; colIndex <= colIndexEnd; colIndex++) { var table = document.all ? document.all.table1 : document.getElementById('table1'); for (var r = 0; r < table.rows.length; r++){ if(table.rows[r].cells[colIndex].style.display == \"none\") { table.rows[r].cells[colIndex].style.display = \'\'; //treetable_fireEventColStateChanged(colId, \'1\'); } else { table.rows[r].cells[colIndex].style.display = \'none\'; // treetable_fireEventColStateChanged(colId, \'0\'); } } } }to call the function use starting and ending column to hideI am looking for a script that only shows one row at a time and if you klik on the link it will close the previous row and show only the selected one. Can you help me?
On all scripts you have to close the previous selected row but I would like it to happen automatically so you will only see one row at a time
regards dennis
First of all thanks for your script. I found it very useful. Do you have some news about the ‘default visibility state’ of rows the first time they are displayed? I would appreciate your help. Thanks again!!!
Hello
Very interesting information! Thanks!
G’night
Great Stuff Bojan, Can we get 4child nodes!! Present stuff showings only one level its possible to do it for 4 levels???
Thanks, Ven
Venkat, sure, you can add as many as you need.
I’m quite interesting in a PHP script as well, if you can send me i’ll be so thanksfull..
Thanks, even for the example
Speak your mind: