Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My solution is based on <code>dojo/store/Memory</code> inspired by <a href="http://dojotoolkit.org/documentation/tutorials/1.7/store_driven_tree/" rel="nofollow noreferrer">Connecting a Store to a Tree</a>:</p> <p><img src="https://i.stack.imgur.com/yMnah.png" alt="enter image description here"></p> <p>You can find live demo at <a href="http://egoworx.com/" rel="nofollow noreferrer">http://egoworx.com/</a> or download complete source from <a href="http://dl.dropbox.com/u/2131149/tree.zip" rel="nofollow noreferrer">dropbox</a>. </p> <p>Now code. First <code>dojo/store/Memory</code>:</p> <pre><code>var data = {"infos":{"address":"my address","phone":"my phone", "gift": false, "now": new Date()},"insurance":{"forks":[14,53,123],"prices":[5,8,"3%"]}}; var store = new Memory({ data: data, mayHaveChildren: function(object) { var type = this.getType(object); return (type == "Object" || type == "Array"); }, getChildren: function(object, onComplete, onError) { var item = this.getData(object); var type = this.getType(object); var children = []; switch(type) { case "Array": children = item; break; case "Object": for (i in item) { children.push({label: i, data: item[i]}); } break; } onComplete(children); }, getRoot: function(onItem, onError) { onItem(this.data); }, getLabel: function(object) { var label = object.label || object + ""; var type = this.getType(object); switch(type) { case "Number": case "String": case "Boolean": case "Date": var data = this.getData(object); if (data != label) { label += ": " + this.getData(object); } } return label; }, getData: function(object) { if (object &amp;&amp; (object.data || object.data === false) &amp;&amp; object.label) { return object.data; } return object; }, getType: function(object) { var item = this.getData(object); if (lang.isObject(item)) { if (lang.isArray(item)) return "Array"; if (lang.isFunction(item)) return "Function"; if (item instanceof Date) return "Date"; return "Object"; } if (lang.isString(item)) return "String"; if (item === true || item === false) return "Boolean"; return "Number"; }, getIconClass: function(object, opened) { return this.getType(object); } }); </code></pre> <p>Please note I added a <em>boolean</em> and <em>Date</em> type to your data.</p> <p><code>dijit/Tree</code> based on this store:</p> <pre><code>var tree = new Tree({ model: store, persist: false, showRoot: false, getIconClass: function(object, opened) { if (lang.isFunction(this.model.getIconClass)) { return this.model.getIconClass(object, opened); } return (!item || this.model.mayHaveChildren(item)) ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "dijitLeaf"; } }, "placeholder"); tree.startup(); </code></pre> <p>And finally a stylesheet to display data type icons:</p> <pre><code>.dijitTreeIcon { width: 16px; height: 16px; } .Object { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/object.png); } .Array { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/array.png); } .Date { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/date.png); } .Boolean { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/boolean.png); } .String { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/string.png); } .Number { background-image: url(http://dojotoolkit.org/api/css/icons/16x16/number.png); } </code></pre> <p>I cannot access jsFiddle since I'm currently in China, but I'll put the code above there upon my return to Europe and post a link here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload