PageRenderTime 202ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/sample-select.html

https://github.com/briscoeej/fancytree
HTML | 302 lines | 269 code | 25 blank | 8 comment | 0 complexity | 65c2f04d8b069d33ae4a3bcdd1b4ba71 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  5. <title>Fancytree - Example: Select</title>
  6. <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
  7. <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js" type="text/javascript"></script>
  8. <link href="../src/skin-win7/ui.fancytree.css" rel="stylesheet" type="text/css">
  9. <script src="../src/jquery.fancytree.js" type="text/javascript"></script>
  10. <!-- Start_Exclude: This block is not part of the sample code -->
  11. <link href="../lib/prettify.css" rel="stylesheet">
  12. <script src="../lib/prettify.js" type="text/javascript"></script>
  13. <link href="sample.css" rel="stylesheet" type="text/css">
  14. <script src="sample.js" type="text/javascript"></script>
  15. <!-- End_Exclude -->
  16. <style type="text/css">
  17. </style>
  18. <script type="text/javascript">
  19. var treeData = [
  20. {title: "item1 with key and tooltip", tooltip: "Look, a tool tip!" },
  21. {title: "item2: selected on init", selected: true },
  22. {title: "Folder", folder: true, key: "id3",
  23. children: [
  24. {title: "Sub-item 3.1",
  25. children: [
  26. {title: "Sub-item 3.1.1", key: "id3.1.1" },
  27. {title: "Sub-item 3.1.2", key: "id3.1.2" }
  28. ]
  29. },
  30. {title: "Sub-item 3.2",
  31. children: [
  32. {title: "Sub-item 3.2.1", key: "id3.2.1" },
  33. {title: "Sub-item 3.2.2", key: "id3.2.2" }
  34. ]
  35. }
  36. ]
  37. },
  38. {title: "Document with some children (expanded on init)", key: "id4", expanded: true,
  39. children: [
  40. {title: "Sub-item 4.1 (active on init)", active: true,
  41. children: [
  42. {title: "Sub-item 4.1.1", key: "id4.1.1" },
  43. {title: "Sub-item 4.1.2", key: "id4.1.2" }
  44. ]
  45. },
  46. {title: "Sub-item 4.2 (selected on init)", selected: true,
  47. children: [
  48. {title: "Sub-item 4.2.1", key: "id4.2.1" },
  49. {title: "Sub-item 4.2.2", key: "id4.2.2" }
  50. ]
  51. },
  52. {title: "Sub-item 4.3 (hideCheckbox)", hideCheckbox: true },
  53. {title: "Sub-item 4.4 (unselectable)", unselectable: true }
  54. ]
  55. },
  56. {title: "Lazy folder", folder: true, lazy: true }
  57. ];
  58. $(function(){
  59. // --- Initialize sample trees
  60. $("#tree1").fancytree({
  61. // extensions: ["select"],
  62. checkbox: true,
  63. selectMode: 1,
  64. source: treeData,
  65. activate: function(event, data) {
  66. $("#echoActive1").text(data.node.title);
  67. },
  68. select: function(event, data) {
  69. // Display list of selected nodes
  70. var s = data.tree.getSelectedNodes().join(", ");
  71. $("#echoSelection1").text(s);
  72. },
  73. dblclick: function(event, data) {
  74. data.node.toggleSelected();
  75. },
  76. keydown: function(event, data) {
  77. if( event.which === 32 ) {
  78. data.node.toggleSelected();
  79. return false;
  80. }
  81. }
  82. });
  83. $("#tree2").fancytree({
  84. // extensions: ["select"],
  85. checkbox: true,
  86. selectMode: 2,
  87. source: treeData,
  88. select: function(event, data) {
  89. // Display list of selected nodes
  90. var selNodes = data.tree.getSelectedNodes();
  91. // convert to title/key array
  92. var selKeys = $.map(selNodes, function(node){
  93. return "[" + node.key + "]: '" + node.title + "'";
  94. });
  95. $("#echoSelection2").text(selKeys.join(", "));
  96. },
  97. click: function(event, data) {
  98. // We should not toggle, if target was "checkbox", because this
  99. // would result in double-toggle (i.e. no toggle)
  100. if( $.ui.fancytree.getEventTargetType(event) === "title" ){
  101. data.node.toggleSelected();
  102. }
  103. },
  104. keydown: function(event, data) {
  105. if( event.which === 32 ) {
  106. data.node.toggleSelected();
  107. return false;
  108. }
  109. },
  110. // The following options are only required, if we have more than one tree on one page:
  111. cookieId: "fancytree-Cb2",
  112. idPrefix: "fancytree-Cb2-"
  113. });
  114. $("#tree3").fancytree({
  115. // extensions: ["select"],
  116. checkbox: true,
  117. selectMode: 3,
  118. source: treeData,
  119. lazyLoad: function(event, ctx) {
  120. ctx.result = {url: "ajax-sub2.json", debugDelay: 1000};
  121. },
  122. loadChildren: function(event, ctx) {
  123. ctx.node.fixSelection3AfterClick();
  124. },
  125. select: function(event, data) {
  126. // Get a list of all selected nodes, and convert to a key array:
  127. var selKeys = $.map(data.tree.getSelectedNodes(), function(node){
  128. return node.key;
  129. });
  130. $("#echoSelection3").text(selKeys.join(", "));
  131. // Get a list of all selected TOP nodes
  132. var selRootNodes = data.tree.getSelectedNodes(true);
  133. // ... and convert to a key array:
  134. var selRootKeys = $.map(selRootNodes, function(node){
  135. return node.key;
  136. });
  137. $("#echoSelectionRootKeys3").text(selRootKeys.join(", "));
  138. $("#echoSelectionRoots3").text(selRootNodes.join(", "));
  139. },
  140. dblclick: function(event, data) {
  141. data.node.toggleSelected();
  142. },
  143. keydown: function(event, data) {
  144. if( event.which === 32 ) {
  145. data.node.toggleSelected();
  146. return false;
  147. }
  148. },
  149. // The following options are only required, if we have more than one tree on one page:
  150. // initId: "treeData",
  151. cookieId: "fancytree-Cb3",
  152. idPrefix: "fancytree-Cb3-"
  153. });
  154. $("#tree4").fancytree({
  155. // extensions: ["select"],
  156. checkbox: false,
  157. selectMode: 2,
  158. source: treeData,
  159. beforeSelect: function(event, data) {
  160. if( data.node.folder ){
  161. return false;
  162. }
  163. },
  164. select: function(event, data) {
  165. // Display list of selected nodes
  166. var selNodes = data.tree.getSelectedNodes();
  167. // convert to title/key array
  168. var selKeys = $.map(selNodes, function(node){
  169. return "[" + node.key + "]: '" + node.title + "'";
  170. });
  171. $("#echoSelection4").text(selKeys.join(", "));
  172. },
  173. click: function(event, data) {
  174. if( ! data.node.folder ){
  175. data.node.toggleSelected();
  176. }
  177. },
  178. dblclick: function(event, data) {
  179. data.node.toggleExpanded();
  180. },
  181. keydown: function(event, data) {
  182. if( event.which === 32 ) {
  183. data.node.toggleSelected();
  184. return false;
  185. }
  186. },
  187. // The following options are only required, if we have more than one tree on one page:
  188. // initId: "treeData",
  189. cookieId: "fancytree-Cb4",
  190. idPrefix: "fancytree-Cb4-"
  191. });
  192. $("#btnToggleSelect").click(function(){
  193. $("#tree2").fancytree("getRootNode").visit(function(node){
  194. node.toggleSelected();
  195. });
  196. return false;
  197. });
  198. $("#btnDeselectAll").click(function(){
  199. $("#tree2").fancytree("getTree").visit(function(node){
  200. node.setSelected(false);
  201. });
  202. return false;
  203. });
  204. $("#btnSelectAll").click(function(){
  205. $("#tree2").fancytree("getTree").visit(function(node){
  206. node.setSelected(true);
  207. });
  208. return false;
  209. });
  210. });
  211. </script>
  212. </head>
  213. <body class="example">
  214. <h1>Example: Selection and checkbox</h1>
  215. <!-- Tree #1 -->
  216. <p class="description">
  217. This tree has <b>checkoxes and selectMode 1 (single-selection)</b> enabled.<br>
  218. A double-click handler selects a <i>document</i> node (not folders).<br>
  219. A keydown handler selects on [space].<br>
  220. The <code>checkbox</code> icons are replaced by radio buttons by adding
  221. the 'fancytree-radio' class to the container.<br>
  222. Note: the initialization data contains multiple selected nodes. This is
  223. considered bad input data and <b>not</b> fixed automatically (only on
  224. the first click).
  225. </p>
  226. <div>
  227. <label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select>
  228. </div>
  229. <div id="tree1" class="fancytree-radio">
  230. </div>
  231. <div>Active node: <span id="echoActive1">-</span></div>
  232. <div>Selection: <span id="echoSelection1">-</span></div>
  233. <!-- Tree #2 -->
  234. <p class="description">
  235. This tree has <b>selectMode 2 (multi-selection)</b> enabled.<br>
  236. A single-click handler selects the node.<br>
  237. A keydown handler selects on [space].
  238. </p>
  239. <p>
  240. <a href="#" id="btnSelectAll">Select all</a> -
  241. <a href="#" id="btnDeselectAll">Deselect all</a> -
  242. <a href="#" id="btnToggleSelect">Toggle select</a>
  243. </p>
  244. <div id="tree2"></div>
  245. <div>Selected keys: <span id="echoSelection2">-</span></div>
  246. <!-- Tree #3 -->
  247. <p class="description">
  248. This tree has <b>checkoxes and selectMode 3 (hierarchical multi-selection)</b> enabled.<br>
  249. A double-click handler selects the node.<br>
  250. A keydown handler selects on [space].
  251. </p>
  252. <div id="tree3"></div>
  253. <div>Selected keys: <span id="echoSelection3">-</span></div>
  254. <div>Selected root keys: <span id="echoSelectionRootKeys3">-</span></div>
  255. <div>Selected root nodes: <span id="echoSelectionRoots3">-</span></div>
  256. <!-- Tree #4 -->
  257. <p class="description">
  258. This tree has selectMode 2 (multi-selection) enabled, but <b>no checkboxes</b>.<br>
  259. A single-click handler selects the node.<br>
  260. A keydown handler selects on [space].<br>
  261. A double-click handler expands documents.<br>
  262. A onQuerySelect handler prevents selection of folders.
  263. </p>
  264. <div id="tree4"></div>
  265. <div>Selected keys: <span id="echoSelection4">-</span></div>
  266. <!-- Start_Exclude: This block is not part of the sample code -->
  267. <hr>
  268. <p class="sample-links no_code">
  269. <a class="hideInsideFS" href="https://github.com/mar10/fancytree">jquery.fancytree.js project home</a>
  270. <a class="hideOutsideFS" href="#">Link to this page</a>
  271. <a class="hideInsideFS" href="index.html">Example Browser</a>
  272. <a href="#" id="codeExample">View source code</a>
  273. </p>
  274. <pre id="sourceCode" class="prettyprint" style="display:none"></pre>
  275. <!-- End_Exclude -->
  276. </body>
  277. </html>