1

I'm new to jQueryUI and I'm not able to drag multiple <li> elements to the drop area. However I managed to drag one and drop it on the drop area. Please can anyone help me with this.

JavaScript

$(function() {
    $trash= $( "#trash" );

    $("a", ".polaroids").draggable({
     zIndex: 999,
        revert: "invalid" ,
        helper: function(){
            $copy = $(this).clone();
            return $copy;},
        appendTo: 'body',
        scroll: false
    });

    $("a", ".polaroids").selectable();
        $trash.droppable({
        accept: ".polaroids a",
        activeClass: "custom-state-active",
        drop: function( event, ui ) {
            $(this).append(ui.draggable);
            }
    });
});

Here is the <div> in which the <li> elements are dragged but one by one

<div class="st_view_container">
    <div class="st_view">
    <?php
        foreach($gpID as $k => $v) {
    ?>
        <div id="stv_content_<?php echo $v;?>" class="st_tab_view st_first_tab_view">
            <ul class="polaroids" id ="polaroids">
            <?php
                $sql2=mysql_query("select * from user_group WHERE group_id='$v' AND user_id=3");
                $i=1;
                while($row=mysql_fetch_array($sql2)) {
                    $memid=$row['member_id'];
                    $sql1=mysql_query("select * from users_profile WHERE uid='$memid'");

                    while($row1=mysql_fetch_array($sql1)) {
                        $ufname=$row1['fname'];
                        $ulname=$row1['lname'];
                        $upic=$row1['profile_pic'];
            ?>
                <li>
                    <a href="#" title="<?php echo $ufname; ?>">
                        <img src="../<?php echo $upic; ?>" rel="<?php echo $row1['uid']?>" width="65px" height="65px" />
                    </a>
                </li>
                <?php
                        if($i%6==0) {;}
                        $i++;
                    }
                ?>
            </ul>
        </div>
    <?php } ?>
    </div> <!-- /.st_view -->
</div> <!-- /.st_view_container -->

and here is the <div> in which i want the multiple elements to be dropped, but not one by one.

<div id="trash" style="width:200px; height:200px; border:1px solid;">
    <h4 class="ui-widget-header"><span class="ui-icon ui-icon-trash">Trash</span> Trash</h4>
</div>
Community
  • 1
  • 1
Sakshi Sharma
  • 1,414
  • 4
  • 20
  • 39
  • http://stackoverflow.com/questions/793559/grouping-draggable-objects-with-jquery-ui-draggable might help – andyb Oct 28 '11 at 06:59
  • I tidied up your question. Hope I didn't break any PHP in the process. Please let me know if it's broken and I will correct. – andyb Oct 28 '11 at 07:16
  • gey frnd thanks for that but the post u linked me to is a very complicated process . can u plz help me out with simple one. as the post you linked me 2 the guy is using checkbox in each element to drag it . i just want to simple drag and drop multiple elements without chekbox – Sakshi Sharma Oct 28 '11 at 07:45
  • I could try and write another solution but I believe it has been answered on this site already. Have you seen http://stackoverflow.com/questions/1997454/is-it-possible-to-link-two-jquery-ui-draggables-together/2004620#2004620 (another solution to multiple drag)? – andyb Oct 28 '11 at 07:57
  • ha ha m so sorry i think i m troubling u a lot my friend . but this demo it moves the other elements automatically . when one is dragged the other also does automatically . i dnt want that . i want to drag as many of elements myself . thanks for helping so much friend . – Sakshi Sharma Oct 28 '11 at 08:21
  • How do you want to select the _multiple_ items? – andyb Oct 29 '11 at 09:10

1 Answers1

1

Here is a demo based on some research…

and me playing with the jQueryUI droppable photo manager demo which is what you are using as a template.

Functionality includes single click and drag (as is the default behaviour) or use Ctrl+left click to select multiple items and then drag. The drag helper function is used to select all the items with class="selected" and the drop function is customised to extract the img elements from the container the drag helper added them to. The other function simple enables the Ctrl+click behaviour.

The following code is duplicated below from the demo but does require jQuery, jQueryUI and one of the jQueryUI themes.

HTML

<ul id="draggable">
    <li><img src="nature-q-c-50-50-1.jpg" alt="" /></li>
    <li><img src="nature-q-c-50-50-2.jpg" alt="" /></li>
    <li><img src="nature-q-c-50-50-3.jpg" alt="" /></li>
    <li><img src="nature-q-c-50-50-4.jpg" alt="" /></li>
    <li><img src="nature-q-c-50-50-5.jpg" alt="" /></li>
    <li><img src="nature-q-c-50-50-6.jpg" alt="" /></li>
</ul>

<div id="trash">
    <h4 class="ui-widget-header">Trash<span class="ui-icon ui-icon-trash"></span></h4>
</div>

CSS

body {
    font-family:"Trebuchet MS";
}
#draggable {
    margin:0;
    padding:10px;
    width:300px;
    list-style-type:none;
    background-color:#000;
}
li {
    display:inline;
}
img {
    border:5px solid white;
}
.image-group img {
    margin-right:5px;
}
#trash {
    margin-top:10px;
    width:200px;
    height:200px;
    border:1px dotted #000;
}
.selected {
    border-color:#aed0ea
}
#trash h4 {
    margin:0;
    padding:0 5px;
}
.ui-icon {
    display:inline-block;
}

JavaScript

$('#draggable li').draggable({
    revertDuration:100,
    helper:function() {
        var selected = $('#draggable img.selected');

        if (selected.length === 0) {
            selected = $('img', $(this)).addClass('selected');
        }
        console.log('selected', selected);
        var container = $('<div class="image-group"/>');
        container.append(selected.clone());
        console.log('container', container);
        return container;
    },
    cursorAt:{ left:25,top:25 }
});

$('#trash').droppable({
    drop: function(event, ui) {
        var newItems = $(ui.helper).find('img').clone(false)
                        .removeClass('selected');  

        $(this).append(newItems);
        console.log('ui.draggable', ui.draggable);
        $('#draggable img.selected').parent().remove();
    }
});

$('#draggable li').click(function(event) {
    if (event.ctrlKey) {
        $('img', $(this)).toggleClass('selected');
    }
});
Community
  • 1
  • 1
andyb
  • 43,435
  • 12
  • 121
  • 150
  • sorry to say friend i worked out a lot on this example but it still didnt worked out . and this var container = $('
    '); is not there even in the exmaple , thanks a lot for helping me out
    – Sakshi Sharma Nov 01 '11 at 14:52
  • i tried the demo . its working, i tried to implement with my code, but it didnt worked ,....and i found something in the demo var container = $('
    '); . this is written in the example but actually it is not there in the demo . i tried with firebug also , it is not there , if this var container = $('
    '); line gets solved . it would run in my code too . there is something wrong with this line of the demo.
    – Sakshi Sharma Nov 01 '11 at 16:04
  • i am stuck on this code of my final project from more then 1 week . – Sakshi Sharma Nov 01 '11 at 16:09
  • The container exists only when an image or multiple images are being dragged. You can see this in the Chrome inspector. The element is added as a child of the `
      ` but is then deleted by jQueryUI when the image is dropped. I don't have Firefox to test with right now so cannot confirm if Firebug actually shows the transient container.
    – andyb Nov 01 '11 at 16:34
  • so how can i make such div which can only exist when i drag something , i hear it first time. damn this code is so difficult – Sakshi Sharma Nov 01 '11 at 20:33
  • There is nothing special happening really. The helper callback function is part of jQueryUI draggable code and the container element is automatically deleted when the _drag_event is stopped. You can look at the [jQueryUI 1.8.16 source](http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js) to see all the internal magic :-) – andyb Nov 02 '11 at 00:36