1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery treeTable Plugin Documentation</title>
<link href="stylesheets/master.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascripts/jquery.js"></script>
<script type="text/javascript" src="javascripts/jquery.ui.js"></script>
<!-- BEGIN Plugin Code -->
<link href="../src/stylesheets/jquery.treeTable.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../src/javascripts/jquery.treeTable.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// TODO Fix issue with multiple treeTables on one page, each with different options
// Moving the #example3 treeeTable call down will break other treeTables that are expandable...
$("#example3").treeTable({
expandable: false
});
$(".example").treeTable();
// Drag & Drop Example Code
$("#dnd-example .file, #dnd-example .folder").draggable({
helper: "clone",
opacity: .75,
refreshPositions: true, // Performance?
revert: "invalid",
revertDuration: 300,
scroll: true
});
$("#dnd-example .folder").each(function() {
$($(this).parents("tr")[0]).droppable({
accept: ".file, .folder",
drop: function(e, ui) {
$($(ui.draggable).parents("tr")[0]).appendBranchTo(this);
},
hoverClass: "accept",
over: function(e, ui) {
if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
$(this).expand();
}
}
});
});
// Make visible that a row is clicked
$("table#dnd-example tbody tr").mousedown(function() {
$("tr.selected").removeClass("selected"); // Deselect currently selected rows
$(this).addClass("selected");
});
// Make sure row is selected when span is clicked
$("table#dnd-example tbody tr span").mousedown(function() {
$($(this).parents("tr")[0]).trigger("mousedown");
});
});
</script>
<!-- END Plugin Code -->
</head>
<body>
<h1>jQuery treeTable Plugin Documentation</h1>
<table class="example">
<caption>Simple treeTable Example</caption>
<thead>
<tr>
<th>TreeColumn</th>
<th>Additional data</th>
</tr>
</thead>
<tbody>
<tr id="ex0-node-1">
<td>Node 1: Click on the icon in front of me to expand this branch.</td>
<td>I live in the second column.</td>
</tr>
<tr id="ex0-node-1-1" class="child-of-ex0-node-1">
<td>Node 1.1: Look, I am a table row <em>and</em> I am part of a tree!</td>
<td>Interesting.</td>
</tr>
<tr id="ex0-node-1-1-1" class="child-of-ex0-node-1-1">
<td>Node 1.1.1: I am part of the tree too!</td>
<td>That's it!</td>
</tr>
</tbody>
</table>
<p>See the <a href="#examples">Examples</a> chapter for more examples. To download releases of this plugin, visit <a href="http://plugins.jquery.com/project/treeTable">treeTable's project page</a>. The source code of this plugin and this documentation is available on GitHub: <a href="http://github.com/ludo/jquery-plugins/tree/master/treeTable">http://github.com/ludo/jquery-plugins/tree/master/treeTable</a>.</p>
<h2>Table of Contents</h2>
<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#installation">Installation</a></li>
<li><a href="#usage">Usage</a>
<ol>
<li><a href="#head">Include the plugin in your html document</a></li>
<li><a href="#tree">Representing your tree in a table</a></li>
<li><a href="#settings">Configuration</a></li>
</ol>
</li>
<li><a href="#examples">Examples</a>
<ol>
<li><a href="#example-2">A complex tree</a></li>
<li><a href="#example-3">A tree that is not collapsable</a></li>
<li><a href="#example-1">Dragging and dropping example</a></li>
<li><a href="performance.html">A large, complex table with drag and drop</a></li>
</ol>
</li>
</ol>
<h2>1. <a name="introduction">Introduction</a></h2>
<p>treeTable is a plugin for <a href="http://jquery.com">jQuery</a>, the 'Write Less, Do More, JavaScript Library'. With this plugin you can display a tree in a table, i.e. a directory structure or a nested list. Why not use a list, you say? Because lists are great for displaying a tree, and tables are not. Oh wait, but this plugin uses tables, doesn't it? Yes. Why do I use a table to display a list? Because I need multiple columns to display additional data besides the tree.</p>
<p>This plugin is released under the MIT license by <a href="http://blog.cubicphuse.nl">Ludo van den Boom</a>.</p>
<h4>Unobtrusiveness</h4>
<p>I wanted this plugin to be as unobtrusive as possible. Being 'unobtrusive' is very cool nowadays, so that was an important requirement. But it is cool for a reason: it keeps your html documents clean and it allows my code to degrade nicely when JavaScript is not available.</p>
<p>Unfortunately, the treeTable plugin requires that you add <tt>class</tt> and <tt>id</tt> attributes to every row that is part of the tree. It would have been great if this weren't necessary, because it doesn't look pretty in your html, but the plugin needs to know what your tree looks like. Otherwise, it would have to guess the structure of the tree and it wouldn't be very successful in doing that. See the <a href="#usage">Usage</a> chapter for more information on how to describe a tree.</p>
<h4>Features</h4>
<ul>
<li>It can display a tree of data in a table column.</li>
<li>It does this as unobtrusively as possible.</li>
<li>It allows branches to be collapsed and expanded (think of how a directory structure works in most file explorers).</li>
<li>It allows unlimited tree depth.</li>
<li>It uses the lightweight <a href="http://jquery.com">jQuery</a> JavaScript libray.</li>
</ul>
<p>NOTE: This plugin was originally released under the name ActsAsTreeTable, but has been renamed to treeTable with version 2.0.</p>
<h2>2. <a name="installation">Installation</a></h2>
<p>Installing this plugin is straight-forward. You will have to copy several files and, if necessary, adjust some paths so that every file is available to the plugin.</p>
<ol>
<li>Add <a href="http://jquery.com">jQuery</a> to your project. See their website for instructions on doing this. You need at least version 1.2.6.</li>
<li>Add <a href="../src/javascripts/jquery.treeTable.js"><tt>src/javascripts/jquery.treeTable.js</tt></a> (or the <a href="../src/javascripts/jquery.treeTable.min.js">minified</a> version) to your project.</li>
<li>Add the stylesheet <a href="../src/stylesheets/jquery.treeTable.css"><tt>src/stylesheets/jquery.treeTable.css</tt></a> to your project.</li>
<li>Copy the images in <a href="../src/images"><tt>src/images</tt></a> to your project.</li>
<li>Adjust the paths to <tt>background-image</tt>s in the stylesheet <tt>jquery.treeTable.css</tt> to point to the image files that you have just copied.</li>
</ol>
<p>That's it! You are now ready to start <a href="#usage">using</a> the plugin in your project.</p>
<h2>3. <a name="usage">Usage</a></h2>
<p><b>Note:</b> This chapter assumes that you have already installed jQuery as described on their website.</p>
<h3>3.1 <a name="head">Include the plugin in your html document</a></h3>
<p>Paste the following code between the <tt>head</tt> tags in your html document, <b>underneath</b> the part where you include jQuery. Change the <b style="color: #f00;">red</b> parts to reflect your situation.</p>
<pre class="listing">
<link href="<b>path/to/</b>jquery.treeTable.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<b>path/to/</b>jquery.treeTable.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("<b>#your_table_id</b>").treeTable();
});
</script>
</pre>
<h3>3.2 <a name="tree">Representing your tree in a table</a></h3>
<p>When you pasted the above code and adjusted it to reflect your situation, you enabled the <em>possibility</em> of displaying a tree in your table. To make the tree actually display as a tree you have to add <tt>id</tt> and <tt>class</tt> attributes to your table rows (<tt>tr</tt>).</p>
<h4>How to do this?</h4>
<p>First, you should add a <em>unique</em> <tt>id</tt> to each of the rows in your table, for example '<tt>node-<em>x</em></tt>' where <em>x</em> is a number. Then you add a <tt>class</tt> attribute to each child of a node, give this class a name of '<tt>child-of-<em>node-x</em></tt>'. The <em>node-x</em> part should be the same as the <tt>id</tt> of its parent. Do you still follow me? Let me show you an example of a very simple tree: a single parent with a single child. For more examples you should view the source code of this page, where you find several tables for the examples in the <a href="#examples">Examples</a> chapter.</p>
<pre class="listing">
<table id="tree">
<tr id="node-1">
<td>Parent</td>
</tr>
<tr id="node-2" class="child-of-node-1">
<td>Child</td>
</tr>
<tr id="node-3" class="child-of-node-2">
<td>Child</td>
</tr>
</table>
</pre>
<p>Please note that the plugin expects the rows in the HTML table to be in the same order in which they should be displayed in the tree. For example, suppose you have three nodes: <strong style="color: red;">A</strong>, <strong style="color: navy;">B</strong> (child of node <span style="color: red">A</span>) and <strong style="color: green;">C</strong> (child of node <span style="color: navy">B</span>). If you create rows for these nodes in your HTML table in the following order <span style="color: red">A</span> - <span style="color: green">C</span> - <span style="color: navy">B</span>, then the tree will not display correctly. You have to make sure that the rows are in the order <span style="color: red">A</span> - <span style="color: navy">B</span> - <span style="color: green">C</span>.</p>
<h3>3.3 <a name="settings">Configuration</a></h3>
<p>There are several settings that let you adjust the behavior of the plugin. Each of these settings is described in this section. See <a href="#example-3">Example 3</a> for an example of how to change these settings.</p>
<table>
<thead>
<tr>
<th>Setting</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><tt>childPrefix</tt></td>
<td><tt>string</tt></td>
<td><tt>"child-of-"</tt></td>
<td>Customize the prefix used for node classes.</td>
</tr>
<tr>
<td><tt>clickableNodeNames</tt></td>
<td><tt>bool</tt></td>
<td><tt>false</tt></td>
<td>Set to true to expand branches not only when expander icon is clicked but also when node name is clicked.</td>
</tr>
<tr>
<td><tt>expandable</tt></td>
<td><tt>bool</tt></td>
<td><tt>true</tt></td>
<td>Should the tree be expandable? An expandable tree contains buttons to make each branch with children collapsable/expandable.</td>
</tr>
<tr>
<td><tt>indent</tt></td>
<td><tt>int</tt></td>
<td><tt>19</tt></td>
<td>The number of pixels that each branch should be indented with.</td>
</tr>
<tr>
<td><tt>initialState</tt></td>
<td><tt>string</tt></td>
<td><tt>"expanded"</tt></td>
<td>Possible values: <tt>"expanded"</tt> or <tt>"collapsed"</tt>.</td>
</tr>
<tr>
<td><tt>treeColumn</tt></td>
<td><tt>int</tt></td>
<td><tt>0</tt></td>
<td>The number of the column in the table that should be displayed as a tree.</td>
</tr>
</tbody>
</table>
<h2>4. <a name="examples">Examples</a></h2>
<p>The examples in this chapter all use the treeTable plugin to display a tree in a table, with collapsable branches. View the source code of this file to see how it is done and read the <a href="#usage">Usage</a> chapter for further details.</p>
<h3>4.1 <a name="example-2">A complex tree</a></h3>
<table class="example">
<caption>Example 1: A complex tree.</caption>
<tr id="ex2-node-1">
<td>Node 1</td>
</tr>
<tr id="ex2-node-1-1" class="child-of-ex2-node-1">
<td>Node 1.1</td>
</tr>
<tr id="ex2-node-1-2" class="child-of-ex2-node-1">
<td>Node 1.2</td>
</tr>
<tr id="ex2-node-1-3" class="child-of-ex2-node-1">
<td>Node 1.3</td>
</tr>
<tr id="ex2-node-2">
<td>Node 2</td>
</tr>
<tr id="ex2-node-2-1" class="child-of-ex2-node-2">
<td>Node 2.1</td>
</tr>
<tr id="ex2-node-2-1-1" class="child-of-ex2-node-2-1">
<td>Node 2.1.1</td>
</tr>
<tr id="ex2-node-2-2" class="child-of-ex2-node-2">
<td>Node 2.2</td>
</tr>
<tr id="ex2-node-2-2-1" class="child-of-ex2-node-2-2">
<td>Node 2.2.1</td>
</tr>
<tr id="ex2-node-2-2-1-1" class="child-of-ex2-node-2-2-1">
<td>Node 2.2.1.1</td>
</tr>
<tr id="ex2-node-2-2-2" class="child-of-ex2-node-2-2">
<td>Node 2.2.2</td>
</tr>
</table>
<h3>4.2 <a name="example-3">A tree that is not collapsable</a></h3>
<pre class="listing">
$("#example3").treeTable({
expandable: false
});
</pre>
<table id="example3">
<caption>Example 2: A tree that is not collapsable.</caption>
<thead>
<tr>
<th>Tree column</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr id="ex3-node-1">
<td>Node 1</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-1-1" class="child-of-ex3-node-1">
<td>Node 1.1</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-1-2" class="child-of-ex3-node-1">
<td>Node 1.2</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-1-3" class="child-of-ex3-node-1">
<td>Node 1.3</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2">
<td>Node 2</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2-1" class="child-of-ex3-node-2">
<td>Node 2.1</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2-1-1" class="child-of-ex3-node-2-1">
<td>Node 2.1.1</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2-2" class="child-of-ex3-node-2">
<td>Node 2.2</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2-2-1" class="child-of-ex3-node-2-2">
<td>Node 2.2.1</td>
<td>Second column</td>
</tr>
<tr id="ex3-node-2-2-2" class="child-of-ex3-node-2-2">
<td>Node 2.2.2</td>
<td>Second column</td>
</tr>
</tbody>
</table>
<h3>4.3 <a name="example-1">Draging and dropping example</a></h3>
<p>This example uses the <a href="http://ui.jquery.com" title="jQuery UI Website">jQuery UI</a> components Draggable and Droppable to create a tree that can be manipulated by dragging and dropping of the nodes. You can drag a node by clicking on it's title and drag it to a different position. This behavior requires a bit more coding than the simple trees above, but it is still pretty straight-forward. The code for this example is listed below. The most interesting line in this code, from the plugin's point of view is <tt>$($(ui.draggable).parents("tr")).appendBranchTo(this);</tt>. Here the <tt>appendBranchTo</tt> function is called to move the selected branch to a new location.</p>
<table class="example" id="dnd-example">
<caption>Example 3: Dragging and dropping example.</caption>
<thead>
<tr>
<th>Title</th>
<th>Size</th>
<th>Kind</th>
</tr>
</thead>
<tbody>
<tr id="node-20">
<td><span class="file">CHANGELOG</span></td>
<td>4 KB</td>
<td>Plain text</td>
</tr>
<tr id="node-1">
<td><span class="folder">doc</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-2" class="child-of-node-1">
<td><span class="folder">images</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-8" class="child-of-node-2">
<td><span class="file">bg-table-thead.png</span></td>
<td>52 KB</td>
<td>Portable Network Graphics image</td>
</tr>
<tr id="node-9" class="child-of-node-2">
<td><span class="file">folder.png</span></td>
<td>4 KB</td>
<td>Portable Network Graphics image</td>
</tr>
<tr id="node-10" class="child-of-node-2">
<td><span class="file">page_white_text.png</span></td>
<td>4 KB</td>
<td>Portable Network Graphics image</td>
</tr>
<tr id="node-3" class="child-of-node-1">
<td><span class="file">index.html</span></td>
<td>4 KB</td>
<td>HTML document</td>
</tr>
<tr id="node-4" class="child-of-node-1">
<td><span class="folder">javascripts</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-5" class="child-of-node-4">
<td><span class="file">jquery.js</span></td>
<td>56 KB</td>
<td>JavaScript source</td>
</tr>
<tr id="node-6" class="child-of-node-1">
<td><span class="folder">stylesheets</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-7" class="child-of-node-6">
<td><span class="file">master.css</span></td>
<td>4 KB</td>
<td>CSS style sheet</td>
</tr>
<tr id="node-14">
<td><span class="file">MIT-LICENSE</span></td>
<td>4 KB</td>
<td>Plain text</td>
</tr>
<tr id="node-18">
<td><span class="file">README.markdown</span></td>
<td>4 KB</td>
<td>Markdown document</td>
</tr>
<tr id="node-11">
<td><span class="folder">src</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-12" class="child-of-node-11">
<td><span class="folder">images</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-15" class="child-of-node-12">
<td><span class="file">bullet_toggle_minus.png</span></td>
<td>4 KB</td>
<td>Portable Network Graphics image</td>
</tr>
<tr id="node-16" class="child-of-node-12">
<td><span class="file">bullet_toggle_plus.png</span></td>
<td>4 KB</td>
<td>Portable Network Graphics image</td>
</tr>
<tr id="node-13" class="child-of-node-11">
<td><span class="folder">stylesheets</span></td>
<td>--</td>
<td>Folder</td>
</tr>
<tr id="node-17" class="child-of-node-13">
<td><span class="file">jquery.treeTable.css</span></td>
<td>4 KB</td>
<td>CSS style sheet</td>
</tr>
<tr id="node-19" class="child-of-node-11">
<td><span class="file">jquery.treeTable.js</span></td>
<td>8 KB</td>
<td>JavaScript source</td>
</tr>
</tbody>
</table>
<pre class="listing">
/* NOTE: Do not forget to download the jQuery UI Draggable and Droppable
* components if you want to enable dragging and dropping behavior!
*/
// Configure draggable nodes
$("#dnd-example .file, #dnd-example .folder").draggable({
helper: "clone",
opacity: .75,
refreshPositions: true, // Performance?
revert: "invalid",
revertDuration: 300,
scroll: true
});
// Configure droppable rows
$("#dnd-example .folder").each(function() {
$(this).parents("tr").droppable({
accept: ".file, .folder",
drop: function(e, ui) {
// Call jQuery treeTable plugin to move the branch
$($(ui.draggable).parents("tr")).appendBranchTo(this);
},
hoverClass: "accept",
over: function(e, ui) {
// Make the droppable branch expand when a draggable node is moved over it.
if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
$(this).expand();
}
}
});
});
// Make visible that a row is clicked
$("table#dnd-example tbody tr").mousedown(function() {
$("tr.selected").removeClass("selected"); // Deselect currently selected rows
$(this).addClass("selected");
});
// Make sure row is selected when span is clicked
$("table#dnd-example tbody tr span").mousedown(function() {
$($(this).parents("tr")[0]).trigger("mousedown");
});
</pre>
</body>
</html>
|