javascript - 用户 jQuery 拖动一个 DIV 并放入一个 TD ... 并将 DIV "snap"放置到位

标签 javascript jquery asp.net-mvc jquery-ui drag-and-drop

我正在完成任务管理系统的重写,并且正在添加拖放功能。

alt text

我希望用户能够将任务 DIV 从一列 (TD) 拖放到另一列 (TD) 中。我有这个工作正常,除了一些小的格式问题。我的 TD 有一个名为 droppable 的类,它接受可拖动的类。我想要发生的事情实际上是从当前的 TD 中删除任务 DIV,并将其附加到已删除的 TD 上。

这是我的脚本:

<script type="text/javascript">
    $(function() {
        $(".draggable").draggable({
            cursor: 'move',
            cancel: 'a',
            revert: 'invalid',
            snap: 'true'
        });
    });
    $(function() {
        $(".droppable").droppable({
            accept: '.draggable',
            hoverClass: 'droppable-hover',
            drop: function(event, ui) { }
        });
    });

</script>

这是我的 HTML:

<h3>
    My Queue</h3>
<table style="width: 100%;" class="queue">
    <tbody>
        <tr>            
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StagePG">                
            </td>            
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StageRY">                
            </td>           
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StagePR">                
                <div class="queue-item draggable" title="Task description goes here.">
                    <em>Customer</em>
                    <strong>Project</strong>
                    <h4><a href="/Sauron/Task/Details/100001">100001</a></h4>
                </div>

                <div class="queue-item draggable" title="Task description goes here.">

                    <em>Customer</em>
                    <strong>Project</strong>
                    <h4><a href="/Sauron/Task/Details/100002">100002</a></h4>
                </div>              
            </td>
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StageRT">
            </td>
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StageTE">
            </td>
            <td style="width: 14%; vertical-align:bottom ;" class="droppable" id="StageRL">
            </td>            
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td style="width: 14%; text-align: center;">
                Pending (0)
            </td>
            <td style="width: 14%; text-align: center;">
                Ready (0)
            </td>
            <td style="width: 14%; text-align: center;">
                In Progress (2)
            </td>
            <td style="width: 14%; text-align: center;">
                Ready for Testing (0)
            </td>
            <td style="width: 14%; text-align: center;">
                Testing (0)
            </td>
            <td style="width: 14%; text-align: center;">
                Ready for Release (0)
            </td>
        </tr>
    </tfoot>
</table>

努力处理 drop 事件以及如何实现它。感谢您的帮助!

最佳答案

此示例有效,但仍有待观察,因为 div 位于任何位置。这是有效的,因为事件首先被触发为“可放置”而不是“可拖动”

    $(function() {

     $_dropEnd = null;

     $(".draggable").draggable({
            cursor: 'move',
            cancel: 'a',
            revert: 'invalid',
            snap: 'true',
            stop: function(event, ui) { 
               $(this).appendTo($_dropEnd);
               $_dropEnd = null;
            }
        });
    });
    $(function() {
        $(".droppable").droppable({
            accept: '.draggable',
            hoverClass: 'droppable-hover',
            drop: function(event, ui) {
               $_dropEnd = this;
            }
        });
    });

关于javascript - 用户 jQuery 拖动一个 DIV 并放入一个 TD ... 并将 DIV "snap"放置到位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1730656/

相关文章:

javascript - 如何创建淡入倒数计时器?

javascript - 通过只传递一次 props 来多次执行状态和数组操作

javascript - 如何在复选框单击时停止事件冒泡

jquery - CSS3 渐变 jQuery slider

javascript - jQuery scrollTop 不适用于具有高度和滚动条的 HTML 元素

asp.net-mvc - MVC @import html 关键字

javascript - 使用jquery按名称获取多个隐藏值

javascript - 防止 jquery 自动完成在单击外部时关闭搜索结果框

javascript - 搜索 div 及其所有子项内的文本

JavaScript window.showModalDialog() 无法在 Chrome 上运行