4

jquery code

 $("#products li").draggable({
    appendTo: "body",
    helper: "clone"
});
$(".shoppingCart ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
    }
}).sortable({
    items: "li:not(.placeholder)",
    sort: function() {
        $(this).removeClass("ui-state-default");
    }
});

and this is html code

<div id="products">
    <h1 class="ui-widget-header">Blocks</h1>
    <div class="ui-widget-content">
        <ul>
            <li data-id="1"> 10000$ </li>
            <li data-id="2"> -10000$ </li>
            <li data-id="3"> 10000$ </li>
            <li data-id="4"> -10000$ </li>
            <li data-id="5"> Bank </li>
            <li data-id="6"> Loan </li>
        </ul>
    </div>
</div>

<div id="shoppingCart1" class="shoppingCart">
    <h1 class="ui-widget-header">Cresit Side</h1>
    <div class="ui-widget-content">
        <ol>
            <li class="placeholder">Add your items here</li>
        </ol>
    </div>
</div>
<div id="shoppingCart2" class="shoppingCart">
    <h1 class="ui-widget-header">Debit side</h1>
    <div class="ui-widget-content">
        <ol>
            <li class="placeholder">Add your items here</li>
        </ol>
    </div>
</div>

Now i want to know how can i drop any element in specific container like in credit side and debit side.

my demo is here http://jsfiddle.net/Sanjayrathod/S4QgX/541/

now i want to drag and drop first three element is in credit side only and remaining elements in debit side.

So how can i do this. Please help.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sanjay Rathod
  • 1,083
  • 5
  • 18
  • 45
  • I couldn't understand what's wrong with your fiddle – Itay Sep 14 '13 at 11:15
  • You want to be able to drop given element only in a specific container? – Jarek Mazur Sep 14 '13 at 11:16
  • Nothing is wrong with my fiddle bro 6 elements in blocks can be drag and drop into crdit side blocks and debit side block. – Sanjay Rathod Sep 14 '13 at 11:17
  • no i can drag and drop elements into both side credit side and debit side but i want to drag and drop into specific contaner like first three elements intop credit side and remaining can be drag and drop into debit side – Sanjay Rathod Sep 14 '13 at 11:18

2 Answers2

3

Add specific class name in accept parameter of dropable area. Add the class credit or debit in the dragable element.

$("#shoppingCart1 ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept:".credit",  //for credit
    drop: function(event, ui) {


$("#shoppingCart2 ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept:".debit",  //for credit
    drop: function(event, ui) {

DEMO Here.

sudhansu63
  • 6,025
  • 4
  • 39
  • 52
1

If you want to restrict dropping elements to specific containers use 'accept' option of target droppable: http://jqueryui.com/droppable/#accepted-elements.

It determines elements that can be dropped via selector e.g.

 $( "#droppable" ).droppable({
      accept: "#canBeDroppedId",
      ...
 });
Jarek Mazur
  • 2,052
  • 1
  • 25
  • 41