Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. UI draggable   droppable append anywhere?
  2. $("#catalog ul, #cart ol").sortable().disableSelection();
  3.        
  4. //EDIT - for the droppable behaviour to meet the requiremens,
  5. //we need to make individual items in the cart into droppables;
  6. //as more elements are added at runtime, droppable has to be
  7. //re-initiated, otherwise you'll end up only being to drop
  8. //onto the first item in the cart (not cool)
  9. function initDroppable() {
  10.     $("#cart ol li").droppable({
  11.         accept: "#catalog ul li",
  12.         hoverClass: "ui-state-hover",
  13.         drop: function(event, ui) {
  14.             //we're going to clone the item from the cart and
  15.             //insert it after "this", which refers to the item
  16.             //in cart being "dropped onto"
  17.             $("<li></li>").text(ui.draggable.text()).insertAfter(this);
  18.             $("#cart ol li").droppable("destroy");
  19.             initDroppable();
  20.             event.preventDefault();
  21.             return false;
  22.         }
  23.     });
  24. }
  25.  
  26. //now initialize the cart as a droppable for the first run
  27. initDroppable();
  28.        
  29. var $catalog_items = $("h3" ).droppable({
  30.     accept: "#catalog ul li",
  31.     hoverClass: "ui-state-hover",
  32.     drop: function(event, ui) {
  33.         //reference to the original item that's being dragged
  34.         var $item = $(event.target);
  35.         //well, this is awkward. I couldn't find it in jQuery
  36.         //doco how to find the accordion's list... so we are
  37.         //looking for a "ul" inside of the element that
  38.         //immediately follows the accordion expander element
  39.         //This works out to be "ul inside a div after this h3" :)
  40.         var $list = $("ul", $(this).next());
  41.         //now remove the original item...
  42.         $item.remove();
  43.         //...and add the item to its new parent :)
  44.         //NB simply appending $item didn't work
  45.         $("<li></li>").text(ui.draggable.text()).appendTo($list);
  46.         //new accordion structure = new size
  47.         $catalog.accordion("resize");
  48.         //and trigger the accordion to expand on the correct category
  49.         $(this).click();
  50.  
  51.         event.preventDefault();
  52.         return false;
  53.     }
  54. });
  55.        
  56. $( "#cart li" ).droppable({
  57.     drop: function( event, ui ) {
  58.         $( this ).siblings( ".placeholder" ).remove();
  59.         $( "<li></li>" ).text( ui.draggable.text() ).insertAfter( this );
  60.     }
  61. });