在Over和Out上使用Draggable / Droppable行为的jQuery

时间:2013-07-31 20:25:04

标签: jquery html-table draggable behavior droppable

我真的需要你的帮助。 我一直在努力做到这一点,但我不能这样做..

jsfiddle:http://jsfiddle.net/5Vyq8/13/

JS-代码:

$(document).ready(function () {

    // Treetable
    $("table").treetable({
        expandable: true,
        initialState: "expanded",
        expanderTemplate: "<a href='#'>&nbsp;&nbsp;&nbsp;&nbsp;</a>",
        indent: 24,
        column: 0
    });

    // Draggable
    $("table .draggable").draggable({
        opacity: .75,
        refreshPositions: true,
        revert: "invalid",
        revertDuration: 300,
        scroll: true,
        delay: 100,
        cursor: 'move'
    });

    //Droppable
    $("table tbody tr").each(function () {
        $(this).droppable({
            accept: ".draggable",
            hoverClass: "append-to-task",
            over: function (e, ui) {         

                // add class 'accept-incoming-task' to the row under after 1 second
            },
            out: function () {

            },
            drop: function (e, ui) {

                var droppedEl = ui.draggable;
                // Adds the task as the first child to dropped row
                $("table").treetable("move", droppedEl.data("ttId"), $(this).data("ttId"));
            },
        });
    });
});

我想要实现的目标是:

  1. 将一行拖到另一行(完成!)
  2. 当悬停超过1秒时,它应该在
  3. 下的行中添加一个类
  4. 当仍然悬停并移动到其他行时,它应该删除上一步中添加的类
  5. 我赞美你的时间和帮助。

2 个答案:

答案 0 :(得分:1)

查看http://jsfiddle.net/snowMonkey/7yEaU/

$("table tbody tr").each(function () {
    var that=this;
    $(this).droppable({
        accept: ".draggable",
        over: function (e, ui) { 
            // add class 'accept-incoming-task' to the row under after 1 second
            hoverTimeout = setTimeout(function(){
                $(that).addClass("append-to-task");
            }, 1000);
        },
        out: function () {
           clearTimeout(hoverTimeout);
            $(that).removeClass("append-to-task");
        },
        drop: function (e, ui) {

            var droppedEl = ui.draggable;
            // Adds the task as the first child to dropped row
            $("table").treetable("move", droppedEl.data("ttId"), $(this).data("ttId"));
        },
    });
});

我让你的第二步工作。你需要做的第一件事是从你的悬停中删除hoverClass,你会在超时延迟后手动拉它。

其次,在此之外创建一个hoverTimeout var(因此您可以从hover和out回调中访问它)。

第三,在over:callback中,将hoverTimeout设置为1000ms,并在触发时添加该类。

最后,在out回调中,清除超时并删除该类。

它处理第二步和第三步 - 但它不允许你删除并将删除的项附加到捕手。希望它有所帮助!

答案 1 :(得分:0)

我终于设法解决了这个问题! 这是解决方案:

$(document).ready(function () {

    // Treetable
    $("table").treetable({
        expandable: true,
        initialState: "expanded",
        expanderTemplate: "<a href='#'>&nbsp;&nbsp;&nbsp;&nbsp;</a>",
        indent: 24,
        column: 0
    });

    // Draggable
    $("table .draggable").draggable({
        opacity: .75,
        refreshPositions: true,
        revert: "invalid",
        revertDuration: 300,
        scroll: true,
        delay: 100,
        cursor: 'move'
    });

    //Droppable
    var hoverTimeout;
    $("table tbody tr").each(function () {
        var that=this;
        $(this).droppable({
            accept: ".draggable",
            hoverClass: "append-to-task",
            over: function (e, ui) { 
                clearTimeout(hoverTimeout);
                $('.accept-incoming-task').removeClass('accept-incoming-task');
                // add class 'accept-incoming-task' to the row under after 1 second
                hoverTimeout = setTimeout(function(){
                    $(that).addClass("accept-incoming-task");
                }, 1000);
            },
            out: function () {

            },
            drop: function (e, ui) {
                $('.accept-incoming-task').removeClass('accept-incoming-task');
                var droppedEl = ui.draggable;
                // Adds the task as the first child to dropped row
                $("table").treetable("move", droppedEl.data("ttId"), $(this).data("ttId"));
            },
        });
    });
});

小提琴:http://jsfiddle.net/7yEaU/2/