Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [jQuery][Ajax] JSTree - id klikniętego elementu
Forum PHP.pl > Forum > XML, AJAX
einter-project
Witam

Zainstalowałem sobie asynchroniczne dzrzewo jsTree.
http://testowa.shop-project.net/

Chce teraz dodać zdarzenie aby po kliknięciu w nazwę kategorii po prawej pokazywało dane o tej kategorii, ale nie wiem jak wyciągnąć id klikniętego elementu.

  1. <script type="text/javascript">
  2. $(function () {
  3. $("#category").jstree({
  4.  
  5. // List of active plugins
  6. "plugins" : [
  7. "themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu"
  8. ],
  9.  
  10. // I usually configure the plugin that handles the data first
  11. // This example uses JSON as it is most common
  12. "json_data" : {
  13. // This tree is ajax enabled - as this is most common, and maybe a bit more complex
  14. // All the options are almost the same as jQuery's AJAX (read the docs)
  15. "ajax" : {
  16. // the URL to fetch the data
  17. "url" : "php/server.php",
  18. // the `data` function is executed in the instance's scope
  19. // the parameter is the node being loaded
  20. // (may be -1, 0, or undefined when loading the root nodes)
  21. "data" : function (n) {
  22. // the result is fed to the AJAX request `data` option
  23. return {
  24. "operation" : "get_children",
  25. "id" : n.attr ? n.attr("id").replace("node_","") : 1
  26. };
  27. }
  28. }
  29. },
  30.  
  31. // Configuring the search plugin
  32. "search" : {
  33. // As this has been a common question - async search
  34. // Same as above - the `ajax` config option is actually jQuery's AJAX object
  35. "ajax" : {
  36. "url" : "php/server.php",
  37. // You get the search string as a parameter
  38. "data" : function (str) {
  39. return {
  40. "operation" : "search",
  41. "search_str" : str
  42. };
  43. }
  44. }
  45. },
  46.  
  47. // Using types - most of the time this is an overkill
  48. // read the docs carefully to decide whether you need types
  49. "types" : {
  50. // I set both options to -2, as I do not need depth and children count checking
  51. // Those two checks may slow jstree a lot, so use only when needed
  52. "max_depth" : -2,
  53. "max_children" : -2,
  54. // I want only `drive` nodes to be root nodes
  55. // This will prevent moving or creating any other type as a root node
  56. "valid_children" : [ "drive" ],
  57. "types" : {
  58. // The default type
  59. "default" : {
  60. // I want this type to have no children (so only leaf nodes)
  61. // In my case - those are files
  62. "valid_children" : "none",
  63. // If we specify an icon for the default type it WILL OVERRIDE the theme icons
  64. "icon" : {
  65. "image" : "img/file.png"
  66. }
  67. },
  68.  
  69. // The `folder` type
  70. "folder" : {
  71. // can have files and other folders inside of it, but NOT `drive` nodes
  72. "valid_children" : [ "default", "folder" ],
  73. "icon" : {
  74. "image" : "img/folder.png"
  75. }
  76. },
  77.  
  78. // The `drive` nodes
  79. "drive" : {
  80. // can have files and folders inside, but NOT other `drive` nodes
  81. "valid_children" : [ "default", "folder" ],
  82. "icon" : {
  83. "image" : "img/root.png"
  84. },
  85. // those prevent the functions with the same name to be used on `drive` nodes
  86. // internally the `before` event is used
  87. "start_drag" : false,
  88. "move_node" : false,
  89. "delete_node" : false,
  90. "remove" : false
  91. }
  92. }
  93. },
  94.  
  95. // UI & core - the nodes to initially select and open will be overwritten by the cookie plugin
  96. // the UI plugin - it handles selecting/deselecting/hovering nodes
  97. "ui" : {
  98. // this makes the node with ID node_4 selected onload
  99. "initially_select" : [ "node_4" ]
  100. },
  101.  
  102. // the core plugin - not many options here
  103. "core" : {
  104. // just open those two nodes up
  105. // as this is an AJAX enabled tree, both will be downloaded from the server
  106. "initially_open" : [ "node_2" , "node_3" ]
  107. }
  108. })
  109.  
  110.  
  111. .bind("create.jstree", function (e, data) {
  112. $.post(
  113. "php/server.php",
  114. {
  115. "operation" : "create_node",
  116. "id" : data.rslt.parent.attr("id").replace("node_",""),
  117. "position" : data.rslt.position,
  118. "title" : data.rslt.name,
  119. "type" : data.rslt.obj.attr("rel")
  120. },
  121. function (r) {
  122. if(r.status) {
  123. $(data.rslt.obj).attr("id", "node_" + r.id);
  124.  
  125. } else {
  126. $.jstree.rollback(data.rlbk);
  127. }
  128. }
  129. );
  130. })
  131.  
  132.  
  133. .bind("remove.jstree", function (e, data) {
  134. data.rslt.obj.each(function () {
  135. $.ajax({
  136. async : false,
  137. type: 'POST',
  138. url: "php/server.php",
  139. data : {
  140. "operation" : "remove_node",
  141. "id" : this.id.replace("node_","")
  142. },
  143. success : function (r) {
  144. if(!r.status) {
  145. data.inst.refresh();
  146. }
  147. }
  148. });
  149. });
  150. })
  151.  
  152.  
  153. .bind("rename.jstree", function (e, data) {
  154. $.post(
  155. "php/server.php",
  156. {
  157. "operation" : "rename_node",
  158. "id" : data.rslt.obj.attr("id").replace("node_",""),
  159. "title" : data.rslt.new_name
  160. },
  161. function (r) {
  162. if(!r.status) {
  163. $.jstree.rollback(data.rlbk);
  164. }
  165. }
  166. );
  167. })
  168.  
  169.  
  170. .bind("move_node.jstree", function (e, data) {
  171. data.rslt.o.each(function (i) {
  172. $.ajax({
  173. async : false,
  174. type: 'POST',
  175. url: "php/server.php",
  176. data : {
  177. "operation" : "move_node",
  178. "id" : $(this).attr("id").replace("node_",""),
  179. "ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""),
  180. "position" : data.rslt.cp + i,
  181. "title" : data.rslt.name,
  182. "copy" : data.rslt.cy ? 1 : 0
  183. },
  184. success : function (r) {
  185. if(!r.status) {
  186. $.jstree.rollback(data.rlbk);
  187.  
  188. } else {
  189. $(data.rslt.oc).attr("id", "node_" + r.id);
  190. if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
  191. data.inst.refresh(data.inst._get_parent(data.rslt.oc));
  192. }
  193. }
  194. $("#analyze").click();
  195. }
  196. });
  197. });
  198. });
  199. });
  200.  
  201. // Code for the menu buttons
  202. $(function () {
  203. $("#mmenu input").click(function () {
  204. switch(this.id) {
  205. case "add_default":
  206. case "add_folder":
  207. $("#category").jstree("create", null, "last", { "attr" : { "rel" : this.id.toString().replace("add_", "") } });
  208. break;
  209. case "search":
  210. $("#category").jstree("search", document.getElementById("text").value);
  211. break;
  212. case "text": break;
  213. default:
  214. $("#category").jstree(this.id);
  215. break;
  216. }
  217. });
  218.  
  219.  
  220. $("#category a").live("click", function(e) {
  221. // Tutaj id kliknietago elementu
  222.  
  223.  
  224. });
  225.  
  226. });


Bardzo prosiłbym o naprowadzenie jak wyciągnąć id klikniętego elementu.
thek
To plugin jQuery, więc chyba możesz zerknąć czym są klikane elementy i przechwycić zdarzenie click? wink.gif A poza tym dokumentacja pomaga nieco... Zwróć uwagę na dział UI, a zwłaszcza select_node smile.gif
einter-project
Witam
Dzięki za odpowiedź, niem znam jQuery, dopiero się ucze.

Wykombinowałem coś takiego, zwraca mi id z tym że nie wiem na ile to poprawnie napisałem:

  1. $("#category").delegate("a","click", function(e) {
  2. var idn = $(this).parent().attr("id").split("_")[1];
  3.  
  4. $('#categoryName').html(idn);
  5.  
  6. });
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.