2012-12-14 129 views
26

如何從排序列表中排除元素?例如,有一個類名爲'note'的元素,我不要想要可拖拽如何排除元素被拖到可排序列表中?

<ul class="sortable"> 

      <li id="item_3">Item 3</li> 
      <li id="item_4">Item 4</li> 
      <li id="item_5">Item 5</li> 

      <p class="note">This is a note only</p> 
    </ul> 

jQuery UI的排序,jQuery的not顯然是行不通的......

$(function() { 
    $(".sortable:not(.note)").sortable(
    }).disableSelection(); 
}); 

回答

35

您需要使用取消。

參見工作示例here

JS:

$(function() { 
    $('.sortable').sortable(); 
    $('.sortable').disableSelection(); 
    $('.sortable').sortable({ cancel: '.note' }); 
});​ 

由於@Zephyr指出的那樣,這使你可以重新安排拖動兄妹的位置,如果你想避免這種情況,使用owise1 approach

$('.sortable').sortable({ 
    items : ':not(.note)' 
}); 
+0

謝謝大家的幫助! – laukok

+0

@lauthiamkok沒有問題,我剛剛適應了我的答案你的具體問題,所以檢查出來。 :) – Trufa

+0

是的,我注意到你編輯了答案。感謝編輯! :-D – laukok

20

你可以使用可排序的「items」選項:

$(".sortable").sortable({ 
    items : 'li' 
}); 

example

或...

​​

example

+0

這也適用於''包含數據集的標籤:'$(「#table」)。sortable({items:「[data-real-line]」});'。 –

3

你可以明確地排除的項目:

$(".sortable").sortable({ 
    cancel: ".disable-sorting" 
}); 
相關問題