I have two tables where I can drag and drop items between tables and drag and drop items within the same table. I want to detect the table from which the item is coming.
HTML code:
<code><table id="peopleTable1">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>Peter</td>
<td>13</td>
<td>Boy</td>
</tr>
<tr>
<td>David</td>
<td>20</td>
<td>Boy</td>
</tr>
<tr>
<td>Martha</td>
<td>40</td>
<td>Girl</td>
</tr>
</tbody>
</table>
<table id="peopleTable2">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>7</td>
<td>Boy</td>
</tr>
<tr>
<td>Laura</td>
<td>27</td>
<td>Girl</td>
</tr>
<tr>
<td>ELisabeth</td>
<td>73</td>
<td>Girl</td>
</tr>
</tbody>
</table>
</code>
<code><table id="peopleTable1">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>Peter</td>
<td>13</td>
<td>Boy</td>
</tr>
<tr>
<td>David</td>
<td>20</td>
<td>Boy</td>
</tr>
<tr>
<td>Martha</td>
<td>40</td>
<td>Girl</td>
</tr>
</tbody>
</table>
<table id="peopleTable2">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>7</td>
<td>Boy</td>
</tr>
<tr>
<td>Laura</td>
<td>27</td>
<td>Girl</td>
</tr>
<tr>
<td>ELisabeth</td>
<td>73</td>
<td>Girl</td>
</tr>
</tbody>
</table>
</code>
<table id="peopleTable1">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>Peter</td>
<td>13</td>
<td>Boy</td>
</tr>
<tr>
<td>David</td>
<td>20</td>
<td>Boy</td>
</tr>
<tr>
<td>Martha</td>
<td>40</td>
<td>Girl</td>
</tr>
</tbody>
</table>
<table id="peopleTable2">
<thead class="thead-acu">
<tr>
<th class="col-30p">Name</th>
<th class="col-35p">Age</th>
<th class="col">Sex</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>7</td>
<td>Boy</td>
</tr>
<tr>
<td>Laura</td>
<td>27</td>
<td>Girl</td>
</tr>
<tr>
<td>ELisabeth</td>
<td>73</td>
<td>Girl</td>
</tr>
</tbody>
</table>
JS code:
<code>$("#peopleTable1 tbody").sortable({
connectWith: "#peopleTable2 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
stop: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users2 -- Users1")
},
axis: "xy",
});
$("#peopleTable2 tbody").sortable({
connectWith: "#peopleTable1 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
update: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users1 -- Users2")
},
axis: "xy",
});
</code>
<code>$("#peopleTable1 tbody").sortable({
connectWith: "#peopleTable2 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
stop: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users2 -- Users1")
},
axis: "xy",
});
$("#peopleTable2 tbody").sortable({
connectWith: "#peopleTable1 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
update: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users1 -- Users2")
},
axis: "xy",
});
</code>
$("#peopleTable1 tbody").sortable({
connectWith: "#peopleTable2 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
stop: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users2 -- Users1")
},
axis: "xy",
});
$("#peopleTable2 tbody").sortable({
connectWith: "#peopleTable1 tbody",
helper: function (e, tr) {
const $originals = tr.children();
const $helper = tr.clone();
$helper.children().each(function (index) {
// Set helper cell sizes to match the original sizes
$(this).width($originals.eq(index).width());
});
return $helper;
},
start: function (e, ui) {
},
update: function (e, ui) {
},
receive: function (e, ui) {
console.log("Users1 -- Users2")
},
axis: "xy",
});
Receive event only fires when item comes from other table. I also need to detect when item comes only from the same table. Any ideas? See demo