English 中文(简体)
User jQuery to drag a DIV and drop in a TD ... and have the DIV "snap" into place
原标题:

I am finishing up a rewrite of task management system, and am in the process of adding drag and drop capabilities.

alt text

I want the user to be able to drag a task DIV from one column (TD) and drop it in another column (TD). I have this working correctly, save for some minor formatting issues. I have the TD with a class called droppable that accepts draggable classes. What I would like to happen is actually remove the task DIV from the current TD and append it to the dropped on TD.

Here is my script:

<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>

Here is my 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>

Struggling with the drop event and how to implement this. Any help is appreciated!

问题回答

This example works, but it remains to be seen because the div is locatd anywhere. This works because the event is fired first "droppable" rather than "draggable"

    $(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;
            }
        });
    });

As @David Lively suggested we went with the sortable route ... did exactly what I needed it to do!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签