dynamically enable/disable jquery sortable item

前端 未结 5 1330
南旧
南旧 2021-02-07 15:31

I have table rows that are sortable depending on whether certain radio buttons are checked or not. The sortables are initialized on document.ready as follows:

5条回答
  •  你的背包
    2021-02-07 15:51

    I ran into the same problem, that the items option doesn't seem to remove items which had been previously enabled.

    The cancel option, however, does. Note that the disabled items will shift around to make room for the sortable ones (the spot is still available as a drop target), but dragging the disabled items themselves will not work. Using a disabled class also makes it easy to change the style based on whether or not the item is sortable (see on jsfiddle).

    The code here is partially based on Bah Bah the Lamb's answer, but it has been greatly tidied and simplified.

    The html:

    • Item 1

    • Item 2

    • Item 3

    The 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");
        }
    });
    

    The CSS:

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

提交回复
热议问题