【问题标题】:SortableJS - Disable sort on a specific elementSortableJS - 禁用对特定元素的排序
【发布时间】:2016-05-20 21:22:33
【问题描述】:

我无法在我的 ul 中禁用对特定 li 的排序。 我正在使用SortableJS

<ul id="items">
    <li class="static">
        <div class="image"><img src="image.jpg" /></div>
        <div class="text">Static</div>
        <div class="clearboth"></div>
    </li>
    <li>
        <div class="image"><img src="image.jpg" /></div>
        <div class="text">Dynamic</div>
        <div class="clearboth"></div>
    </li>
    <li>
        <div class="image"><img src="image.jpg" /></div>
        <div class="text">Dynamic</div>
        <div class="clearboth"></div>
    </li>
</ul>

staticli 不应该是可排序的。其他的应该是可排序的。

var el = document.getElementById('items');

var sortable = new Sortable(el, {
    onUpdate: function (evt) {
        var itemEl = evt.item;

        // here happens some stuff
    },
    filter: '.js-remove',
    onFilter: function (evt) {
        // here happens some stuff
    }
});

know 你可以在jQuery UI sortable 这样做:

$( ".sortable" ).sortable({
    cancel: ".static"
});

如何在 SortableJS 中做到这一点?

【问题讨论】:

  • 那么您是使用还是未使用可排序的 jQuery UI?
  • @epascarello,不,对不起。我删除了标签。
  • 当你尝试它时会发生什么?我们可以有一个 jsfiddle 吗?
  • 它的 filter 选项 = filter: '.static',

标签: javascript jquery sortablejs


【解决方案1】:

除了@BenG 评论,您需要使用filter 而不是cancel

var el = document.getElementById('items');
var sortable = Sortable.create(el, {
   filter: ".static"
});
<script src="//cdnjs.cloudflare.com/ajax/libs/Sortable/1.4.2/Sortable.min.js"></script>
<link href="http://rubaxa.github.io/Sortable/st/app.css" rel="stylesheet" />
<ul id="items" class="block__list block__list_words">
    <li>item 1</li>
    <li class="static">item 2</li>
    <li>item 3</li>
</ul>

【讨论】:

  • filter 选项禁用拖动。但是仍然可以通过在其上放置不同的元素来重新排列该元素。禁用丢弃。你需要the technique 来检查related 事件处理程序中的related 属性:onMove: function (e) { return e.related.className.indexOf('static') === -1; } (这个答案确实满足了最初的问题,但只是为了一个密切相关的问题增加一点。为了后代。)
猜你喜欢
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多