简体   繁体   中英

Accordion Containment not working properly

I had build a parent and child system using accordion. The structure is something like below

parent1
  child1
  child2
  child3
parent2
  child4
  child5
  child6
parent3
  child7
  child8
  child9

I have also added one feature of drag and drop. My requirement is I don't want a child should be drag out from the current parent. It can be only dragged and replace in the current parent. For eg child 1,2 & 3 position can be interchanged with drag but child 2 should not be allowed to go inside parent2 & parent3. Also, the parent2 child shouldn't be allowed to drag in parent1 and parent2

I did a research and I found accordion provides a property containment: parent But it doesn't work properly. Can anyone help me out with this problem?

<div id="available_fields_container" class="accordion list_container ui-sortable active" data-accordion="one">
  <dd class="accordion-navigation active" id="data_download_fields">
    <a href="#data_download_fields">
      <b>FootBall Fields</b>
      <span class="collapse-symbol"></span>
    </a>
    <span class="select-group" data-fields="#data_download_fields">Select all</span>
    <div id="data_download_fields" class="content active" data-group="FootBall Fields">

      <div class="field_items"  data-index="4" data-group="FootBall Fields">
        Andrew
        <span class="plus-icon action-icon">+</span>
        <i class="icon-cross2 action-icon"></i>
      </div>
      <div class="field_items"  data-index="5" data-group="FootBall Fields">
        Sam
        <span class="plus-icon action-icon">+</span>
        <i class="icon-cross2 action-icon"></i>
      </div>

    </div>
  </dd>

    <dd class="accordion-navigation " id="data_download_hockey_fields">
      <a href="#data_download_hockey_fields">
        <b>hockey Fields</b>
        <span class="collapse-symbol"></span>
      </a>
      <span class="select-group" data-fields="#data_download_hockey_fields">Select all</span>
      <div id="data_download_hockey_fields" class="content " data-group="hockey Fields">
          <div class="field_items"  data-index="0" data-group="hockey Fields">
  jason
    <span class="plus-icon action-icon">+</span>
    <i class="icon-cross2 action-icon"></i>
  </div>
  <div class="field_items"  data-index="1" data-group="hockey Fields">
  Jane
    <span class="plus-icon action-icon">+</span>
    <i class="icon-cross2 action-icon"></i>
  </div>

      </div>
    </dd>

</div>

For achieving these type of things you can use jquery sortable callbacks. In your case, you will need start and out callback

var before_drag;

start: function(event, ui){
  before_drag = ui.item.parent().attr('id')
}

out: function(event, ui){
  var after_drag = ui.item.parent().attr('id')
        if(before_drag != after_drag){
          event.preventDefault();
          event.stopPropagation();
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM