【问题标题】:Having Jquery sortable within droppable and draggable <div>让 Jquery 在可放置和可拖动的 <div> 中排序
【发布时间】:2013-03-28 13:07:45
【问题描述】:

我目前正在开发一个功能,我需要同时结合可放置、可拖动和可排序的功能。下面是我的测试代码。很抱歉,我无法为这段代码获取 jsfiddle。 Droppable 和 Draggable 工作正常。

问题:我很难在可放置的 div 中进行水平排序。如果有人可以帮助/指导我将其与排序列表中元素的索引/位置一起获取,我将不胜感激,无论该元素位于第 1、第 2 等位置。

HTML:

        <ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">
          <li class="ui-widget-content ui-corner-tr">
            <div class="ui-widget-header">High Tatras 1</div>
          </li>
          <li class="ui-widget-content ui-corner-tr">
            <div class="ui-widget-header">High Tatras 2</div>
          </li>
          <li class="ui-widget-content ui-corner-tr">
            <div class="ui-widget-header">High Tatras 3</div>
          </li>
          <li class="ui-widget-content ui-corner-tr">
            <div class="ui-widget-header">High Tatras 4</div>
          </li>
        </ul>

        <div id="trash" class="ui-widget-content ui-state-default">
            <ul id = "sortable" class='gallery ui-helper-reset sortable'></ul>
        </div>

以上是我用作测试数据的 HTML 代码。

JQuery:下面是我目前使用的Jquery代码。

                   var $gallery = $( "#gallery" ),
                   $trash = $( "#trash" );

                 // let the gallery items be draggable
                 $( "li", $gallery ).draggable({
                   cancel: "a.ui-icon", // clicking an icon won't initiate dragging
                   revert: "invalid", // when not dropped, the item will revert back to its initial position
                   containment: "document",
                   helper: "clone",
                   cursor: "move"
                 });

                 // let the trash be droppable, accepting the gallery items
                 $trash.droppable({
                   accept: "#gallery > li",
                   activeClass: "ui-state-highlight",
                   drop: function( event, ui ) {
                     addToContainer( ui.draggable );
                   }
                 });

                 // let the gallery be droppable as well, accepting items from the trash
                 $gallery.droppable({
                   accept: "#trash li",
                   activeClass: "custom-state-active",
                   drop: function( event, ui ) {
                     RemoveFromContainer( ui.draggable );
                   }
                 });

                 function addToContainer( $item ) {
                   $item.fadeOut(function() {
                     var $list = $( "ul", $trash );

                     $item.appendTo($list).fadeIn(function() {
                       $item
                         .addClass('ui-state-default')
                         .find( "div .ui-widget-header" )
                           .animate({ height: "36px"});
                     });
                   });

                   $( ".sortable" ).sortable({
                       connectWith: "#sortable"
                   }).disableSelection();

                 }

                 function RemoveFromContainer( $item ) {
                   $item.fadeOut(function() {
                     $item
                       .find( "a.ui-icon-refresh" )
                         .remove()
                       .end()
                       .css( "width", "96px")
                       .find( "img" )
                         .css( "height", "72px" )
                       .end()
                       .appendTo( $gallery )
                       .fadeIn();
                   });
                 }

                 // resolve the icons behavior with event delegation
                 $( "ul.gallery > li" ).click(function( event ) {

                   var $item = $( this ),
                     $target = $( event.target );

                   if ( $target.is( "a.ui-icon-trash" ) ) {
                     addToContainer( $item );
                   } else if ( $target.is( "a.ui-icon-refresh" ) ) {
                     RemoveFromContainer( $item );
                   }

                   return false;
                 });

如果我遗漏了什么并且未能发布正确的问题,请告诉我。我对stackoverflow进行了一些研究,但仍然找不到确切的解决方案。非常感谢您提前。

【问题讨论】:

标签: jquery jquery-ui-sortable jquery-ui-droppable


【解决方案1】:

使用 2 个sortable 并将connectWith 属性设置为彼此:

var $gallery = $("#gallery"),
    $trash = $("#trash");
$("#gallery").sortable({
    connectWith: "#trash",
    helper: "",
    revert: "invalid",
    start: function (event, ui) {
        var $item = ui.helper;
        $item.css({
            'width': $('#gallery li').width()
        });
    },
    stop: function (event, ui) {
       //get what you want here
    }
});

$('#trash').sortable({
    placeholder: "ui-state-highlight",
    connectWith: '#gallery',
    revert: true,
    cursor: "move",
    items: "li",
    drop: function (event, ui) {

    },
    stop: function (event, ui) {
        var $obj = $(ui.item);
       //get what you want here
    }
});

尝试使用小提琴:

http://jsfiddle.net/mw3Pn/2/

【讨论】:

  • 它有点工作,因为现在您使用的是 2 个可排序列表,而我使用的是拖放。 Jquery UI 显示了这两种方式。但是我想我们的解决方案都不支持一些事情。如果您能帮助我指导如下所示的内容,我将不胜感激。这是我第一次使用 Jquery UI 的拖放/排序功能,所以尽我所能。
  • 1.在您的解决方案中,两者都是可排序的列表。有没有办法可以使#gallery 不可排序并且只有垃圾列表可排序...? 2. 每当我从#gallery 中丢弃任何东西或在#trash 中对任何东西进行排序时,是否可以获得我要丢弃的位置/位置?我认为我在每个事件中都使用 ui.item.index() 来解决这些问题?
猜你喜欢
  • 2012-06-23
  • 2011-08-13
  • 2013-04-13
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多