[英]How do I stop an item being sortable in a jquery UI sortable list?
I have a list, some items with a class "sort": 我有一个列表,有些项目带有“ sort”类:
<ul id="items">
<li class="sort">ITEM 1 <a href="#" class="fix-position">Fix Position</a></li>
<li class="sort">ITEM 2 <a href="#" class="fix-position">Fix Position</a></li>
<li class="sort">ITEM 3 <a href="#" class="fix-position">Fix Position</a></li>
<li class="sort">ITEM 4 <a href="#" class="fix-position">Fix Position</a></li>
<li>ITEM 5</li>
<li>ITEM 6</li>
<li>ITEM 7</li>
<li>ITEM 8</li>
</ul>
I have then initialised the list as sortable for all items with the class "sort". 然后,我将所有类别为“ sort”的项目初始化为可排序的列表。
$("#items").sortable({
items: "li.sort"
});
This works fine, however, I want to be able to fix the position of the sortable items so that they can no longer be dragged. 这很好,但是,我希望能够固定可排序项目的位置,以便不再拖动它们。 I've started by adding a button to remove the class "sort" from the list item. 我首先添加了一个按钮,以从列表项中删除“排序”类。 This is the code for the button: 这是按钮的代码:
$("#items").on("click", ".fix-position", function(e){
e.preventDefault();
$(this).parent().removeClass("sort");
$(this).remove();
});
This does prevent the other items from using it as a drop target, but the item can still be dragged itself. 这确实阻止了其他项目将其用作放置目标,但是该项目仍可以自己拖动。 See JS FIDDLE: http://jsfiddle.net/3Js5u/1/ 参见JS FIDDLE: http : //jsfiddle.net/3Js5u/1/
I have also tried to refresh the list: 我也尝试过刷新列表:
$("#items").sortable("refresh");
but that does not seem to work. 但这似乎不起作用。
Only other option I can think of is to destroy the sortable list and recreate it like this: 我能想到的唯一其他选择是销毁可排序列表并像这样重新创建它:
$("#items").sortable( "destroy" );
$("#items").sortable({
items: "li.sort"
});
JS FIDDLE for that option: http://jsfiddle.net/3Js5u/3/ . 该选项的JS FIDDLE: http : //jsfiddle.net/3Js5u/3/ 。 Not sure if that's the best way to go though. 不确定这是否是最好的方法。 Any help appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。
Do the opposite and use cancel
option instead. 进行相反的操作,而是使用cancel
选项。
HTML: HTML:
<ul id="items">
<li>ITEM 1 <a href="#" class="fix-position">Fix Position</a></li>
<li>ITEM 2 <a href="#" class="fix-position">Fix Position</a></li>
<li>ITEM 3 <a href="#" class="fix-position">Fix Position</a></li>
<li>ITEM 4 <a href="#" class="fix-position">Fix Position</a></li>
<li class="static">ITEM 5</li>
<li class="static">ITEM 6</li>
<li class="static">ITEM 7</li>
<li class="static">ITEM 8</li>
</ul>
JavaScript: JavaScript的:
$("#items").sortable({
cancel: ".static"
});
$("#items").on("click", ".fix-position", function(e) {
$(this).parent().addClass("static").end().remove();
e.preventDefault();
});
DEMO: http://jsfiddle.net/3Js5u/2/ 演示: http : //jsfiddle.net/3Js5u/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.