Drag n Drop between 2 Datatables

Drag n Drop between 2 Datatables

by Tim Cadieux

HTML

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
    <table class="dataTable" id="table1">
        <thead>
            <tr>
                <th>Rendering engine</th>

            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Trident</td>

            </tr>
            <tr>
                <td>Trident</td>

            </tr>
            <tr>
                <td>Trident</td>

            </tr>
            <tr>
                <td>Trident</td>
            </tr>

        </tbody>
    </table>

    <h2>Table 2</h2>
    <table class="dataTable" id="table2">
        <thead>
            <tr>
                <th>Rendering engine</th>
            </tr>
        </thead>

    </table>

JavaScript

var t1 = $('#table1').DataTable();
            var t2 = $('#table2').DataTable();

            //this will hold reference to the tr we have dragged and its helper
            var c = {};

            $("#table1 tr").draggable({
                helper: "clone",
                start: function (event, ui) {
                    c.tr = this;
                    c.helper = ui.helper;
                }
            });


            $("#table2").droppable({
                drop: function (event, ui) {
                    t2.row.add(c.tr).draw();

                    // <-- Do this if you want to remove the rows from the first table
                    $(c.tr).remove();
                    $(c.helper).remove();

                    //Redraw Table1
                    t1.draw();
                    // -->
                    return false;
                }
            });