Project

General

Profile

1
<!DOCTYPE html
2
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml">
5
<head>
6
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
	<title>jsTree v.1.0 - json_data documentation</title>
8
	<script type="text/javascript" src="../_lib/jquery.js"></script>
9
	<script type="text/javascript" src="../_lib/jquery.cookie.js"></script>
10
	<script type="text/javascript" src="../_lib/jquery.hotkeys.js"></script>
11
	<script type="text/javascript" src="../jquery.jstree.js"></script>
12

    
13
	<link type="text/css" rel="stylesheet" href="syntax/!style.css"/>
14
	<link type="text/css" rel="stylesheet" href="!style.css"/>
15
	<script type="text/javascript" src="syntax/!script.js"></script>
16
</head>
17
<body>
18
<div id="container">
19

    
20
<h1>jsTree v.1.0 - json_data plugin</h1>
21
<h2>Description</h2>
22
<div id="description">
23
<p>The <code>json_data</code> plugin enables jsTree to convert JSON objects to interactive trees. The data (JSON) can be set up in the config or retrieved from a server (also ondemand). Version 1.0 also introduces the experimental progressive render feature, which is suitable for large heavy trees, when the DOM would be too heavy to manipulate.</p>
24
<p>The basic structure you need to follow when supplying data in the JSON format is:</p>
25
<div class="code_f">
26
<pre class="brush:js;">
27
{ 
28
	data : "node_title", 
29
	// omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
30
	attr : { id : "node_identificator", some-other-attribute : "attribute_value" }, 
31
	// `state` and `children` are only used for NON-leaf nodes
32
	state: "closed", // or "open", defaults to "closed"
33
	children: [ /* an array of child nodes objects */ ]
34
}
35
</pre>
36
</div>
37
<p>The attr object will appear as attributes on the resulting <code>li</code> node.</p>
38
<p>You may need to pass some attributes to the <code>a</code> node, or set some metadata, or use language versions (for the <a href="languages.html">languages plugin</a>):</p>
39
<div class="code_f">
40
<pre class="brush:js;">
41
{
42
	// `data` can also be an object
43
	data : { 
44
		title : "The node title", 
45
		// omit when not needed
46
		attr : {}, 
47
		// if `icon` contains a slash <code>/</code> it is treated as a file, used for background
48
		// otherwise - it is added as a class to the &lt;ins&gt; node
49
		icon : "folder"
50
	},
51

    
52
	// the `metadata` property will be saved using the jQuery `data` function on the `li` node
53
	metadata : "a string, array, object, etc",
54

    
55
	// if you use the language plugin - just set this property
56
	// also make sure that `data` is an array of objects
57
	language : "en" // any code you are using
58
}
59
</pre>
60
</div>
61
<p>As seen in the first example below - you can also use a simple string to define a node (Child 1 &amp; Child 2).</p>
62
</div>
63

    
64
<h2 id="configuration">Configuration</h2>
65
<div class="panel configuration">
66
<h3>data</h3>
67
<p class="meta">A JSON object (or <code>false</code> if not used). Default is <code>false</code>.</p>
68
<p>Specifies the content to load into the container and convert to a tree.</p>
69
<h3>ajax</h3>
70
<p class="meta">An object (or <code>false</code> if not used). Default is <code>false</code>.</p>
71
<p>The ajax config object is pretty much the same as the <a href="http://api.jquery.com/jQuery.ajax/">jQuery ajax settings object</a>.</p>
72
<p>You can set the <code>data</code> option to a function, that will be executed in the current tree's scope (<code>this</code> will be the tree instance) and gets the node about to be open as a paramater (or <code>-1</code> for initial load). Whatever you return in the <code>data</code> function will be sent to the server as data (so for example you can send the node's ID).</p>
73
<p>You can set the <code>url</code> option to a function, that will be executed in the current tree's scope (<code>this</code> will be the tree instance) and gets the node about to be open as a paramater (or <code>-1</code> for initial load). Whatever you return in the <code>url</code> function will be used as the ajax URL (so that you can accomodate pretty paths such as /get_children/node_2).</p>
74
<p>The <code>error</code> and <code>success</code> functions (if present) also fire in the context of the tree, and if you return a value in the <code>success</code> function it will be used to populate the tree - this can be useful if you want to somehow change what the server returned on the client side before it is displayed in the tree (for example some .NET json implementations require this to work: <code>"success" : function (data) { return data.d; }</code>.</p>
75
<h3>correct_state</h3>
76
<p class="meta">A Boolean. Default is <code>true</code>.</p>
77
<p>If this option is set to <code>true</code> if an AJAX returns an empty result, the node that was about to be opened will be converted to a leaf node (the open icon will no longer be displayed).</p>
78
<h3>progressive_render</h3>
79
<p class="meta">A Boolean. Default is <code>false</code>.</p>
80
<p>If this option is set to <code>true</code> only the visible (open nodes) parts of the returned JSON are converted to DOM nodes, any hidden parts are saved away and parsed ondemand (when a node becomes visible). This is useful when you have a large nested tree which would result in a heavy DOM.</p>
81

    
82
<p class="note"><strong>NOTE:</strong><br />If both <code>data</code> and <code>ajax</code> are set the initial tree is rendered from the <code>data</code> string. When opening a closed node (that has no loaded children) an AJAX request is made.</p>
83
</div>
84

    
85
<h2 id="demos">Demos</h2>
86
<div class="panel">
87

    
88
<h3>Using the data config option</h3>
89
<div id="demo1" class="demo"></div>
90
<script type="text/javascript" class="source">
91
$(function () {
92
	$("#demo1").jstree({ 
93
		"json_data" : {
94
			"data" : [
95
				{ 
96
					"data" : "A node", 
97
					"children" : [ "Child 1", "Child 2" ]
98
				},
99
				{ 
100
					"attr" : { "id" : "li.node.id" }, 
101
					"data" : { 
102
						"title" : "Long format demo", 
103
						"attr" : { "href" : "#" } 
104
					} 
105
				}
106
			]
107
		},
108
		"plugins" : [ "themes", "json_data" ]
109
	});
110
});
111
</script>
112

    
113
<h3>Using the ajax config option</h3>
114
<div id="demo2" class="demo"></div>
115
<script type="text/javascript" class="source">
116
$(function () {
117
	$("#demo2").jstree({ 
118
		"json_data" : {
119
			"ajax" : {
120
				"url" : "_json_data.json",
121
				"data" : function (n) { 
122
					return { id : n.attr ? n.attr("id") : 0 }; 
123
				}
124
			}
125
		},
126
		"plugins" : [ "themes", "json_data" ]
127
	});
128
});
129
</script>
130

    
131
<h3>Using the progressive render config option</h3>
132
<div id="demo3" class="demo"></div>
133
<script type="text/javascript" class="source">
134
$(function () {
135
	$("#demo3").jstree({ 
136
		"json_data" : {
137
			"data" : [
138
				{ 
139
					"data" : "A node", 
140
					"children" : [ "Child 1", "Child 2" ]
141
				},
142
				{ 
143
					"attr" : { "id" : "li.node.id" }, 
144
					"data" : { 
145
						"title" : "Long format demo", 
146
						"attr" : { "href" : "#" } 
147
					} 
148
				}
149
			],
150
			"progressive_render" : true
151
		},
152
		"plugins" : [ "themes", "json_data" ]
153
	});
154
});
155
</script>
156

    
157
<h3>Using both the data &amp; ajax config options</h3>
158
<div id="demo4" class="demo"></div>
159
<script type="text/javascript" class="source">
160
$(function () {
161
	$("#demo4").jstree({ 
162
		"json_data" : {
163
			"data" : [
164
				{ 
165
					"data" : "A node", 
166
					"state" : "closed"
167
				},
168
				{ 
169
					"attr" : { "id" : "li.node.id" }, 
170
					"data" : { 
171
						"title" : "Long format demo", 
172
						"attr" : { "href" : "#" } 
173
					} 
174
				}
175
			],
176
			"ajax" : { "url" : "_json_data.json" }
177
		},
178
		"plugins" : [ "themes", "json_data" ]
179
	});
180
});
181
</script>
182
</div>
183

    
184
<h2 id="api">API</h2>
185
<div class="panel api">
186
<p>Both dummy functions - <code>_is_loaded</code> and <code>load_node</code> are overwritten.</p>
187
<h3 id="load_node_json">.load_node_json ( node , success_callback , error_callback )</h3>
188
<p>This function is called instead of <code>load_node</code>.</p>
189
<ul class="arguments">
190
	<li>
191
		<code class="tp">mixed</code> <strong>node</strong>
192
		<p>This can be a DOM node, jQuery node or selector pointing to an element you want loaded. Use <code>-1</code> for root nodes.</p>
193
	</li>
194
	<li>
195
		<code class="tp">function</code> <strong>success_callback</strong>
196
		<p>A function to be executed once the node is loaded successfully - used internally. You should wait for the <code>load_node</code> event.</p>
197
	</li>
198
	<li>
199
		<code class="tp">function</code> <strong>error_callback</strong>
200
		<p>A function to be executed if the node is not loaded due to an error - used internally. You should wait for the <code>load_node</code> event.</p>
201
	</li>
202
</ul>
203
<h3 id="_parse_json">._parse_json ( data , is_callback )</h3>
204
<p>This function converts JSON nodes to the DOM structure required by jstree. Returns a jQuery object.</p>
205
<ul class="arguments">
206
	<li>
207
		<code class="tp">mixed</code> <strong>node</strong>
208
		<p>This can be a tree node in the JSON format described above, or an array of such JSON nodes, may also be a string.</p>
209
	</li>
210
	<li>
211
		<code class="tp">bool</code> <strong>is_callback</strong>
212
		<p>Specifies if the function is called recursively - used ONLY internally.</p>
213
	</li>
214
</ul>
215
<h3 id="get_json">.get_json ( node , li_attr , a_attr )</h3>
216
<p>This function returns an array of tree nodes converted back to JSON.</p>
217
<ul class="arguments">
218
	<li>
219
		<code class="tp">mixed</code> <strong>node</strong>
220
		<p>This can be a DOM node, jQuery node or selector pointing to an element you want returned. Use <code>-1</code> or omit to get the whole tree.</p>
221
	</li>
222
	<li>
223
		<code class="tp">array</code> <strong>li_attr</strong>
224
		<p>The attributes to collect from the <code>LI</code> node. Defaults to <code>[ "id" , "class" ]</p>
225
	</li>
226
	<li>
227
		<code class="tp">array</code> <strong>a_attr</strong>
228
		<p>The attributes to collect from the <code>A</code> node. Defaults to <code>[ ]</p>
229
	</li>
230
	<li>
231
		<code class="tp">boolean</code> <strong>is_callback</strong>
232
		<p>Used internally.</p>
233
	</li>
234
</ul>
235

    
236
</div>
237

    
238
</div>
239
</body>
240
</html>
(18-18/27)