A container that is sortable, droppable and dragga

2019-04-12 00:06发布

问题:

This question related to jQuery's UI.

Can anybody provide an example the does the following:

  1. Container A is sortable and droppable
  2. Any element can be dragged into Container A and then sorted.
  3. Any element can be dragged out of Container A and then simply become draggable around the page.
  4. Container A is also draggable around the page

This is what I have so far:

$(function() {
    $("#sortable").sortable().droppable();
    $("#sortable").disableSelection();
    $("#drop").droppable({
        accept: "#sortable li",
        drop: function (evt, ui) {
            var $destination = $(this); 
            ui.draggable.appendTo( $destination ); 

        }
    }).draggable();
});

I created a fiddle here: http://jsfiddle.net/eEJHb/1/

Problems that I'm running into: A) I can't get the sortable to drop into the container B) Once in the container, I need the element to be draggable back to the sortable list

Thanks for your help!

UPDATE ** Part of my problem lied in option "connectWith". It simply wasn't working. Instead, I used the option "connectToSortable" (as an option in the draggable) and I was able to get the sortable and draggable working together.

回答1:

I had the same problem until I find this

    $(function () {
        $("#timetable .items").sortable({
        	connectWith: "ul"   
        });
        
        $("ul[id^='available']").draggable({
            helper: "clone",
            connectToSortable: ".items"
        });
    });
<style type="text/css">
.bar {
    width: 100px;
    height: 20px;
    margin: 2px;
    border: 1px solid black;
    background-color: lightgreen;
    text-align: center;
    float: left;
    margin: 2px;
    padding: 2px;
    cursor: pointer;
    border-radius: 3px;
}

.list {
    background-color: lightblue;
    border: 1px solid gray;
}

.items .ui-selected {
    background: red;
    color: white;
    font-weight: bold;
}

.items {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 100px;
}

.items li {
    margin: 2px;
    padding: 2px;
    cursor: pointer;
    border-radius: 3px;
}

.weekday {
    float: left;
}

.availablelist {
    background-color: orange;
    display: inline;
}
</style>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://www.pureexample.com/js/lib/jquery.ui.touch-punch.min.js"></script>


<div style="float:left;width:500px;">
    <div>Available employee</div>
    <ul id="available1" class="items">
        <li class="list availablelist">Jhonny</li>
    </ul>
    <ul id="available2" class="items">    
        <li class="list availablelist">Tim</li>
    </ul>
    <ul id="available3" class="items">    
        <li class="list availablelist">George</li>
    </ul>
    <ul id="available4" class="items">    
        <li class="list availablelist">Melissa</li>
    </ul>
    <ul id="available5" class="items">    
        <li class="list availablelist">Alice</li>
    </ul>
</div>
<div style="clear:both"></div>
<div id="timetable" style="float:left;width:700px;">
    <div style="text-align:center">Time table</div>
    <div class="weekday">Monday
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Tuesday 
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Wednesday 
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Thursday 
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Friday
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Saturday
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
    <div class="weekday">Sunday
        <ul class="items">
            <li class="list">Jhonny</li>
            <li class="list">Tim</li>
            <li class="list">George</li>
            <li class="list">Melissa</li>
            <li class="list">Alice</li>
        </ul>
    </div>
</div>



回答2:

I would suggest you check out this documentation:

http://jqueryui.com/sortable/#connect-lists

http://jqueryui.com/draggable/#sortable

and the answers to these questions:

Getting jQuery sortable, droppable and draggable to work together

jQuery-UI Draggable and Sortable



回答3:

You can use the Interactions from jquery-ui. It provides dragable and sortable interactions.

Here are examples.