jQuery UI可排序连接列表css问题

时间:2013-10-30 14:18:24

标签: javascript jquery css jquery-ui jquery-ui-sortable

我正在使用jquery ui对连接列表进行排序。我有一个问题,一个最重要的要求。

  1. 拖动li.ui-splitselect-item
  2. 时,项目会排在其他列表后面
  3. 将项目拖动到右侧(当鼠标太右时)创建水平滚动
  4. 重要提示:列表ul.ui-splitselect-list应该overflow-y:auto;因此列表标题会保持不变    并且只滚动列表项  enter image description here

    注意: 我之前在STACKOVERFLOW上提出了这个问题,但没有注意到我的重要要求在解决方案中丢失了,所以我再次明确地提出了问题。

    JSFIDDLE: http://jsfiddle.net/bababalcksheep/z67Td/

    HTML样机:

    <div class="ui-splitselect ui-helper-clearfix ui-widget ui-widget-content" style="height:200px;" >
        <div class="ui-widget-content ui-splitselect-selected">
            <div class="ui-widget-header ui-helper-clearfix">
                 List1
            </div>
            <ul id="sortable1"  class="ui-splitselect-list" style="height: 332px;">
                <li class="ui-splitselect-item ui-state-default">
                    <a class='ui-splitselect-handle-drag'><span class='ui-icon ui-icon-carat-2-n-s'></span></a>
                    <span class="ui-splitselect-handle-select">TEST-List1</span>
                    <a class="ui-splitselect-handle-move" href="#"><span class="ui-icon ui-icon-plus"></span></a>
                </li>  
            </ul>
        </div>
        <div class="ui-widget-content ui-splitselect-available"  >
            <div class="ui-widget-header ui-helper-clearfix">
                  List2
            </div>
            <ul id="sortable2"  class="ui-splitselect-list" style="height: 332px;">
            </ul>
        </div>
    </div>
    

    CSS:

    .ui-splitselect{font-size:.8em;width:100%!important;text-align:center;margin:0 auto;padding:0}
    .ui-splitselect ul{-moz-user-select:none}
    .ui-splitselect .ui-widget-header{border:none;font-size:11px}
    .ui-splitselect-selected{height:100%!important;float:left;border:none;width:50%;margin:0;padding:0}
    .ui-splitselect-available{height:100%!important;width:49.4%;float:left;border-top:none;border-bottom:none;border-right:none;margin:0;padding:0}
    .ui-splitselect-list{height:92%!important;position:relative;list-style:none;overflow:auto;margin:0;padding:0}
    .ui-splitselect-item{cursor:default;line-height:20px;height:20px;font-size:11px;list-style:none;display:list-item;white-space:nowrap;overflow:hidden;margin:1px;padding:0}
    .ui-splitselect-item.ui-sortable-helper{z-index:99999}
    .ui-splitselect-handle-select{float:left}
    .ui-splitselect-handle-drag{float:left;height:20px;border-top:0;border-bottom:0;cursor:pointer;margin:0 10px 0 5px;padding:2px 5px}
    .ui-splitselect-handle-move{text-decoration:none;cursor:pointer;float:right;height:20px;border-top:0;border-bottom:0;margin:0 5px 0 10px;padding:2px 5px}
    

    JS:

     $("#sortable1, #sortable2").sortable({
         connectWith: ".ui-splitselect-list",
         containment: ".ui-splitselect",
         scroll: false,
         placeholder: "ui-state-highlight ui-splitselect-item"
     }).disableSelection();
    

2 个答案:

答案 0 :(得分:4)

尝试为appendTo: document.body添加helper: clonesortable选项,如下所示:

$("#sortable1, #sortable2").sortable({
  appendTo: document.body,
  helper: "clone",
  connectWith: ".ui-splitselect-list",
  containment: ".ui-splitselect",
  scroll: false,
  placeholder: "ui-state-highlight ui-splitselect-item"
}).disableSelection();

Js fiddle:http://jsfiddle.net/XpP25/2/

Trick正在创建克隆原始元素的排序助手,然后将其附加到body元素以解决zIndex问题。在可拖动和可排序的停止事件之后,所有帮助程序都会自动删除,因此不应该弄乱您的代码:)

希望它有所帮助。

答案 1 :(得分:1)

我在嵌套可排序项目上也遇到类似的问题。我有多个可排序的容器,它们组成一个网格,然后是这些可排序容器中的小部件。每个小部件内部都有一个嵌套的可排序容器,该容器占用了宽度和高度。在嵌套可排序对象内部,我可以有多个嵌套小部件,它们跨越嵌套可排序对象的宽度。

以下是这些可排序项之一的外观:

<div class="sortable">
  <div class="widget">
    <div class="nested-sortable">
      <div class="nested-widget"></div>
    </div>
  </div>
</div>

现在,一旦我开始拖动嵌套的可排序项目,它就会放在每个外部小部件的后面,除了包含该小部件的那个之外。我尝试在排序开始时更改项目的z索引,但没有运气。我需要类似jQuery UI的Draggable stack选项的功能,该选项将当前拖动的项目“堆叠”在同一类的所有其他项目上。

这是github上第800行的jQuery UI的可拖动堆栈函数。

所以,我和我的朋友做了一些修改,并在“外部”小部件上对其进行了命名。

$.fn.stack = function () {
    // where $(this) == '.widget'
    var o = $(this).closest('.sortable').siblings('.sortable').find('.widget');

    // create an array of all the widgets
    var group = $(o).sort(function(a,b) {
        // compare the each set of widgets
        return (parseInt($(a).css("z-index"),10) || 1) - (parseInt($(b).css("z-index"),10) || 1);
    });
    if (!group.length) { return; }

    var min = parseInt($(group[0]).css('z-index')) || 1; // first widget's z-index
    $(group).each(function(i) {
        // increase each widget's z-index by min + $(this) widget's index value
        $(this).css('z-index',  min + i);
    });

    // make currently dragged widget's z-index min + length of group
    $(this[0]).css('z-index', min + group.length);
}

然后以可排序的开始调用:

$('.nested-sortable').sortable({
    start: function(event, ui) {
        $(this).closest('.widget').stack();
    }
});

这是我的代码笔供参考:Nested Grid System

相关问题