Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A little late maybe, but I think this is a better solution... Will post for documentation/search purposes in case other people find this useful</p> <p>Note: you also need jQuery UI's Position element loaded in your UI build for this solution to work. </p> <p>In my example, I'm creating a set of draggable "coupons" which can be placed in various positions. Only one coupon can sit in any droppable area at a given time. </p> <p>In order to manage coupon assignment to droppable areas, I created some helper objects which I will use to extend the options object of the draggable &amp; droppable prototypes:</p> <pre><code>var dropObj = { hasCoupon: false, couponId: null } var couponObj = { dropId: null } </code></pre> <p>Then (on DOM ready) extend:</p> <pre><code>$.extend( $.ui.droppable.prototype.options, dropObj ); $.extend( $.ui.draggable.prototype.options, couponObj ); </code></pre> <p>Finally, setup your draggables &amp; droppables... When you're setting up the droppable use jquery UI's position helper to assist in swapping the draggables...</p> <pre><code>$('.selector').droppable({ drop: function( event, ui ) { var current, swapId; if ( $(this).droppable('option', 'hasCoupon') === false ) { // ... } else { current = $(this).droppable('option', 'couponId'); swapId = ui.draggable.draggable('option', 'dropId'); if ( swapId !== null ){ current.position({ my: "center", at: "center", of: swapId, using: function(to) { $(this).animate({ top: to.top, left: to.left }, ui.draggable.revertDuration, function() { swapId.droppable('option', 'hasCoupon', true); swapId.droppable('option', 'couponId', current); current.draggable('option', 'dropId', swapId); }); } }); } } $(this).droppable('option', 'hasCoupon', true); $(this).droppable('option', 'couponId', ui.draggable); ui.draggable.draggable('option', 'dropId', $(this)); ui.draggable.position({ my: "center", at: "center", of: $(this), using: function(to) { $(this).animate({ top: to.top, left: to.left }, 150 ); } }); } }); </code></pre> <p>Note: We're manually setting the values for couponId/dropId in the animation callback for the swap, since drag events are not actually firing in that situation (movement is handled via a jquery animation call instead).</p> <p>I played around with using .trigger() on dragstart/dragstop originally, but they didn't trigger the drop event as I assumed they should.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload