【问题标题】:Make a selectable list also sortable使可选择的列表也可排序
【发布时间】:2013-05-24 20:03:13
【问题描述】:

我有一个可选择的列表,我也希望它是可排序的。我还想禁用列表中的套索功能,因为可选列表可以通过单击而不是使用套索功能来选择多个项目。当我尝试将 $('#selectable').sortable() 添加到我的代码时,列表不再可选择,但它是可排序的。

这是我的 jsfiddle 的链接:

http://jsfiddle.net/jortgonfreit/HJugc/

这是我当前的 javascript 代码:

var $currentlySelected = null;
var selected = [];

$('#selectable').sortable();
$('#selectable').selectable({
start: function(event, ui) {
    $currentlySelected = $('#selectable .ui-selected');
},
stop: function(event, ui) {
    for (var i = 0; i < selected.length; i++) {
        if ($.inArray(selected[i], $currentlySelected) >= 0) {
          $(selected[i]).removeClass('ui-selected');
        }
    }
    selected = [];
},
selecting: function(event, ui) {
    $currentlySelected.addClass('ui-selected'); // re-apply ui-selected class to currently selected items
},
selected: function(event, ui) {
    selected.push(ui.selected); 
}
});

【问题讨论】:

  • 你会澄清你的目标吗?您预计会发生什么,目前正在发生什么,您遇到了哪些阻碍您完成任务的问题?
  • 我希望可选择列表中的项目能够由用户自行决定移动。我希望所有项目在被重新使用后仍然可以选择。
  • ...目前正在发生什么,您遇到了哪些阻碍您完成它的问题? 你看,这不是“我的代码”类型的网站。我们可以帮助您解决与您的代码有关的问题,但我们不会按订单构建您的系统。
  • 当我将 $('#selectable').sortable 添加到 javascript 代码中时,该列表是可排序的,但不再可选择。
  • @GeorgeCummins 我意识到这一点。我只是没有包括所有的信息。我重新编辑了帖子以传达我对 sortable 的尝试。

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


【解决方案1】:

您可能无法同时进行这两种操作,因为 sortable 和 selectable 都在同一个点击事件上,而只需给一个持有者进行排序。

$('#selectable').sortable({ handle: ".holder" })//Make sortable on the handle
  .selectable({
    start: function(event, ui) {
        $currentlySelected = $('#selectable .ui-selected');
    }
    ,cancel: ".holder" //Cancel selection for holder
    ,
    stop: function(event, ui) {
        for (var i = 0; i < selected.length; i++) {
            if ($.inArray(selected[i], $currentlySelected) >= 0) {
              $(selected[i]).removeClass('ui-selected');
            }
        }
        selected = [];
    },
    selecting: function(event, ui) {
        $currentlySelected.addClass('ui-selected'); // re-apply ui-selected class to currently selected items
    },
    selected: function(event, ui) {
        selected.push(ui.selected); 
    }
}).find( "li" )
        .addClass( "ui-corner-all" )
         .prepend( 
             $('<div/>',
                 {'class':'holder'} )
            .append(
                 $('<span/>',
                     {'class':'ui-icon ui-icon-transfer-e-w'})
                  )
             );//Just prepend handle to li's or just addthem in your html itself and remove this section.

Fiddle

如果您不使用自己的图标,可以使用此Jquery UI Icons 提供您想要的任何图标。

【讨论】:

    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2019-09-19
      相关资源
      最近更新 更多