Skip to content

Patch for lazy node creation #1

@captncarrot

Description

@captncarrot

I need to create additional elements on the fly. So I patched treeTable a bit. Not very elegant since I am quite new to jquery ;-)

+
+  fillRow = function(tr, klass, name, content)
+  {
+    var span = document.createElement('span');
+    span.className = klass;
+    span.innerHTML = name;
+
+    var td = document.createElement('td');
+    td.appendChild(span);
+    tr.appendChild(td);
+
+    td = document.createElement('td');
+    td.innerHTML = content;
+    tr.appendChild(td);
+
+    return tr;
+  }
+
+ 
+  $.fn.treeTable.createLazyParent = function(id, klass, name, content, parentid)
+  {
+    var tr = document.createElement('tr');
+    tr.id = id;
+    if (parentid)
+      tr.className = "lazyfill parent node child-of-" + parentid;
+    else    
+      tr.className = "lazyfill parent node";

+    return fillRow(tr, klass, name, content);
+  }
+
+  $.fn.treeTable.createChild = function(id, klass, name, content, parentid, oddrow)
+  {
+    tr = document.createElement('tr');
+    tr.id = id;
+    if(parentid)
+      parentid = "child-of-" + parentid;
+
+    if(oddrow)
+      tr.className = "odd " + parentid;
+    else
+      tr.className = "even " + parentid;
+
+    return fillRow(tr, klass, name, content);
+  }
+
   $.fn.treeTable.defaults = {
     childPrefix: "child-of-",
     clickableNodeNames: false,
     expandable: true,
     indent: 19,
     initialState: "collapsed",
-    treeColumn: 0
+    treeColumn: 0,
+    lazyfiller:null
   };
-  
+
   // Recursively hide all node's children in a tree
   $.fn.collapse = function() {
     $(this).addClass("collapsed");
-    
+
     childrenOf($(this)).each(function() {
       if(!$(this).hasClass("collapsed")) {
         $(this).collapse();
       }
-      
+
       this.style.display = "none"; // Performance! $(this).hide() is slow...
     });
-    
+
     return this;
   };
-  
+
   // Recursively show all node's children in a tree
   $.fn.expand = function() {
     $(this).removeClass("collapsed").addClass("expanded");
-    
+
+    if (options.lazyfiller && $(this).hasClass("lazyfill"))
+    {
+      $(this).removeClass("lazyfill");
+      options.lazyfiller(this)
+    }
+
     childrenOf($(this)).each(function() {
       initialize($(this));
-      
+
       if($(this).is(".expanded.parent")) {
         $(this).expand();
       }

Usage is like this when adapting index.html from the sources:

    var nodectr = 1;

    lazyfillerdemo = function(parent)
    {
       tr1 = $.fn.treeTable.createLazyParent ("lazynode-" + nodectr, "folder", "node-" + nodectr, "asdfasdf n" + nodectr);
       tr2 = $.fn.treeTable.createChild("lazychild-a-" + nodectr, "folder", "child-a-" + nodectr, "asdfasdf ca" + nodectr, null, false);
       tr3 = $.fn.treeTable.createChild("lazychild-b-" + nodectr, "folder", "child-b-" + nodectr, "asdfasdf cb" + nodectr, null, true);

       nodectr++;

       $(tr3).appendBranchTo(parent[0]);
       $(tr2).appendBranchTo(parent[0]);
       $(tr1).appendBranchTo(parent[0]);
    }

    $(".example").treeTable({ lazyfiller: lazyfillerdemo});
<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="lazy-1" class="lazyfill parent">
      <td>lazy click.</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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions