Jquery drag/drop копирует содержимое ячейки, а не перемещает его
У меня есть 2 таблицы:
- Таблица 1 содержит статические данные, которые можно скопировать в любое место таблицы 2.
- Таблица 2 может быть обновлена данными, скопированными из таблицы 1 ИЛИ, перемещая данные в этой таблице.
Все работает, кроме переезда. Копирует содержимое, когда я перетаскиваю.
Кто-нибудь понял?
Здесь вы можете посмотреть онлайн-образец:
1 ответ
Я решил проблему с обходным путем. Теперь я просто удаляю контент из оригинального TD. И я добавил дополнительную таблицу, чтобы служить мусорным ведром.
Онлайн пример делает все, что должен. Однако я не уверен, что это самый эффективный способ сделать это. Я довольно неопытен, если речь идет о JQuery.
Так что, если у кого-то есть более элегантное решение, которое делает то же самое, я очень хочу учиться:)
На всякий случай вот код JQ, который я использую:
jQuery(function($) {
var td1 = $("#table1 td");
var td2 = $("#table2 td");
var bin = $("#trash td");
td1.draggable({
cursor: "move",
appendTo: "body",
helper: "clone",
opacity: "0.5",
revert: "invalid"
});
td2.draggable({
cursor: "move",
appendTo: "body",
helper: "clone",
opacity: "0.5",
revert: "invalid"
});
td2.droppable({
accept: 'td',
tolerance: "pointer",
drop: function (event, ui) {
// check from which table we are dragging
var fromTable = $(ui.draggable).closest("table").attr("id");
// check from which td we are dragging
var fromTD = $(ui.draggable).attr("id");
// get the inner html content for the td we are dragging the div from
var cell = $(ui.draggable).html();
// insert the complete html content into the target drop cell
$(this).html(cell);
// for purposes of result logging / debugging
var location = $(this).attr('id');
$('#result').html('Moved '+cell+'<br>From '+fromTable+' / '+fromTD+' (table / td)<br>To cell: '+location+'<br>complete with containing DIV');
// in case we moved cell content within table2, remove the original content
if(fromTable == "table2"){
$(ui.draggable).html('<div></div>');
}
}
});
bin.droppable({
accept: 'td',
tolerance: "pointer",
drop: function (event, ui) {
// check if we are dragging from table 2
var fromTable = $(ui.draggable).closest("table").attr("id");
if(fromTable != "table2"){
$('#result').html('You cannot move content<br>from '+fromTable+' to the trash bin...');
return false;
}
else {
// check from which td we are dragging
var fromTD = $(ui.draggable).attr("id");
// get the inner html content for the td we are dragging the div from
var cell = $(ui.draggable).html();
// insert the complete html content into the target drop cell
$(this).html(cell);
// for purposes of result logging / debugging
var location = $(this).attr('id');
$('#result').html('Moved '+cell+'<br>From '+fromTable+' / '+fromTD+' (table / td)<br>To the trash bin');
// replace the content of the source td after drop
$(ui.draggable).html('<div></div>');
}
}
});
});