jquery-ui - jquery ui sortables 连接列表 : copy items

标签 jquery-ui jquery-ui-sortable

我有两个列表,我希望它们都可以排序,并且希望能够将项目从 list1 复制(拖动)到 list2,反之亦然。

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

是我想要的,但项目被移动,而不是复制。
我对draggables 和droppables 做了一些实验,但我无法开始工作以保持它们的可排序性。例如:http://jsfiddle.net/tunafish/dvXmf/

最佳答案

好的,这是我的应用程序;两个图像列表,可排序,您可以从连接的列表中复制。
如果一个项目已经存在于目标中,它就会被禁用。
希望对某人有用...

JSFF 在这里:http://jsfiddle.net/tunafish/VBG5V/

CSS:

.page { width: 410px; padding: 20px; margin: 0 auto; background: darkgray; }
.album { list-style: none; overflow: hidden; 
    width: 410px; margin: 0; padding: 0; padding-top: 5px;
    background: gray; 
} 
.listing { margin-bottom: 10px; }
.album li { float: left; outline: none;
    width: 120px; height: 80px; margin: 0 0 5px 5px; padding: 5px;
    background: #222222;
 }
li.placeholder { background: lightgray; }

JS:
$("ul, li").disableSelection();

$(".album, .favorites").sortable({
    connectWith: ".album, .favorites",
    placeholder: "placeholder", 
    forcePlaceholderSize: true, 
    revert: 300,
    helper: "clone",
    stop: uiStop,
    receive: uiReceive,
    over: uiOver
});


$(".album li").mousedown(mStart);

var iSender, iTarget, iIndex, iId, iSrc, iCopy;
var overCount = 0;

/* everything starts here */
function mStart() {
    // remove any remaining .copy classes
    $(iSender + " li").removeClass("copy");

    // set vars
    if ($(this).parent().hasClass("listing")) { iSender = ".listing"; iTarget = ".favorites"; } 
    else { iSender = ".favorites"; iTarget = ".listing"; }
    iIndex  = $(this).index();
    iId     = $(this).attr("id");
    iSrc    = $(this).find("img").attr("src");  
    iCopy   = $(iTarget + " li img[src*='" + iSrc + "']").length > 0; // boolean, true if there is allready a copy in the target list   

    // disable target if item is allready in there  
    if (iCopy) { $(iTarget).css("opacity","0.5").sortable("disable"); }
}

/* when sorting has stopped */
function uiStop(event, ui) {
    // enable target
    $(iTarget).css("opacity","1.0").sortable("enable");

    // reset item vars
    iSender = iTarget = iIndex = iId = iSrc = iCopy = undefined;
    overCount = 0;

    // reinit mousedown, live() did not work to disable
    $(".album li").mousedown(mStart);
}

/* rolling over the receiver - over, out, over etc. */
function uiOver(event, ui) {
    // only if item in not allready in the target
    if (!iCopy) {                   
        // counter for over/out (numbers even/uneven)
        overCount++;
        // if even [over], clone to original index in sender, show and fadein (sortables hides it)
        if (overCount%2) {
            if (iIndex == 0) { ui.item.clone().addClass("copy").attr("id", iId).prependTo(iSender).fadeIn("slow"); } 
            else { ui.item.clone().addClass("copy").attr("id", iId).insertAfter(iSender + " li:eq(" + iIndex + ")").fadeIn("slow"); }
        } 
        // else uneven [out], remove copies
        else { $(iSender + " li.copy").remove(); }
    } 
    // else whoooooo
}

/* list transfers, fix ID's here */
function uiReceive(event, ui) {
    (iTarget == ".favorites") ? liPrefix = "fli-" : liPrefix = "lli-";  
    // set ID with index for each matched element
    $(iTarget + " li").each(function(index) {
        $(this).attr("id", liPrefix + (index + 1)); // id's start from 1
    });
}

HTML:
<div class="page">

    <div class="container">
        <h2>Photo Album</h2>
        <ul class="listing album">
            <li id="li-1"><img src="tn/001.jpg" /></li>
            <li id="li-2"><img src="tn/002.jpg" /></li>
            <li id="li-3"><img src="tn/003.jpg" /></li>
            <li id="li-4"><img src="tn/004.jpg" /></li>
            <li id="li-5"><img src="tn/005.jpg" /></li>
        </ul>
    </div>

    <div style="clear:both;"></div>

    <div class="container">
        <h2>Favorites</h2>
        <ul class="favorites album">
            <li id="fli-1"><img src="tn/001.jpg" /></li>
            <li id="fli-2"><img src="tn/002.jpg" /></li>
            <li id="fli-3"><img src="tn/010.jpg" /></li>
        </ul>
    </div>

</div>

关于jquery-ui - jquery ui sortables 连接列表 : copy items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3916089/

相关文章:

javascript - 在将 Draggable 放到贪婪的 Droppable 上并再次拖动后,droppable 的 Over 事件无法正常工作

jquery-ui - YUI3 和 socket.io

css - IE8 - 隐藏 CSS 溢出,修复导致 jquery 的进一步问题

Jquery UI 可排序的索引更改重新排序列表

javascript - 根据数组排序

javascript - jQuery 对话框不适用于 Datepicker

php - 如何在使用 Jquery sortable 排序后检索父容器 ID?

jquery - 使用 jQuery UI .sortable 的序列化方法获取排序元素的 html 作为值

javascript - updatepanel (JavaScript) 回发后,jQuery 可排序不起作用

jquery - 使用 jQuery Sortables 拖动项目时单选按钮被取消选择