2_operations.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 &raquo; Examples </title>
  8. <link rel="stylesheet" type="text/css" href="../media/style.css" />
  9. <link rel="shortcut icon" href="../media/favicon.ico" />
  10. <link rel="stylesheet" type="text/css" href="../media/prettify.css" />
  11. <script type="text/javascript" src="../media/prettify.js"></script>
  12. <script type="text/javascript" src="../media/iepngfix_tilebg.js"></script>
  13. <script type="text/javascript">window.onload = function() { prettyPrint(); }</script>
  14. <link rel="stylesheet" type="text/css" href="../../source/tree_component.css" />
  15. <script type="text/javascript" src="../../source/_lib.js"></script>
  16. <script type="text/javascript" src="../../source/tree_component.js"></script>
  17. </head>
  18. <body>
  19. <div id="container">
  20. <div id="head">
  21. <h1>jsTree</h1>
  22. <h2>javascript tree component</h2>
  23. <h3>Examples</h3>
  24. </div>
  25. <div id="sidebar">
  26. <h3 style="margin-top:0;">Documentation</h3>
  27. <ul>
  28. <li class="page_item"><a href="../_documentation/1_files.html" title="Required &amp; optional files">Required &#038; optional files</a></li>
  29. <li class="page_item"><a href="../_documentation/2_creation.html" title="Instance creation">Instance creation</a></li>
  30. <li class="page_item"><a href="../_documentation/3_configuration.html" title="Configuration">Configuration</a></li>
  31. <li class="page_item"><a href="../_documentation/4_data.html" title="Data sources">Data sources</a></li>
  32. <li class="page_item"><a href="../_documentation/5_customize.html" title="Customization">Customization</a></li>
  33. <li class="page_item"><a href="../_documentation/6_output.html" title="Output">Output</a></li>
  34. <li class="page_item"><a href="../_documentation/7_api.html" title="API">API&nbsp;</a></li>
  35. </ul>
  36. <h3>Examples</h3>
  37. <ul>
  38. <li class="page_item"><a href="1_datasources.html" title="Data sources">Data sources</a></li>
  39. <li class="page_item current_page_item"><a href="2_operations.html" title="Operations">Operations</a></li>
  40. <li class="page_item"><a href="3_callbacks.html" title="Callbacks">Callbacks</a></li>
  41. <li class="page_item"><a href="4_themes.html" title="Themes">Themes</a></li>
  42. <li class="page_item"><a href="5_others.html" title="Others">Other examples</a></li>
  43. <li class="page_item important"><a href="7_full.html" title="Full example">Full PHP &amp; mySQL example&nbsp;</a></li>
  44. </ul>
  45. </div>
  46. <div id="data" style="margin-top:-45px;">
  47. <div id="content">
  48. <ul class='nav'>
  49. <li><a href="1_datasources.html" title="Data sources">&laquo;&nbsp;Data sources</a></li>
  50. <li><a href="../index.html">Up to Reference</a></li>
  51. <li><a href="3_callbacks.html" title="Callbacks">Callbacks&nbsp;&raquo;</a></li>
  52. </ul>
  53. <h2 style="margin-top:25px;">Operations</h2>
  54. <div class="entry"><div id="documentation">
  55. <p>All of the examples on this page, are built on the predefined JSON example from the data sources demo.</p>
  56. <h2>Selecting nodes</h2>
  57. <p>The user can select nodes by clicking on them (in this example if Ctrl is held multiple nodes can be selected).</p>
  58. <p>You can define a node to be selected onload by using the <span class="var">selected</span> config setting (check documentation for more).</p>
  59. <p>The <span class="var">get_*</span> functions are most useful for keyboard shortcuts - they can move either the hover or selected states - this is defined using the <span class="var">ui.hover_mode</span> config setting or using the <span class="var">force</span> argument in each of the functions.</p>
  60. <p>The <span class="var">hover_branch</span> functions moves the hover state to a specific node.</p>
  61. <p>There are a few ways to select nodes programatically:</p>
  62. <input type="button" value="select_branch" onclick="$.tree_reference('demo1').select_branch($('#pjson_4'));" />
  63. <input type="button" value="hover_branch" onclick="$.tree_reference('demo1').hover_branch($('#pjson_3'));" />
  64. <input type="button" value="get_next" onclick="$.tree_reference('demo1').get_next();"/>
  65. <input type="button" value="get_prev" onclick="$.tree_reference('demo1').get_prev();" />
  66. <input type="button" value="get_left" onclick="$.tree_reference('demo1').get_left();" />
  67. <input type="button" value="get_right" onclick="$.tree_reference('demo1').get_right();" />
  68. <div id="demo1" class="demo"></div>
  69. <div id="sources1" class="sources">
  70. <pre>
  71. $("#demo1").tree({
  72. // Data settings omitted
  73. selected : "pjson_1",
  74. rules : { multiple : "ctrl" }
  75. });
  76. // Button codes
  77. $.tree_reference('demo1').select_branch($('#pjson_4'));
  78. $.tree_reference('demo1').hover_branch($('#pjson_3'));
  79. $.tree_reference('demo1').get_next();
  80. $.tree_reference('demo1').get_prev();
  81. $.tree_reference('demo1').get_left();
  82. $.tree_reference('demo1').get_right();
  83. </pre>
  84. </div>
  85. <script type="text/javascript">
  86. $(function () {
  87. $("#demo1").tree({
  88. data : {
  89. type : "json",
  90. json : [
  91. { attributes: { id : "pjson_1" }, state: "open", data: "Root node 1", children : [
  92. { attributes: { id : "pjson_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  93. { attributes: { id : "pjson_3" }, data: "Child node 2" },
  94. { attributes: { id : "pjson_4" }, data: "Some other child node" }
  95. ]},
  96. { attributes: { id : "pjson_5" }, data: "Root node 2" }
  97. ]
  98. },
  99. selected : "pjson_1",
  100. rules : { multiple : "ctrl" }
  101. });
  102. });
  103. </script>
  104. <h2>Open/close nodes</h2>
  105. <p>The user can open and close nodes by clicking the open/close icon where applicable. Here are the programmtic ways:</p>
  106. <input type="button" value="open_branch" onclick="$.tree_reference('demo2').open_branch($('#pjson1_1'));" />
  107. <input type="button" value="close_branch" onclick="$.tree_reference('demo2').close_branch($('#pjson1_1'));" />
  108. <input type="button" value="toggle_branch" onclick="$.tree_reference('demo2').toggle_branch($('#pjson1_1'));" />
  109. <div id="demo2" class="demo"></div>
  110. <div id="sources2" class="sources">
  111. <pre>
  112. // Button codes
  113. $.tree_reference('demo2').open_branch($('#pjson_1'));
  114. $.tree_reference('demo2').close_branch($('#pjson_1'));
  115. $.tree_reference('demo2').toggle_branch($('#pjson_1'));
  116. </pre>
  117. </div>
  118. <script type="text/javascript">
  119. $(function () {
  120. $("#demo2").tree({
  121. data : {
  122. type : "json",
  123. json : [
  124. { attributes: { id : "pjson1_1" }, data: "Root node 1", children : [
  125. { attributes: { id : "pjson1_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  126. { attributes: { id : "pjson1_3" }, data: "Child node 2" },
  127. { attributes: { id : "pjson1_4" }, data: "Some other child node" }
  128. ]},
  129. { attributes: { id : "pjson1_5" }, data: "Root node 2" }
  130. ]
  131. }
  132. });
  133. });
  134. </script>
  135. <h2>Creating nodes</h2>
  136. <p>You can create nodes programatically with the <span class="var">create</span> function. You can place the new node inside another node (even at a specific position among its children) or <span class="val">"below"</span> or <span class="val">"after"</span> the node (if you omit the reference node, the currently selected is used). This is controlled by the <span class="var">rules.createat</span> config option, and the third argument of the create function.</p>
  137. <input type="button" value="create" onclick="$.tree_reference('demo0').create();" />
  138. <input type="button" value="create with title" onclick="$.tree_reference('demo0').create({ data: 'Created node' },$('#pjson0_3'));" />
  139. <input type="button" value="create with title and icon" onclick="$.tree_reference('demo0').create({ attributes : { 'class' : 'cc' }, data: { title : 'ID and ICON', icon : '../media/images/ok.png' } },$('#pjson0_3'));" />
  140. <div id="demo0" class="demo"></div>
  141. <div id="sources0" class="sources">
  142. <pre>
  143. // Button codes
  144. $.tree_reference('demo0').create();
  145. $.tree_reference('demo0').create({ data: 'Created node' },$('#pjson0_3'));
  146. $.tree_reference('demo0').create({ attributes : { 'class' : 'cc' }, data: { title : 'ID and ICON', icon : '../media/images/ok.png' } },$('#pjson0_3'));
  147. </pre>
  148. </div>
  149. <script type="text/javascript">
  150. $(function () {
  151. $("#demo0").tree({
  152. data : {
  153. type : "json",
  154. json : [
  155. { attributes: { id : "pjson0_1" }, data: "Root node 1", children : [
  156. { attributes: { id : "pjson0_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  157. { attributes: { id : "pjson0_3" }, data: "Child node 2" },
  158. { attributes: { id : "pjson0_4" }, data: "Some other child node" }
  159. ]},
  160. { attributes: { id : "pjson0_5" }, data: "Root node 2" }
  161. ]
  162. }
  163. });
  164. });
  165. </script>
  166. <h2>Delete nodes (refresh tree)</h2>
  167. <p>The <span class="var">remove</span> function removes the currently selected node, or the node it receives as an argument from the tree.</p>
  168. <p>The <span class="var">refresh</span> function is also present in this example so that you can refresh the tree after deleting nodes.</p>
  169. <p>By default deleting nodes is not allowed, this is why in this example <span class="var">rules.deletable</span> is set to <span class="val">"all"</span> (check the documentation for more)</p>
  170. <input type="button" value="refresh" onclick="$.tree_reference('demo3').refresh();" />
  171. <input type="button" value="remove" onclick="$.tree_reference('demo3').remove();" />
  172. <input type="button" value="remove($('#pjson2_3'))" onclick="$.tree_reference('demo3').remove($('#pjson2_3'));" />
  173. <div id="demo3" class="demo"></div>
  174. <div id="sources3" class="sources">
  175. <pre>
  176. $(function () {
  177. $("#demo3").tree({
  178. // Data settings omitted
  179. rules : {
  180. deletable : "all"
  181. }
  182. });
  183. });
  184. // Button codes
  185. $.tree_reference('demo3').refresh();
  186. $.tree_reference('demo3').remove();
  187. $.tree_reference('demo3').remove($('#pjson3_1'));
  188. </pre>
  189. </div>
  190. <script type="text/javascript">
  191. $(function () {
  192. $("#demo3").tree({
  193. data : {
  194. type : "json",
  195. json : [
  196. { attributes: { id : "pjson2_1" }, data: "Root node 1", children : [
  197. { attributes: { id : "pjson2_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  198. { attributes: { id : "pjson2_3" }, data: "Child node 2" },
  199. { attributes: { id : "pjson2_4" }, data: "Some other child node" }
  200. ]},
  201. { attributes: { id : "pjson2_5" }, data: "Root node 2" }
  202. ]
  203. },
  204. rules : {
  205. deletable : "all"
  206. }
  207. });
  208. });
  209. </script>
  210. <h2>Rename nodes</h2>
  211. <p>The <span class="var">rename</span> function renames the currently selected node, or the node it receives as an argument.</p>
  212. <p class="note">NOTE: when passing an argument (renaming a node that is not selected), renaming might look a little odd according to theme.</p>
  213. <input type="button" value="rename" onclick="$.tree_reference('demo4').rename();" />
  214. <input type="button" value="rename($('#pjson3_2'))" onclick="$.tree_reference('demo4').rename($('#pjson3_2'));" />
  215. <div id="demo4" class="demo"></div>
  216. <div id="sources4" class="sources">
  217. <pre>
  218. // Button codes
  219. $.tree_reference('demo4').rename();
  220. $.tree_reference('demo4').rename($('#pjson3_2'));
  221. </pre>
  222. </div>
  223. <script type="text/javascript">
  224. $(function () {
  225. $("#demo4").tree({
  226. data : {
  227. type : "json",
  228. json : [
  229. { attributes: { id : "pjson3_1" }, data: "Root node 1", children : [
  230. { attributes: { id : "pjson3_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  231. { attributes: { id : "pjson3_3" }, data: "Child node 2" },
  232. { attributes: { id : "pjson3_4" }, data: "Some other child node" }
  233. ]},
  234. { attributes: { id : "pjson3_5" }, data: "Root node 2" }
  235. ]
  236. }
  237. });
  238. });
  239. </script>
  240. <h2>Moving/copying nodes</h2>
  241. <p>The user can move nodes around using drag'n'drop. As for copying the <span class="var">rules.drag_copy</span> config option controls the behavior (disabled, when the ctrl key is down, always copy).</p>
  242. <p>In this example the <span class="var">rules.draggable</span> config option is set to <span class="val">"all"</span> so you can drag all nodes around.</p>
  243. <p>You can drag nodes between trees which have <span class="var">rules.multitree</span> set to <span class="val">true</span>.</p>
  244. <p>You can also use the <span class="var">copy, cut, paste</span> functions on the first tree.</p>
  245. <input type="button" value="copy" onclick="$.tree_reference('demo5').copy();" />
  246. <input type="button" value="cut" onclick="$.tree_reference('demo5').cut();" />
  247. <input type="button" value="paste" onclick="$.tree_reference('demo5').paste();" />
  248. <div style="width:200px; float:left;">
  249. <div id="demo5" class="demo"></div>
  250. <div id="demo6" class="demo" style="margin-top:10px;"></div>
  251. </div>
  252. <div id="sources5" class="sources">
  253. <pre>
  254. $(function () {
  255. $("#demo5").tree({
  256. // Data settings omitted
  257. rules : {
  258. multitree : true,
  259. draggable : "all"
  260. }
  261. });
  262. });
  263. // Button codes
  264. $.tree_reference('demo5').copy();
  265. $.tree_reference('demo5').cut();
  266. $.tree_reference('demo5').paste();
  267. </pre>
  268. </div>
  269. <script type="text/javascript">
  270. $(function () {
  271. $("#demo5").tree({
  272. data : {
  273. type : "json",
  274. json : [
  275. { attributes: { id : "pjson4_1" }, data: "Root node 1", children : [
  276. { attributes: { id : "pjson4_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  277. { attributes: { id : "pjson4_3" }, data: "Child node 2" },
  278. { attributes: { id : "pjson4_4" }, data: "Some other child node" }
  279. ]},
  280. { attributes: { id : "pjson4_5" }, data: "Root node 2" }
  281. ]
  282. },
  283. rules : {
  284. multitree : true,
  285. draggable : "all"
  286. }
  287. });
  288. $("#demo6").tree({
  289. data : {
  290. type : "json",
  291. json : [
  292. { attributes: { id : "pjson5_1" }, data: "Root node 1", children : [
  293. { attributes: { id : "pjson5_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
  294. { attributes: { id : "pjson5_3" }, data: "Child node 2" },
  295. { attributes: { id : "pjson5_4" }, data: "Some other child node" }
  296. ]},
  297. { attributes: { id : "pjson5_5" }, data: "Root node 2" }
  298. ]
  299. },
  300. rules : {
  301. multitree : true,
  302. draggable : "all"
  303. }
  304. });
  305. });
  306. </script>
  307. </div></div>
  308. </div>
  309. </div>
  310. </div>
  311. </body>
  312. </html>