Loading ...
Sorry, an error occurred while loading the content.

Re: Fw: [ydn-javascript] Initialize TreeView with JSON...

Expand Messages
  • santini.alberto
    ... arguments, ... function is ... Modifying the walking function (thanks Mr. Crockford) you can find in http://www.json.org/json2.js I can build a function
    Message 1 of 2 , Jun 28, 2008
    • 0 Attachment
      --- In ydn-javascript@yahoogroups.com, "Daniel Barreiro" <satyam@...>
      wrote:
      >
      >
      > Method parse of YUI's JSON utility allows you to give it a function,
      > nevertheless, that function only receives key-value pairs as its
      arguments,
      > not the depth, which is vital for you. Anyway, once you parse the JSON
      > string into an actual object, traversing it with a recursive
      function is
      > not
      > much harder than using the tree walker.
      >
      > Satyam

      Modifying the walking function (thanks Mr. Crockford) you can find in
      http://www.json.org/json2.js
      I can build a function populating a TreeView widget with a JSON object.

      function initTree() {
      var oTreeView = new YAHOO.widget.TreeView("treeDiv1");
      var rootNode = oTreeView.getRoot();

      function walk(holder, key, lastNode) {
      var k, v, value = holder[key];
      if (value && typeof value === 'object') {
      if (lastNode !== undefined) {
      lastNode = new YAHOO.widget.MenuNode(value, lastNode);
      } else {
      lastNode = new YAHOO.widget.MenuNode(value, rootNode);
      }
      for (k in value) {
      if (Object.hasOwnProperty.call(value, k)) {
      v = walk(value, k, lastNode);
      if (v !== undefined) {
      value[k] = v;
      } else {
      delete value[k];
      }
      }
      }
      }
      }

      // catalog_json is a JSON object, 'catalog' is the root node
      walk(catalog_json, 'catalog');
      oTreeView.draw();
      }

      Thanks Daniel for your hint,
      Alberto
    Your message has been successfully submitted and would be delivered to recipients shortly.