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

javascript - How to drag and drop multiple elements between different tabs - Stack Overflow

programmeradmin3浏览0评论

I am trying to drag and drop multiple elements between different tabs.

in this jsfiddle, When an item is being dragged, i want to drag all other checked items along with it, like Gmail does when you move several email from inbox to another folder.

I think it is necessary to use ui.helper but i don't have enough skill in query.

following is the code i'm currently working with:

$( "#sortable1, #sortable2" ).sortable().disableSelection();
var $tabs = $( "#tabs" ).tabs();
var $tab_items = $( "ul:first li", $tabs ).droppable({
  accept: ".connectedSortable li",
  hoverClass: "ui-state-hover",
  drop: function( event, ui ) {
    var $item = $( this );
    var $list = $( $item.find( "a" ).attr( "href" ) )
      .find( ".connectedSortable" );
    ui.draggable.hide( "slow", function() {
      $tabs.tabs( "option", "active", $tab_items.index( $item ) );
      $( this ).appendTo( $list ).show( "slow" );
    });
  }
});

I am trying to drag and drop multiple elements between different tabs.

in this jsfiddle, When an item is being dragged, i want to drag all other checked items along with it, like Gmail does when you move several email from inbox to another folder.

I think it is necessary to use ui.helper but i don't have enough skill in query.

following is the code i'm currently working with:

$( "#sortable1, #sortable2" ).sortable().disableSelection();
var $tabs = $( "#tabs" ).tabs();
var $tab_items = $( "ul:first li", $tabs ).droppable({
  accept: ".connectedSortable li",
  hoverClass: "ui-state-hover",
  drop: function( event, ui ) {
    var $item = $( this );
    var $list = $( $item.find( "a" ).attr( "href" ) )
      .find( ".connectedSortable" );
    ui.draggable.hide( "slow", function() {
      $tabs.tabs( "option", "active", $tab_items.index( $item ) );
      $( this ).appendTo( $list ).show( "slow" );
    });
  }
});
Share Improve this question edited Jun 30, 2014 at 8:23 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Apr 11, 2014 at 10:53 user3428975user3428975 511 silver badge4 bronze badges 1
  • Wondering whether the answer helped.. :) – T J Commented Nov 6, 2014 at 17:14
Add a ment  | 

1 Answer 1

Reset to default 6

After a lot of fiddling, I came up with the following based on my answer here

Basically we save the selected items using data(), Initialize the tabs as droppable() and append the selected items into the sortable on drop event.

$('.connectedSortable').on('click', 'input', function() {
  $(this).parent().toggleClass('selected');
});

$("#sortable1, #sortable2").sortable({
  revert: 0,
  helper: function(e, item) { //create custom helper
    if (!item.hasClass('selected')) item.addClass('selected');
    // clone selected items before hiding
    var elements = $('.selected').not('.ui-sortable-placeholder').clone();
    //hide selected items
    item.siblings('.selected').addClass('hidden');
    return $('<ul/>').append(elements);
  },
  start: function(e, ui) {
    var $elements = ui.item.siblings('.selected.hidden').not('.ui-sortable-placeholder');
    //store the selected items to item being dragged
    ui.item.data('items', $elements);
  },
  stop: function(e, ui) {
    //show the selected items after the operation
    ui.item.siblings('.selected').removeClass('hidden');
    //unselect since the operation is plete
    $('.selected').removeClass('selected');
    $(this).find('input:checked').prop('checked', false);
  }
});

var $tabs = $("#tabs").tabs(),
  $tab_items = $("ul:first li", $tabs).droppable({
    accept: "ul, .connectedSortable li",
    hoverClass: "ui-state-hover",
    drop: function(event, ui) {
      var $item = $(this),
        $elements = ui.draggable.data('items'),
        $list = $($item.find("a").attr("href")).find(".connectedSortable");
      ui.draggable.show().hide("slow", function() {
        $tabs.tabs("option", "active", $tab_items.index($item));
        $(this).appendTo($list).show("slow").before($elements.show("slow"));

      });
    }
  });
ul {
  list-style-type: none;
}
.connectedSortable li {
  margin: 0 5px 5px 5px;
  padding: 5px;
  font-size: 1.2em;
  width: 120px;
}
.chbox {
  margin-right: 10px;
}
.selected {
  background: red !important;
}
.hidden {
  display: none !important;
}
<link href="http://code.jquery./ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery./ui/1.9.2/jquery-ui.js"></script>
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nunc tincidunt</a>

    </li>
    <li><a href="#tabs-2">Proin dolor</a>

    </li>
  </ul>
  <div id="tabs-1">
    <ul id="sortable1" class="connectedSortable ui-helper-reset">
      <li class="ui-state-default">
        <input class="chbox" type="checkbox" />Item 1</li>
      <li class="ui-state-default">
        <input class="chbox" type="checkbox" />Item 2</li>
      <li class="ui-state-default">
        <input class="chbox" type="checkbox" />Item 3</li>
      <li class="ui-state-default">
        <input class="chbox" type="checkbox" />Item 4</li>
    </ul>
  </div>
  <div id="tabs-2">
    <ul id="sortable2" class="connectedSortable ui-helper-reset"></ul>
  </div>
</div>


Updated Fiddle

发布评论

评论列表(0)

  1. 暂无评论