【问题标题】:sortable clones element instead of sorting可排序克隆元素而不是排序
【发布时间】:2015-04-12 19:26:35
【问题描述】:

我有一个可排序的列表,在这里和那里进行了一些调整。

但最近,当我尝试对它们进行排序时,列表项会重复。 (每次我拉一个,另一个副本被添加)。 我不知道为什么。

很遗憾,它没有在Fiddle 中运行

代码如下:

$(function() {
    /**
     * determines if an item is to be removed from the list
     */
    var removeItem;
    $("#tracks").sortable({
        items: "li:not(.placeholder)",
        connectWith: "li",
        placeholder: "sort_placeholder",
        helper: "clone",
        distance: 20,
        sort: function () {
            $(this).removeClass("ui-state-default");
            updatePlaylist();
        },
        over: function (event,ui) {
            updatePlaylist();
            removeItem = false;
            //gets the class of the original item to add to the clipinfo-field
            var originalClass = ui.helper.context.childNodes[0].className;
            var small_slot = originalClass.match(/(\d+)/g)[0];
            var small_clip = originalClass.match(/(\d+)/g)[1];
            // shows the original index in the playlist
            ui.item.context.children[0].innerHTML = small_clip;
            ui.item.context.children[0].classList.add("slot_clip_info");
        },
        out: function () {
            updatePlaylist();
            // if an item is pulled out of the list, it gets marked for deletion
            removeItem = true;
        },
        beforeStop: function(event,ui) {
            // if an item has been pulled out of the list, remove it
            if (removeItem) {
                ui.item.remove();
            }
        },
        stop: function(event,ui) {

            var list = $('#tracks');
            var count = list.children(':not(.placeholder)').length;
            list.children('.placeholder').css("display", count > 0 ? "none" : "block");
            list.children(':not(.placeholder)').each(function() {
                $(this).removeClass().attr('style', '');  // removes the automatically added styles (width etc)
                $(this).addClass("ui-state-default pl_clipEntry");
            })

            // after every update, save the playlist

            savePlaylist();
        }
    });
});


function updatePlaylist () {
var list = $("#tracks");
var count = list.children(':not(.placeholder)').length;
list.children('.placeholder').css("display", count > 0 ? "none" : "block");

list.children(':not(.placeholder)').each(function(index,elem) {
    var button = $(elem).children().eq(1).children().eq(0);
    $(button).attr('onclick', '').unbind('click');
    $(button).click(function(){emitCommand('playlist_clip ' + index);});
})
}

以及项目来自的可拖动对象:

$(function() {
        $(".pl_clipEntry").draggable({
            appendTo: "body",
            revert: "invalid",
            connectToSortable: "#tracks",
            distance: 20,
            helper: function(){
                return $(this).clone().width($('#placeholder').width());   // for the drag-clone to keep the correct width
            },
            stop: function(ui, event) {
                $($(ui).children("li")[0]).addClass(".slot_clip_info");
             },
            zIndex: 100
        });
    });

有没有办法确定一个项目是否来自可排序本身? 我希望添加项目,如果我从外部拉入它们,但如果我在列表中重新排序它们则不需要

更新: (也许)重要信息: 第一种有效。 之后的每一个排序都会复制该项目

谢谢:)

解决方案:

我有一个 setInterval 设置为每 5 秒重新加载播放列表。 这解释了一切: 第一个或两个排序工作得很好,然后重新加载列表,然后当我排序时,这些项目被认为是“从外部”并添加的。 我删除了间隔并且它有效:)

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-sortable


    【解决方案1】:

    只需删除 helper: "clone" 选项,一切都会好起来的 :) 这个助手克隆了一个项目。

    【讨论】:

    • 没有工作......或者更好:它似乎工作,但随后克隆再次出现:/......这可能是由外部原因引起的吗?代码中其他地方的东西?
    • 要让它在 fiddle 上工作,你只需要添加一个 jQuery UI 库(Fiddle 上的外部资源选项卡)
    • 愚蠢的我(还是聪明的我?:D)我自己才发现。它确实是代码中完全不同的地方,将更新问题。谢谢:)
    • 恭喜 :) 我还在var originalClass = ui.helper.context.childNodes[0].className; 上发现了一个错误——小提琴上的控制台说originalClass 为空(也许它只是在小提琴上),所以我修复了它:var originalClass = $($(ui.helper.context).children('span')[0]).attr('class');跨度>
    猜你喜欢
    • 2013-11-26
    • 2010-11-19
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多