动态启用/禁用jquery可排序项

ann*_*tte 13 jquery jquery-ui jquery-ui-sortable

我有可以排序的表行,具体取决于是否检查了某些单选按钮.可排序的初始化document.ready如下:

$(document).ready(function() {
    // Return a helper with preserved width of cells
    // handy code I got from http://lanitdev.wordpress.com/2009/07/23/make-table-rows-sortable-using-jquery-ui-sortable/
    var fixHelper = function(e, ui) {
        ui.children().each(function() {
            $(this).width($(this).width());
        });
        return ui;
    };
    // #opts = table id; tr.ui-state-disabled class = rows not sortable
    $("#opts tbody").sortable({
        items: 'tr:not(.ui-state-disabled)',
        cursor: 'crosshair',
        helper: fixHelper
    }).disableSelection();
});
Run Code Online (Sandbox Code Playgroud)

我有以下功能附加到单选按钮(ID为"active_")onchange,它们ui-state-disabled从表行添加或删除类属性(动态ID前缀为"opt_"):

    var toggleDrag = function(i){
    if ($('#active_'+i+'-0').is(':checked')) {
        $('#opt_'+i).addClass('ui-state-disabled');
    }
    if ($('#active_'+i+'-1').is(':checked')) {
        $('#opt_'+i).removeClass();
    }
    $("#opts tbody").sortable("option", "items", "tr:not(.ui-state-disabled)");
    //$("#opts tbody").sortable("refresh");
    //alert($('#opt_'+i).attr('class')); - alert indicates that the class attribute is being changed
    //$("#opts tbody").sortable("option", "cursor", "auto"); - this works!
}
Run Code Online (Sandbox Code Playgroud)

如果我选择一个单选按钮,该按钮应该使以前不可排序的行可排序,它可以工作,我可以拖放该行.问题是如果我选择一个单选按钮来创建一个以前可排序,不可排序的行,我仍然可以拖放它..sortable("option", "items", "tr:not..etc")如果以前可以排序,则setter 不会出现"取消注册".我也试过.sortable("刷新")没有运气.我已经检查过是否正在使用警报更改类属性.

任何有关这方面的帮助将不胜感激.

Han*_*ele 15

我遇到了同样的问题,该items选项似乎没有删除以前启用的项目.

cancel但是,该选项确实如此.请注意,禁用的项目将移动以为可排序的项目腾出空间(该点仍然可用作放置目标),但拖动禁用的项目本身将不起作用.使用disabled类还可以根据项目是否可排序来轻松更改样式(请参阅jsfiddle).

这里的代码部分基于Lamb Bah的答案,但它已经大大整理和简化了.

html:

<ul id="sorted-list">
   <li>
       <p><input type="checkbox" checked="true" /> Item 1</p>
    </li>
   <li>
       <p class="disabled"><input type="checkbox" /> Item 2</p>
   </li>
   <li>
       <p><input type="checkbox" checked="true" /> Item 3</p>
   </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

jQuery:

$("#sorted-list").sortable({
    cancel:".disabled"
});

// add or remove the 'disabled' class based on the value of the checkbox
$("#sorted-list input").click(function() {
    if (this.checked) {
        $(this.parentElement).removeClass("disabled");
    } else { 
        $(this.parentElement).addClass("disabled");
    }
});
Run Code Online (Sandbox Code Playgroud)

CSS:

li {
  border: 1px solid #aaa;
  background-color: #eee;
  color:#555;
  padding: 5px;
}

.disabled {
  color:#ddd;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我有一个类似的问题,并找到了一个简单的解决方案(但在我的例子中,我使用的是可排序的ul而不是表格,因为在ul中有更多的自由标记).

它背后的想法是让列表及其所有项目可排序,但删除句柄,从而禁用单个项目的排序能力,如此代码中所示

<ul id="sorted-list">
    <li>
        <div class="handle sortable">
            <p>Item 1</p>
            <input type="button" value="Disable Sort" />
        </div>
    </li>
    <li>
        <div class="handle sortable">
            <p>Item 2</p>
            <input type="button" value="Disable Sort" />
        </div>
    </li>
    <li>
        <div class="handle sortable">
            <p>Item 3</p>
            <input type="button" value="Disable Sort" />
        </div>
    </li>
    <li>
        <div class="handle sortable">
            <p>Item 4</p>
            <input type="button" value="Disable Sort" />
        </div>
    </li>
</ul>
<script>
    $("#sorted-list").sortable({
        items: "li",
        handle: ".handle.sortable"
    });
    $("#sorted-list").find("input").click(function() {
        if ($(this).closest(".handle").hasClass("sortable"))
            $(this).val("Enable Sort").closest(".handle").removeClass("sortable");
        else $(this).val("Disable Sort").closest(".handle").addClass("sortable");
    });
</script>
Run Code Online (Sandbox Code Playgroud)