【问题标题】:Jquery drag/drop copies cell content instead of moving itJquery拖放复制单元格内容而不是移动它
【发布时间】:2013-04-15 19:26:10
【问题描述】:

我有 2 张桌子:

  • 表 1 包含静态数据,可以将其复制到表 2 中的任何位置。
  • 表 2 可以通过从表 1 复制的数据或通过在此表中移动数据来更新。

一切正常,除了移动。它在我拖放时复制内容。

有人知道吗?

这里可以看看online sample

【问题讨论】:

    标签: jquery cell draggable droppable


    【解决方案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>');
            }
        }
    });
    

    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多