Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop table cell contents

Tags:

html

jquery

I have a Dynamic table here. Table data includes one span also. How can drag and drop that span anywhere among table's cells?

<table id="#our_table">
    <tr>
        <th>head1</th>
        <th>head1</th>
        <th>head1</th>
    </tr>
    <tr>
        <td><span id="event"></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>
like image 342
kannan D.S Avatar asked Oct 24 '25 19:10

kannan D.S


1 Answers

Or you just use drag drop html5 with this simple code (Fiddle)

<script type="text/javascript">
$(document).ready(function(){
        $('.event').on("dragstart", function (event) {
              var dt = event.originalEvent.dataTransfer;
              dt.setData('Text', $(this).attr('id'));
            });
        $('table td').on("dragenter dragover drop", function (event) {  
           event.preventDefault();
           if (event.type === 'drop') {
              var data = event.originalEvent.dataTransfer.getData('Text',$(this).attr('id'));
              de=$('#'+data).detach();
              de.appendTo($(this)); 
           };
       });
})
</script>

with this code you must also assign an id to your span tag
in this other fiddle you can see the effect with two span tags.
In this case if there is a span in the cell the drop event does not work

like image 126
Devima Avatar answered Oct 26 '25 09:10

Devima



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!