最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change the settings of Sortablejs after its set - Stack Overflow

programmeradmin1浏览0评论

I am having trouble to change my sortablejs settings after its set.
The initialization of this function is done within an other script(this script may not change) and is done when the page is loaded.
what i have for code in other script is the following:

//initialize sortable
function initSortable() {
   $(".menu-item-list").each(function (index) {
      var id = this.id;
      var options = {
         animation: 150,
         group: "menu-item-list",
         chosenClass: "sortable-chosen",
         ghostClass: "sortable-ghost",
         filter: ".empty-area-text",
         cancel: ".empty-area-text",
         onAdd: function (e) {
            //moved to the new column. save the items position
            saveItemsPosition();
            removeEmptyAreaText(e.to);
            addEmptyAreaText(e.from);
            adjustHeightOfItemsContainer();
            removeSubmenuClass(e.item, e.to);
         },
         onUpdate: function (e) {
            //moved to the same column. save the items position
            saveItemsPosition();
            adjustHeightOfItemsContainer();
            removeSubmenuClass("", "");
         }
      };
      Sortable.create($("#" + id)[0], options);
   });
}

I am having trouble to change my sortablejs settings after its set.
The initialization of this function is done within an other script(this script may not change) and is done when the page is loaded.
what i have for code in other script is the following:

//initialize sortable
function initSortable() {
   $(".menu-item-list").each(function (index) {
      var id = this.id;
      var options = {
         animation: 150,
         group: "menu-item-list",
         chosenClass: "sortable-chosen",
         ghostClass: "sortable-ghost",
         filter: ".empty-area-text",
         cancel: ".empty-area-text",
         onAdd: function (e) {
            //moved to the new column. save the items position
            saveItemsPosition();
            removeEmptyAreaText(e.to);
            addEmptyAreaText(e.from);
            adjustHeightOfItemsContainer();
            removeSubmenuClass(e.item, e.to);
         },
         onUpdate: function (e) {
            //moved to the same column. save the items position
            saveItemsPosition();
            adjustHeightOfItemsContainer();
            removeSubmenuClass("", "");
         }
      };
      Sortable.create($("#" + id)[0], options);
   });
}
Share Improve this question asked Mar 13 at 8:55 oelimoeoelimoe 4212 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To make it so that you can change the settings is by using

setTimeout(() => { // timeout is important
   $(".menu-item-list").each(function() {
      let instance = Sortable.get(this); // Get existing Sortable instance
      if (instance) {
         // Apply the updated filter setting
         instance.option("multiDrag", true);
         instance.option("selectedClass", "selected");
         instance.option("fallbackTolerance", 3);
      }
   });
}, 500);
发布评论

评论列表(0)

  1. 暂无评论