jQuery 可拖动 +可排序 - 如何在两个可滚动列表之间拖放

发布于 2024-09-30 16:18:00 字数 4151 浏览 3 评论 0原文

我想要两个列表,可用项目和选定项目,其中可用项目通过拖放分配给选定项目。我要求所选项目可排序,但不要求可用项目可排序。挑战在于这两个列表都可能包含大量项目,因此需要可滚动。

这是我到目前为止的 jQuery:

        <script type="text/javascript">
        $(function() {
            $( "#available > li" ).draggable({ 
                revert: 'invalid',
                connectToSortable: '#selected',
                containment: '#drag_container'
            });

            $( "#selected" ).sortable({
                axis: 'y',
                placeholder: 'ui-state-highlight'
            });
        });
    </script>

以及相应的 HTML:

            <div class="drag_container">
            <ul id="available" class="drag_column draggable">
                <li id="item1" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 1</li>
                <li id="item2" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 2</li>
                <li id="item3" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 3</li>
                <li id="item4" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 4</li>
                <li id="item5" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 5</li>
                <li id="item6" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 6</li>
                <li id="item9" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 9</li>
                <li id="item10" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 10</li>
            </ul>


            <ul id="selected" class="drag_column draggable sortable" style="margin-left: 20px;">
                <li id="item7" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 7</li>
                <li id="item8" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 8</li>
                <li id="item9" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 9</li>
                <li id="item10" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 10</li>
                <li id="item11" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 11</li>
                <li id="item12" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 12</li>
                <li id="item13" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 13</li>
                <li id="item14" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 14</li>
                <li id="item15" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 15</li>
                <li id="item16" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 16</li>
                <li id="item17" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 17</li>
                <li id="item18" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 18</li>
            </ul>
            <div style="clear: both">&nbsp;</div>
        </div>

但是,随着可滚动列表的要求,我无法让可拖动行为优雅地工作(请参阅 http://pastehtml.com/view/1bsk6bt.html)。

一旦被拖动的项目进入可用项目列表,它就会消失在可滚动框架后面。我尝试过克隆助手,也尝试过使用包含 div、不同的溢出选项、关闭 jQuery 中的滚动选项,但无法使其正常工作。我确信有人已经完成了我在这里要做的事情,并且可以节省我一些时间? :)

任何帮助将不胜感激!

I would like to have two lists, Available Items and Selected Items, whereby Available Items are assigned to Selected Items via drag and drop. I require the Selected Items to be sortable, but not the Available Items. The challenge is that both lists can potentially contain a significant number of items, and therefore need to be scrollable.

Here is the jQuery I have thus far:

        <script type="text/javascript">
        $(function() {
            $( "#available > li" ).draggable({ 
                revert: 'invalid',
                connectToSortable: '#selected',
                containment: '#drag_container'
            });

            $( "#selected" ).sortable({
                axis: 'y',
                placeholder: 'ui-state-highlight'
            });
        });
    </script>

And the corresponding HTML:

            <div class="drag_container">
            <ul id="available" class="drag_column draggable">
                <li id="item1" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 1</li>
                <li id="item2" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 2</li>
                <li id="item3" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 3</li>
                <li id="item4" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 4</li>
                <li id="item5" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 5</li>
                <li id="item6" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 6</li>
                <li id="item9" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 9</li>
                <li id="item10" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 10</li>
            </ul>


            <ul id="selected" class="drag_column draggable sortable" style="margin-left: 20px;">
                <li id="item7" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 7</li>
                <li id="item8" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 8</li>
                <li id="item9" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 9</li>
                <li id="item10" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 10</li>
                <li id="item11" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 11</li>
                <li id="item12" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 12</li>
                <li id="item13" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 13</li>
                <li id="item14" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 14</li>
                <li id="item15" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 15</li>
                <li id="item16" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 16</li>
                <li id="item17" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 17</li>
                <li id="item18" class="ui-state-default"><span class="ui-icon ui-icon-arrow-4-diag"></span>Item 18</li>
            </ul>
            <div style="clear: both"> </div>
        </div>

With the scrollable list requirement, however, I can't get the draggable behaviour to work elegantly (see the demo at http://pastehtml.com/view/1bsk6bt.html).

Once the item being dragged enters the Available Items list, it disappears behind the scrollable frame. I've tried the clone helper, and also tried playing around with containing divs, different overflow options, turning off the scroll option in jQuery, but cannot get it to work properly. I'm sure someone out there has accomplished something like what I'm aiming to do here and can save me some time? :)

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦断已成空 2024-10-07 16:18:00

哇!修复这个问题很有趣。

第一个问题是列表永远水平滚动,我通过 CSS 中的一些简单的溢出更改来修复。我从您的 .drag_column 中删除了两个溢出属性,并将溢出:隐藏在 .drag_container 中。

.drag_container {
    width: 1000px;
    margin: 0 auto;
    position: relative;
    border: 1px solid black;
    position: relative;
    z-index: 1;
    overflow: hidden;
}

.drag_column {
    padding: 5px;
    width: 490px;
    height: 200px;
    float: left;
    position: relative;
}

不幸的是,当我这样做时,当您将可拖动对象从一个列表移动到另一个列表时,它会产生位置错误(它会向上射击,具体取决于您选择的列表项)。为了解决这个问题,我将“helper:clone”行添加到了可拖动创建函数中。

$( "#available > li" ).draggable({ 
    revert: 'invalid',
    connectToSortable: '#selected',
    containment: '#drag_container',
    helper: 'clone'
});

这再次造成了不良影响。克隆助手使得原始列表永远不会删除项目。为了解决这个问题,我必须手动创建一个删除旧项目的函数。我所做的就是向可拖动对象添加一个 start: 函数,该函数获取对象 id 并将其放入变量中。然后我创建了 #se​​lected 列表的可放置对象,并在那里创建了一个 drop: 函数。该 drop 函数只是对具有匹配 id 的对象执行 slipUp(100) 操作。请注意,我在脚本启动时创建了一个变量 - 这修复了 IE 中的错误。

var dragged = null;
$(function() {
    $( "#available > li" ).draggable({ 
        revert: 'invalid',
        connectToSortable: '#selected',
        containment: '#drag_container',
        helper: 'clone',
        start: function(ui, event) {
            dragged = $(this).attr('id');
        }
    });
    $( "#selected" ).droppable({
        drop: function(event, ui) {
            var ident = "#" +  dragged;
            $(ident).slideUp(100);
        }
    });

我在 http://pastehtml.com/view/1by9nfd.html 发布了一个包含完整 HTML 的页面,以便您可以玩如果你愿意的话。希望这有帮助!

Whew! That was a fun one to fix.

The first problem, with the list scrolling forever horizontally, I fixed with some simple overflow changes in your CSS. I removed both of the overflow attributes from your .drag_column, and put an overflow: hidden in .drag_container.

.drag_container {
    width: 1000px;
    margin: 0 auto;
    position: relative;
    border: 1px solid black;
    position: relative;
    z-index: 1;
    overflow: hidden;
}

.drag_column {
    padding: 5px;
    width: 490px;
    height: 200px;
    float: left;
    position: relative;
}

Unfortunately, when I did that it created a position error when you moved your draggable from one list to another (it would shoot upwards, depending on which list item you chose). To fix this, I added the line "helper: clone" to your draggable creation function.

$( "#available > li" ).draggable({ 
    revert: 'invalid',
    connectToSortable: '#selected',
    containment: '#drag_container',
    helper: 'clone'
});

Once again, this created an undesirable effect. The clone helper makes it so the original list never has items deleted. To fix this, I had to manually create a function that would delete the old item. What I did was I added a start: function to your draggable object, that grabbed the object id and put it in a variable. Then I created a droppable object of your #selected list, and made a drop: function there. That drop function simply does a slideUp(100) on the object with the matching id. Note that I have a variable creation at the initiation of the script - this fixes a bug in IE.

var dragged = null;
$(function() {
    $( "#available > li" ).draggable({ 
        revert: 'invalid',
        connectToSortable: '#selected',
        containment: '#drag_container',
        helper: 'clone',
        start: function(ui, event) {
            dragged = $(this).attr('id');
        }
    });
    $( "#selected" ).droppable({
        drop: function(event, ui) {
            var ident = "#" +  dragged;
            $(ident).slideUp(100);
        }
    });

I posted a page with the full HTML at http://pastehtml.com/view/1by9nfd.html so you can play around if you want. Hope this helps!

春庭雪 2024-10-07 16:18:00

我需要这个功能,并且找到了一种方法来

使用appendTo:“body”并在开始时设置绝对位置。被拖动的元素会附加到正文,并且始终位于其他元素的“顶部”,因为它的位置对于正文来说是绝对的。

$( "#available > li" ).draggable({

    revert: 'invalid',
    appendTo: 'body',
    helper: 'clone',
    connectToSortable: '#selected',
    containment: '#drag_container',

start: function( event, ui ){

    $(ui.helper).css('position', 'absolute'); //helper if you are using clone

},


stop: function(event, ui){

    $(ui.helper).css('position', 'relative'); // if you want

} 

});

希望这有帮助

I needed this functionality and had found a way to do this

use appendTo: "body" and set position absolute on start. The element being dragged gets appended to body and is alwayz "top" of other elements because its position is absloute to the body.

$( "#available > li" ).draggable({

    revert: 'invalid',
    appendTo: 'body',
    helper: 'clone',
    connectToSortable: '#selected',
    containment: '#drag_container',

start: function( event, ui ){

    $(ui.helper).css('position', 'absolute'); //helper if you are using clone

},


stop: function(event, ui){

    $(ui.helper).css('position', 'relative'); // if you want

} 

});

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文