JavaScript Drag and Drop example 6

More...

Example shows dragging functionality across scrollable DIV containers. If DIV container is bigger then displayed size, approaching element near to the edge will start to auto-scroll. Other dragging examples are placed in Drag and Drop section.


A
B
Clone element with SHIFT key


The whole magic lies in changing style position declaration. If position declaration isn't defined then element has default static style position. So, when DIV elements is clicked with left mouse button nothing will happen until element starts to move. In that moment, script will change style position from static to fixed. This change will take out DIV element from the normal flow of the page and positioned at the desired coordinates (relative to the browser window). Element will stay at that position regardless of scrolling - and that's desired effect.

Position style has four values: static, relative, absolute, and fixed. Their explanation can be read on very good site http://www.quirksmode.org/css/position.html - it's definitely recommended to visit.

For the first visitors
Example 6 (scrollable and fixed DIV containers) is based on REDIPS.drag library. Please visit Drag and drop table content with JavaScript where you will find more details, comments and source package to download.

This entry was posted on November 15, 2010 and is filed under Drag and Drop, JavaScript

Related posts

29 Responses to JavaScript Drag and Drop example 6

  1. dbunic says:

    When DIV element is dragged across table(s), REDIPS.drag will highlight TDs which can accept DIV element. So, when DIV element is dragged out of a table then last TD will stay highlighted. This is needed to enable dropping DIV element to the last possible TD if user releases button out of table boundaries (otherwise dropped DIV element will be lost).

    If I understood your question well, currently is not possible to delete DIV element by dropping DIV element out of a table.

  2. Adam says:

    Hello,
    when drag div to occupied cell, I would like to ask the user what he would like to do.
    Is it possible ?

  3. dbunic says:

    @Adam - When dropMode is set to "single" then is not possible to drop DIV element to the occupied TD. Instead, you can define dropMode to "multiple" and inside event.droppedBefore(targetCell) event handler write custom JS code. This code will be executed in moment when user releases left mouse button but before dropping DIV element to the target cell. If you return "false" from droppedBefore, then DIV element will be returned to the initial position.

    Input parameter of droppedBefore is target cell reference, so you can test whether cell is empty or not.

  4. Adam says:

    @dbunic - First thank you very much!! I made what you said, I test whether cell is empty or not, if is not empty, I open a jquery-ui menu and return false. More than that I store the target cell reference in another page (because when droppedBefore return false, and I store the reference in the same page it makes it undefined). How should I make the changes of the DIV in the chosen dropMode?

    thanks
    Adam

  5. Adam says:

    I've tried to call from the click event on the select mode to the

    rd.event.dropped = function (targetCell)
    

    but it's not working.

    Adam

  6. dbunic says:

    @Adam - REDIPS.drag is updated to version 5.0.3 (emptyCell method now has option to test target cell and to delete cell content) - this is needed for your case. Here is complete JS code in droppedBefore event handler:

    // event handler invoked before DIV element is dropped to the cell
    rd.event.droppedBefore = function (targetCell) {
        // test if target cell is occupied and set reference to the dragged DIV element
        var empty = rd.emptyCell(targetCell, 'test'),
            obj = rd.obj;
        // if target cell is not empty
        if (!empty) {
            // confirm question should be wrapped in setTimeout because of
            // removeChild and return false below
            setTimeout(function () {
                // ask user if he wants to overwrite TD (cell is already occupied)
                if (confirm('Overwrite content?')) {
                    rd.emptyCell(targetCell);
                }
                // append previously removed DIV to the target cell
                targetCell.appendChild(obj);
            }, 50);
            // remove dragged DIV from from DOM (node still exists in memory)
            obj.parentNode.removeChild(obj);
            // return false (deleted DIV will not be returned to source cell)
            return false;
        }
    };
    

    I also created jsfiddle where you can see demo in live.

    http://jsfiddle.net/bXXE5/2/

    If target cell is not empty, application will ask to confirm whether to overwrite or to append dropped DIV element. This should help you with jQuery popup dialog and possible actions in case of dropping content to non empty cell.

  7. Adam says:

    @dbunic - Thank you very very much!!!
    the last thing i would like to know is if can I ask the user i what drop position he want to chose - shift, switch or overwrite ?

    I mean I want to do this: If the cell is not empty i will show to the user a menu with 3 drop mode - shift, switch and overwrite and he will have to chose, when he will chose the drop will be fired in the mode the user was chose.

    please let me know if is it possible.

    Thanks!

  8. dbunic says:

    @Adam - dropMode in general can be set before dragging begins or in the early stage of drag process like event.clicked and event.moved hooks. If you want to set dropMode depending on cell content, then it's needed to use trick like is shown in jsfiddle demo:
    1) dropped DIV element should be temporary removed
    2) content in target cell should be deleted, switched or shifted in event.droppedBefore
    3) and place DIV element from the first step to the TD

    Nevertheless I have prepared example26: jQuery dialog with shift, switch and overwrite actions. So, answer to your question is yes, it's possible ;)

    I also had to make small modifications within enableDrag and relocate methods so please download latest redips2.tar.gz package (version 5.0.4).

  9. adam says:

    Cooool! thanks very very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

In case of posting HTML tags or JavaScript code please convert special characters to HTML entities.
Especially pay attention to convert "<" character to "&lt;" entity!