2015-04-12 85 views
0

我有一个可排序的列表,在这里和那里进行一些调整。可排序克隆元素而不是排序

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

不幸的是,它并没有在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 
     }); 
    }); 

是有办法确定项目是否源自可排序本身? 我想添加的项目,如果我拉他们从外面,但如果我对它们重新排序列表中

UPDATE: (也许)重要信息: 第一个排序工作。 每样后拷贝该项目

谢谢:)

SOLUTION:

我有一个的setInterval集重新加载播放列表每5秒。 解释一切: 第一个或两个排序工作得很好,然后列表重新加载,当我排序然后,项目是从“外部”考虑和补充。 我删除了间隔,它的作品:)

回答

1

只是删除helper: "clone"选项,并且一切都会好:)这个帮手克隆一个项目。

+0

没有工作...或更好:它似乎工作,但然后克隆再次出现:/ ...这可能是由外部的东西引起的?代码中完全在其他地方的东西? –

+0

为了使它在小提琴上工作,您只需添加一个jQuery UI库(小提琴上的外部资源选项卡) –

+0

愚蠢的我(或聪明的我?:D)我只是自己发现了它。在代码中确实是一个完全不同的地方,gonne更新了这个问题。谢谢:) –