Męczę się z tym już dosyć długo. Zdołałem przesuwać(sortować) z gałęzi do podgałęzi drzewa. Nie mogę natomiast zmusić do działania sytuacji, gdy chcę wrzucić do tego drzewa element z oddzielnej listy- które będę kiedyś generował w locie(ajax).
Co mam:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. <title>jQuery Sortable With AJAX &amp; MYSQL</title>
  5. <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
  6. <script type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js"></script>
  7. <link rel='stylesheet' href='styles.css' type='text/css' media='all' />
  8. <script type="text/javascript">
  9. // When the document is ready set up our sortable with it's inherant function(s)
  10. $(document).ready(function() {
  11. $(function() {
  12. $('div.tree div:has(div)').addClass('parent'); // Requires jQuery 1.2!
  13. $('div.tree').click(function() {
  14. var o = $(this);
  15. o.children('div').toggle();
  16. o.filter('.parent').toggleClass('expanded');
  17. return false;
  18. });
  19. //$(".draggable").draggable();
  20. //$(".tree").draggable({connectToSortable : ".tree", handle : '.handle',});
  21. $( ".tree" ).droppable({
  22. // accept : ".tree",
  23.  
  24. onDragEnter:function(e,source){
  25. $(source).draggable('options').cursor='auto';
  26. },
  27. onDragLeave:function(e,source){
  28. $(source).draggable('options').cursor='not-allowed';
  29. },
  30. drop: function(ev, ui) {
  31. var dropped = ui.draggable;
  32. var droppedOn = $(this);
  33. //$(dropped).detach().appendTo(droppedOn);
  34. // $(dropped).detach().css({top: 0,left: 0}).appendTo(droppedOn);
  35. },
  36. // drop: function( event, ui ) {
  37. // var draggableId = ui.draggable.attr("id");
  38. // var droppableId = $(this).attr("id");
  39. // var idd = document.elementFromPoint(ui.offset.left,ui.offset.top);
  40.  
  41. // $(idd).append("haha");
  42. //$( this )
  43. //.addClass( "ui-state-highlight" )
  44. //.find( "p" ).html("haha "+droppableId);
  45. // ui.draggable.insertAfter($(this).find("p"))
  46. //var elem = document.elementFromPoint(ui.position.left, ui.position.top);
  47. //elem.insertAfter(ui.draggable);
  48.  
  49. // },
  50. accept: function(d) {
  51. if((d.attr("class")=="tree")){
  52. return true;
  53. }
  54. }
  55. });
  56. });
  57. $(".tree").sortable({
  58. handle : '.handle',
  59. connectWith : '.draggable',
  60. update : function () {
  61.  
  62. }});
  63.  
  64. $(".draggable").sortable({
  65. handle : '.handle',
  66. connectWith : '.tree',
  67. update : function () {
  68. }});
  69.  
  70. });
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. </head>
  78.  
  79. <div id="container">
  80. <pre>
  81. <div id="info">Waiting for update</div>
  82. </pre>
  83. <div id="drop-sort" >
  84. <div id="test-list" class="tree">hmm
  85. <div id="listItem_1" class="tree"><img src="arrow.png" alt="move" width="16" height="16" class="handle" />List1<br />
  86.  
  87. <div id="listItem_1" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" /><strong>Item 1.1 </strong>with a link to <a href="http://www.google.co.uk/" rel="nofollow">Google</a></div>
  88. <div id="listItem_2" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" /><strong>Item 1.2</strong><div class="tree">sub</div></div>
  89. <div id="listItem_3" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" /><strong>Item 1.3</strong><div class="tree">sub</div></div>
  90. <div id="listItem_4" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" /><strong>Item 1.4</strong><div class="tree">sub</div></div>
  91.  
  92. </div>
  93. <p>dr</p>
  94. <div id="listItem_1" class="tree"><img src="arrow.png" alt="move" width="16" height="16" class="handle" />List 2<br />
  95.  
  96.  
  97. <div id="fruit_1" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" />Apple<div class="tree"></div></div>
  98. <div id="fruit_2" class="tree"><img src="arrow.png" alt="move" width="16" height="16" class="handle" />Orange<div class="tree"></div></div>
  99. <div id="fruit_3" class="tree"><img src="arrow.png" alt="move" width="16" height="16" class="handle" />Pear<div class="tree"></div></div>
  100.  
  101. </div>
  102. </div>
  103. </div>
  104. <div id="vert-spacer"></div>
  105. <div id="drag-container">
  106. <div id="drag">
  107. <div id="draggable" class="tree draggable"><img src="arrow.png" alt="move" width="16" height="16" class="handle" /> Drag it!</div>
  108. </div>
  109. <div id="form-box">
  110. <input type="text" id="new-item" /><input type="submit" text="Dodaj" onclick="" />
  111. </div>
  112. </div>
  113. </div>
  114. </body>
  115. </html>

Css:
Kod
/* {    margin: 0; padding: 0; }
*/
body {
    font: 0.9em Arial;
    padding: 40px;
}

#info {
    display: block;
    padding: 10px; margin-bottom: 20px;
    border: 1px solid #333;
    background-color: #efefef;
}

#test-list {
    list-style: none;
}

#test-list li {
    display: block;
    padding: 20px 10px; margin-bottom: 3px;
    background-color: #efefef;
}

#test-list li img.handle {
    margin-right: 20px;
    cursor: move;
}

#draggable {
    width: inherit;

    position: inherit;
    background: silver;
    
}
#container {
    width: 800px;
}
#drop-sort {
    float: left;
    width: 250px;
}
#drag {
    float: right;
    width: 300px;
}
#vert-spacer {
    float: left;
    width: auto;
}
div.tree div {
padding-left:16px;
}
div.tree div.parent div {
display:none;
cursor:default;
}
div.tree div.parent {
cursor:pointer !important;
background:transparent url(plus.gif) no-repeat top left;
}
div.tree div.expanded {
background:transparent url(minus.gif) no-repeat top left;
}

Robię stronę i ta funkcjonalność mnie blokuje- nie ruszę dalej bez tego:(