如何将jQuery可排序项放入折叠列表?

时间:2014-06-24 21:28:16

标签: jquery jquery-ui jquery-ui-sortable jquery-mobile-collapsible

测试代码 - http://jsfiddle.net/reGb3/

我正在使用jQuery可排序列表,我想将项目从一个列表拖到另一个列表。当主要类别被扩展时,我有工作(参见测试代码),但如果主要列表被折叠,我无法获得被删除的项目以成功填充隐藏列表。通过制作主要类别列表项目" droppable"然后展开列表,但我需要再次执行拖放操作。任何方法都可以在主要类别折叠(首选)或使用额外的展开步骤时发生这种情况?

HTML:

<ul data-role="listview" class="sortable-list" data-filter="true">

  <li data-role="collapsible" data-iconpos="right" data-inset="false">
    <h2 class="droppable"   id="cat1">EMPTY</h2>
    <ul data-role="listview" data-theme="b" class="sortable-list1" >

    </ul>
  </li>    
  <li data-role="collapsible" data-iconpos="right" data-inset="false" >
    <h2 class="droppable" id="cat2">Birds</h2>
    <ul data-role="listview" data-theme="b" class="sortable-list1">
      <li id="id1"><a href="#">Condor</a></li>
      <li id="id2"><a href="#">Eagle</a></li>
      <li id="id3"><a href="#">Sparrow</a></li>
    </ul>
  </li>
  <li data-role="collapsible" data-iconpos="right" data-inset="false" >
    <h2 class="droppable" id="cat3">Fish</h2>
    <ul data-role="listview" data-theme="b" class="sortable-list1">
      <li id="id4"><a href="#">Salmon</a></li>
      <li id="id5"><a href="#">Pollock</a></li>
      <li id="id6"><a href="#">Trout</a></li>
    </ul>
  </li>
</ul>

JavaScript的:

$(document).ready(function() {
    $('.sortable-list').sortable({
      connectWith: '.sortable-list',
      dropOnEmpty: true,
      update: function(event, ui) {

      }
    });

    $(".droppable").droppable({
        drop: function( event, ui ) {           
            var collapsedList = $(this).parent() ;
            if ($(collapsedList).collapsible( "option", "collapsed" )){
                $(collapsedList).collapsible( "expand" );
            }
          }
    });

    var sortupdate = function (event, ui) {

    };

    $('.sortable-list1').on("sortupdate", sortupdate);
    $('.sortable-list1').sortable({
      connectWith: '.sortable-list1',
      dropOnEmpty: true      
    });
});

CSS:

.ui-listview>.ui-li-static, .ui-listview>.ui-li-divider, .ui-listview>li>a.ui-btn {
overflow: visible !important;
}

.sortable-list1 {
    min-height:50px;
    padding:10px
}

谢谢!

1 个答案:

答案 0 :(得分:1)

通常,sortable窗口小部件中的项目只能通过sortable选项移动到另一个connectWith窗口小部件。在这种情况下,您实际上将项目从sortable列表移动到正常标题(droppable)而不是可排序列表。所以你必须处理事件drop。看起来我们可以简单地将draggable项(通过ui.draggable访问)附加到与此drop事件处理程序中的droppable项关联的列表中,但它并不是那么简单。这不是附加可拖动项目的正确位置,因为在删除目标不是有效可排序列表时,可能会将可拖动项目附加到原始列表后面的某些阶段,因此附加将失败。我想我们必须找到一个解决方法。在drop事件处理程序中,我们只需保存对应该附加可拖动项的列表的引用。然后我们需要处理sortstop事件,在将拖动的项目(可以通过ui.item访问)附加到该列表之前检查对目标列表的引用是否有效(非空)。这是编辑过的代码:

var receiver;
$(".droppable").droppable({
    drop: function( event, ui ) {           
          var collapsedList = $(this).parent() ;
          if ($(collapsedList).collapsible( "option", "collapsed" )){
            $(collapsedList).collapsible( "expand" );
          }            
              //save the target list to receiver for later appending
              receiver = $(this).parent().find('.sortable-list1');
        }
});

$('.sortable-list1').sortable({
        connectWith: '.sortable-list1',
        dropOnEmpty: true,
        //handle the sortstop event
        stop: function(e,ui){
              //check if receiver is valid (not null)
              if(receiver){
                receiver.append(ui.item);
                receiver = null; //remember to reset it to null
              }
            }
});

Demo.