【问题标题】:JQuery UI, sortable and selectableJQuery UI,可排序和可选择
【发布时间】:2013-06-05 10:12:23
【问题描述】:

我在组合 jquery 可排序和可选择时遇到了困难。

我有两个列表,并且希望能够从一个列表拖到另一个列表(无需选择即可)

但是选择而不是拖动它只是再次选择,有没有办法禁用对选定项目的选择或将拖动手柄放在选择手柄的外部,这样它们就不会干扰。

这是代码。

$(function() {
  $(".available, .assigned").sortable({
    connectWith: ".connected",
    handle: ".handle"
  });

  $(".available, .assigned").selectable({ filter: "li", cancel: ".handle" })
  $(".available, .assigned").disableSelection();
});

http://jsfiddle.net/MJYRD/

这是我能够关注这个问题jQuery UI sortable & selectable

谢谢

【问题讨论】:

  • 仅使用链接答案中的代码 sn-p 有什么问题?有了那个sn-p,我很容易got this to work
  • 当你放下它时,它只会放下拖动的项目而不是其他选定的项目,我已经到了我正在实施它的阶段。

标签: jquery jquery-ui jquery-ui-sortable jquery-ui-selectable


【解决方案1】:

我设法让它有点工作。 Check the fiddle.

基本上,我使用了 jquery.multisortable 插件,它扩展了默认的 sortable 小部件。这样,您可以通过 Ctrl+单击来选择多个项目。您可以拖动以重新排序和/或在列表之间移动项目。

或者,如果您想坚持使用选择矩形和拖动手柄的原始机制,您仍然可以使用selectable (demo)。唯一的缺点是multisortable 后面的multiselectable 行为保持活动状态,这意味着它仍然尝试处理Ctrl+单击(但失败)。这只是一个小麻烦,您可以调整multisortable 的代码,使其不会扩展multiselectable,这应该可以解决这个问题。

【讨论】:

  • 它似乎什么也没做。我想我有自己的解决方案。我正在使用可选择和可排序的事件来使它们一起工作。
  • @JosephLeBrech 什么不适合你?此外,如果您解决了自己的问题,您可以将解决方案添加为answer yourself。其他有类似问题的用户稍后可能会发现此问题并发现您的解决方案很有帮助。
  • 您在 jsfiddle 下的演示似乎没有做任何事情。但是,是的,我应该解决。我完成后会发布它。
  • @JosephLeBrech 您是指带有选择矩形和拖动手柄的演示吗?您正在执行的具体步骤是什么?我可以很好地选择和拖动项目。
  • @JosephLeBrech 啊,我明白了。我以为我可以使用来自multisortable 的 GitHub 项目的原始脚本文件,但它不能提供正确的 MIME 类型。 Firefox 似乎不在乎,但其他浏览器却在乎。我更新了两个小提琴以嵌入脚本,它现在应该可以工作了。在一个真实的应用程序中,您可以将jquery.multisortable.js 上传到您自己的服务器并使用<script> 标记包含它,但我无法在JSFiddle 中这样做。
【解决方案2】:

这是使列表可选择和可排序并允许套索实现的解决方法。这是fiddle

<html>
<meta charset="utf-8" />
<title>jQuery UI Sortable with Selectable</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
$(function() {
//

$('body').selectable({
    filter: 'li'
    //filter: '#album2 > li'
});

/*
Since the sortable seems unable to move more than one object at a 
time, we'll do this:

The LIs should act only as wrappers for DIVs.

When sorting a LI, move all the DIVs that are children of selected 
LIs to inside the sorting LI (this will make them move together);
but before doing that, save inside the DIVs a reference to their
respective original parents, so we can restore them later.

When the user drop the sorting, restore the DIVs to their original
parent LIs and place those LIs right after the just-dropped LI.

Voilá!

Tip: doesn't work so great if you try to stick the LIs inside the LI
*/

$('.connectedSortable').sortable({
    connectWith: ".connectedSortable",
    delay: 100,
    start: function(e, ui) {
        var topleft = 0;

        // if the current sorting LI is not selected, select
        $(ui.item).addClass('ui-selected');

        $('.ui-selected div').each(function() {

            // save reference to original parent
            var originalParent = $(this).parent()[0];
            $(this).data('origin', originalParent);

            // position each DIV in cascade
            $(this).css('position', 'absolute');
            $(this).css('top', topleft);
            $(this).css('left', topleft);
            topleft += 20;

        }).appendTo(ui.item); // glue them all inside current sorting LI

    },
    stop: function(e, ui) {
        $(ui.item).children().each(function() {

            // restore all the DIVs in the sorting LI to their original parents
            var originalParent = $(this).data('origin');
            $(this).appendTo(originalParent);

            // remove the cascade positioning
            $(this).css('position', '');
            $(this).css('top', '');
            $(this).css('left', '');
        });

        // put the selected LIs after the just-dropped sorting LI
        $('#album .ui-selected').insertAfter(ui.item);

        // put the selected LIs after the just-dropped sorting LI
        $('#album2 .ui-selected').insertAfter(ui.item);
    }
});




//
});


<style>
*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

#album {
    list-style: none;
    float: left;
    width: 20%;
    border: 1px solid blue;
}
#album2 {
    list-style: none;
    float: left;
    width: 20%;
    border: 1px solid blue;
}
#album li  {
    float: left;
    margin: 5px;
}

#album2 li  {
    float: left;
    margin: 5px;
}


#album div {
    width: 100px;
    height: 100px;
    border: 1px solid #CCC;

    background: #F6F6F6;    
}
#album2 div {
    width: 100px;
    height: 100px;
    border: 1px solid #CCC;

    background: #F6F6F6;    
}
#album .ui-sortable-placeholder {
    border: 1px dashed #CCC;
    width: 100px;
    height: 100px;
    background: none;
    visibility: visible !important;
}
#album2 .ui-sortable-placeholder {
    border: 1px dashed #CCC;
    width: 100px;
    height: 100px;
    background: none;
    visibility: visible !important;
}

#album .ui-selecting div, 
#album .ui-selected div {
    background-color: #3C6;
}

#album2 .ui-selecting div, 
#album2 .ui-selected div {
    background-color: #3C6;
}

#anotheralbum {
    list-style: none;
    float: left;
    width: 20%;
    height: 800px;
    border: 1px solid green;
}
</style>


<body>

<ul id="album" class="connectedSortable">
    <li id="li1"><div>1- First</div></li>
    <li id="li2"><div>2- Second</div></li>
    <li id="li3"><div>3- Third</div></li>
    <li id="li4"><div>4- Fourth</div></li>
    <li id="li5"><div>5- Fifth</div></li>
    <li id="li6"><div>6- Sixth</div></li>
    <li id="li7"><div>7- Seventh</div></li>
    <li id="li8"><div>8- Eighth</div></li>
</ul>

<ul id="album2" class="connectedSortable">
    <li id="li1"><div>1- 1</div></li>
    <li id="li2"><div>2- 2</div></li>
    <li id="li3"><div>3- 3</div></li>
    <li id="li4"><div>4- 4</div></li>
    <li id="li5"><div>5- 5</div></li>
    <li id="li6"><div>6- 6</div></li>
    <li id="li7"><div>7- 7</div></li>
    <li id="li8"><div>8- 8</div></li>
</ul>
<div id="anotheralbum" class="connectedSortable">
another album - no style for the lists inside here
</div>

<br style="clear:both">
</body>
</html>

【讨论】:

    【解决方案3】:

    因为拖动操作与选择操作或多或少是相同的操作,如果已选择项目,则必须取消选择。

    $(".available, .assigned").selectable({ 
        filter: "li", 
        cancel: ".ui-selected"
    })
    
    $("available, .assigned").sortable()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2011-12-11
      • 2013-08-21
      相关资源
      最近更新 更多