te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I disable sort on a specific element? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I disable sort on a specific element? - Stack Overflow

programmeradmin4浏览0评论

I'm having difficulty disabling sorting on a specific li in my ul. I'm using 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>

One li with class static should NOT be sortable. The others should be sortable.

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

I know you can do it in jQuery UI sortable like this:

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

How can I do this in SortableJS?

I'm having difficulty disabling sorting on a specific li in my ul. I'm using 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>

One li with class static should NOT be sortable. The others should be sortable.

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

I know you can do it in jQuery UI sortable like this:

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

How can I do this in SortableJS?

Share Improve this question edited Jan 11, 2024 at 20:50 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Feb 10, 2016 at 13:31 nielsvnielsv 6,82035 gold badges117 silver badges219 bronze badges 4
  • So you are or you are not using jQuery UI sortable? – epascarello Commented Feb 10, 2016 at 13:36
  • @epascarello, no I'm sorry. I removed the tag. – nielsv Commented Feb 10, 2016 at 13:45
  • what happens when you try it? can we have a jsfiddle? – phenxd Commented Feb 10, 2016 at 13:57
  • its the filter option = filter: '.static', – BenG Commented Feb 10, 2016 at 13:58
Add a ment  | 

2 Answers 2

Reset to default 11

Further to @BenG ment, you need to use filter instead of cancel.

var el = document.getElementById('items');
var sortable = Sortable.create(el, {
   filter: ".static"
});
<script src="//cdnjs.cloudflare./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>

You need to add two options when creating the sortable instance:

    var container = document.getElementById('my-list');
    var sortable = Sortable.create(container, {
       // 1.) Exclude all elements having class '.static' from being draggable.
       draggable: 'li:not(.static)',
       // 2.) Prevent all elements having class '.static' from being moved.
       onMove(e) {
         return e.related.className.indexOf('static') === -1;          
       }
    });

Why should I not use filter option?

Looking at the accepted answer, it seems like the filter option would do the job. But whenever you use multiple instances with interchangeable elements, it bees problematic if one of the lists get empty. Suddenly you won't be able to add elements to that list again. This is due to Sortable's internal logic, which still treats the .static element as a draggable element. Even setting the emptyInsertThreshold option to be really high won't fix that, because for Sortable, the list is not empty.

Example 1 using filter option: (Try to add 'Apple' to COMPANIES - it won't work!)

var el = document.getElementById('items');
var sortable = Sortable.create(el, {
   draggable: 'li:not(.static)',
   group: {
     name: 'list'
   },
   onMove(e) {
     return e.related.className.indexOf('static') === -1;          
   }
});
var items2 = document.getElementById('items2');
var sortable = Sortable.create(items2, {
   filter: '.static',
   group: {
     name: 'list'
   },
   onMove(e) {
     return e.related.className.indexOf('static') === -1;          
   }
});
<script src="//cdnjs.cloudflare./ajax/libs/Sortable/1.14.0/Sortable.min.js"></script>
<ul id="items">
    <li class="static">FRUITS</li>
    <li>Strawberry</li>
    <li>Banana</li>
    <li>Peach</li>
    <li>Apple</li>
</ul><ul id="items2">
    <li class="static">COMPANIES</li>
</ul>

Example 2 using draggable option: (Try to add 'Apple' to COMPANIES - it works!)

var el = document.getElementById('items');
var sortable = Sortable.create(el, {
   draggable: 'li:not(.static)',
   group: {
     name: 'list'
   },
   onMove(e) {
     return e.related.className.indexOf('static') === -1;          
   }
});
var items2 = document.getElementById('items2');
var sortable = Sortable.create(items2, {
   draggable: 'li:not(.static)',
   group: {
     name: 'list'
   },
   onMove(e) {
     return e.related.className.indexOf('static') === -1;          
   }
});
<script src="//cdnjs.cloudflare./ajax/libs/Sortable/1.14.0/Sortable.min.js"></script>
<ul id="items">
    <li class="static">FRUITS</li>
    <li>Strawberry</li>
    <li>Banana</li>
    <li>Peach</li>
    <li>Apple</li>
</ul><ul id="items2">
    <li class="static">COMPANIES</li>
</ul>

发布评论

评论列表(0)

  1. 暂无评论