JQuery sortable lists and fixed/locked items

前端 未结 9 1583
故里飘歌
故里飘歌 2020-11-28 06:07

Is it possible to lock list items in JQuery sortable list in a way that those items will stay in that particular place in the list.

For example,

consider th

9条回答
  •  清歌不尽
    2020-11-28 06:39

    This is based on @DarthJDG code. However it wasn't retrieving all the id's and the sorting wasn't working with the table. So I managed to update his solution which works with both list and tables and keeps the id in the array.

    Javascript:

    var fixed = '.static'; //class which will be locked
    var items = 'li'; //tags that will be sorted
    
    $('ul').sortable({
      cancel: fixed,
      items: items,
      start: function () {
        $(fixed, this).each(function () {
          var $this = $(this);
          $this.data('pos', $this.index());
        });
      },
      change: function () {
        var $sortable = $(this);
        var $statics = $(fixed, this).detach();
        var tagName = $statics.prop('tagName');
        var $helper = $('<'+tagName+'/>').prependTo(this);
        $statics.each(function () {
          var $this = $(this);
          var target = $this.data('pos');
          $this.insertAfter($(items, $sortable).eq(target));
        });
        $helper.remove();
      }
    });
    

    Demo: http://plnkr.co/edit/hMeIiRFT97e9FGk7hmbs

提交回复
热议问题