Search code examples
jqueryjquery-uijquery-pluginsnested-sortable

jQuery nestedSortable: Prevent items from being nested


I am using mjsarfattis nestedSortable Plugin to build a hierachical data-structure. Is there a way to prevent items from being nested? An example:

phase 1
  group
    item
    item
    subgroup
      item
      item
      item
    item
  item
  item
  item
phase 2
  group
    item
    item
    subgroup
      item
    item
  item
  item

As you can see, (sub-)groups or items can be nested inside the phases but phases themselve should not be nested inside another phase or (sub-)group.

For this case I used the "mustBeRoot" property that handles this function: _isAllowed that had been modified a bit:

_isAllowed: function(parentItem, levels) {
  var o = this.options;
  // Are we trying to nest under a no-nest or are we nesting too deep?
  if (this.currentItem.hasClass(o.mustBeRoot) && this._getLevel(this.placeholder) != 0) {
    this.placeholder.removeClass(o.mustBeRoot).addClass(o.errorClass);
    if ( this._getLevel(this.placeholder) == 1) {
      this.placeholder.removeClass(o.errorClass).addClass(o.mustBeRoot);
    }
    this.beyondMaxLevels = 1;
  } else if (parentItem == null || !(parentItem.hasClass(o.disableNesting))) {
    if (o.maxLevels < levels && o.maxLevels != 0) {
      this.placeholder.addClass(o.errorClass);
      this.beyondMaxLevels = levels - o.maxLevels;
    } else {
      this.placeholder.removeClass(o.errorClass);
      this.beyondMaxLevels = 0;
    }
  } else {
    this.placeholder.addClass(o.errorClass);
    if (o.maxLevels < levels && o.maxLevels != 0) {
      this.beyondMaxLevels = levels - o.maxLevels;
    } else {
      this.beyondMaxLevels = 1;
    }
  }
},

Ok, this works fine if I try to drop the phase inside another phase, but if I drag it a little deeper (i.e. inside a subgroup) and drop it, it will only "climb" one level up. then I have one phase in another :-/

Phases should be root-elements only! I hope u have any ideas.


Solution

  • In the documentation there is an error, instead of disableNesting should be disableNestingClass. Hope that helps.