简体   繁体   English

jQuery UI拖放,可排序(保留拖动的项目)

[英]Jquery UI Drag, Drop and Sortable (Retaining the dragged item)

There are 2 div Container top and botton which contains ul tag. 有2个div容器顶部和包含ul标签的botton。

My requirement is when record is dragged onto 1st container the same record has to be retained even in bottom container. 我的要求是,将记录拖到第一个容器上时,即使在底部容器中也必须保留相同的记录。 Top Container records should be sortable but bottom is used only for dragging the record to top container and bottom container is not sortable. 顶部容器记录应该是可排序的,但底部仅用于将记录拖动到顶部容器,而底部容器是不可排序的。

Top Container -> Sortable 顶部容器 ->可排序

Bottom Container -> Not Sortable (not needed) 底部容器 ->无法排序(不需要)

Drag -> Happens only from bottom to top (when dragged record should be retained on top and bottom) 拖动 ->仅从底部发生到顶部(当拖动的记录应保留在顶部和底部时)

Jsfiddle Link http://jsfiddle.net/bbhrsn9u/ Jsfiddle链接 http://jsfiddle.net/bbhrsn9u/

    <script type="text/javascript">

       $(document).ready(function() {

        $("#sortable").sortable({
            revert: true,
            helper : 'clone',
            revert :10          
        });


        $("ol li").disableSelection();

        $(".sort_list li").draggable({

            tolerance:"pointer",
            helper : 'clone',
            refreshPositions: true ,
            revert : 'invalid',
            opacity:.4,
        });

        $(".drop_list ol").droppable({
            revert:true,

            greedy: true,
            refreshPositions: true,
            drop : function(ev, ui) 
            {
                $(ui.draggable).appendTo(this);
                if($(this)[0].id === "sortable")
                {
                    console.log($(this).closest("button").find('.hello'));
                    $(this).find('.hello').hide();
                    $(this).find('.AH_section').show();

                    ui.draggable.draggable( 'disable' ).closest('li').prependTo(ui.draggable.closest('ul')); //this will append dragged list at top of the container
                    return true;
                }
            }
        });
    });
    </script>

HTML Code HTML代码

 <div class="drop_list">
        <ol id="sortable" style="list-style:decimal;">
            <li id='item1' class="draggable_li qitem">
                <span class="item">Item = 1</span>
            </li>
            <li id='item2' class="draggable_li qitem">
                <span class="item">Item = 2</span>
            </li>
            <li id='item3' class="draggable_li qitem">
                <span class="item">Item = 3</span>
            </li>
            <li id='item4' class="draggable_li qitem">
                <span class="item">Item = 4</span>
            </li>           
        </ol>
    </div>

    <div class="sort_list">
    <ul id="draggable">
        <li id='item1' class="draggable_li qitem">
            <span class="item">Item Dragged = 1</span>
        </li>
        <li id='item2' class="draggable_li qitem">
            <span class="item">Item Dragged = 2</span>
        </li>
        <li id='item3' class="draggable_li qitem">
            <span class="item">Item Dragged = 3</span>
        </li>
        <li id='item4' class="draggable_li qitem">
            <span class="item">Item Dragged = 4</span>
        </li>   
        <li id='item5' class="draggable_li qitem">
            <span class="item">Item Dragged = 5</span>
        </li>       
    </ul>
    </div>

Snapshot 快照

You could use connectToSortable option. 您可以使用connectToSortable选项。 Then your clone would be kept and simplify your code. 然后将保留您的克隆并简化您的代码。 Like this: 像这样:

 $(document).ready(function() {

        $("#sortable").sortable({
            revert: true,
            helper : 'clone',
            revert :10          
        });


        $("ol li").disableSelection();

        $(".sort_list li").draggable({

            tolerance:"pointer",
            helper : 'clone',
            refreshPositions: true ,
            revert : 'invalid',
            opacity:.4,
            connectToSortable: '#sortable'
        });


    });

http://jsfiddle.net/t0ndh136/1/ http://jsfiddle.net/t0ndh136/1/

If you are trying to append the items from the Bottom container to the Top container but leaving the original ones intact what you need to do is instead of: 如果您试图将Bottom容器中的项目追加到Top容器中,但保持原样不变,则需要执行以下操作:

$(ui.draggable).appendTo(this);

do this: 做这个:

$(ui.draggable.clone()).appendTo(this);

Here is the working example: http://jsfiddle.net/bbhrsn9u/1/ 这是工作示例: http : //jsfiddle.net/bbhrsn9u/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM