JavaScript Drag and Drop example 2
This post shows how to use Drag and Drop library described in my previous post Drag and drop table content with JavaScript. HTML table is stylized to have left column with content, main table and message line. Elements on the left side (green and orange) should be placed to the coloured table cells. After elements are correctly sited, appropriate message will be displayed. Please try ...
|
Green
|
||||||
|
Orange
|
||||||
| Set green and orange elements to the green and orange cells, respectively. | ||||||
Now follows HOWTO. First step is to include Drag and drop library from redips2.tar.gz package. Package contains source code (well commented) and compressed file drag-min.js ready for production.
<script type="text/javascript" src="/my/drag-min.js"></script>
And the second step is to initialize / configure Drag and Drop library. Only initialization is mandatory while other library properties have default values. You can see how to define destination table cells for green and orange elements. It's possible to define destination cells for each dragged element. At the beginning, left column contains green and orange elements with id="green" and id="orange". This elements are defined to clone two elements. Cloned elements will get id greenc0 / greenc1 and orangec0 / orangec1.
window.onload = function () {
var num = 0, // number of successfully placed elements
rd = REDIPS.drag; // reference to the REDIPS.drag class
// initialization
rd.init();
// set hover color
rd.hover_color = '#9BB3DA';
// define green elements for green cells
rd.mark.exception.green = 'green_cell';
rd.mark.exception.greenc0 = 'green_cell';
rd.mark.exception.greenc1 = 'green_cell';
// define orange elements for orange cells
rd.mark.exception.orange = 'orange_cell';
rd.mark.exception.orangec0 = 'orange_cell';
rd.mark.exception.orangec1 = 'orange_cell';
// this function (event handler) is called after element is dropped
REDIPS.drag.myhandler_dropped = function () {
var msg; // message text
// if the DIV element was placed on allowed cell then
if (rd.target_cell.className.indexOf(rd.mark.exception[rd.obj.id]) !== -1){
// make it a unmovable
rd.enable_drag(false, rd.obj.id);
// increase counter
num++;
// prepare and display message
if (num < 6) msg = 'Number of successfully placed elements: ' + num;
else msg = 'Well done!';
document.getElementById('message').innerHTML = msg;
}
}
}
Drag and Drop library has 11 event handlers:
- myhandler_clicked
- myhandler_moved
- myhandler_notmoved
- myhandler_dropped
- myhandler_switched
- myhandler_cloned
- myhandler_clonedend1
- myhandler_clonedend2
- myhandler_notcloned
- myhandler_deleted
- myhandler_undeleted
In this example you can see how to use myhandler_dropped event handler and how to test if element is dropped to the allowed table cell. Example also shows how to define single content table cell on the globally defined "multiple" drop option. Single content cells are in the corners of the main table, while "multiple" is default option and it hasn't be defined explicitly. Hope this post will give some ideas of how to use Drag an Drop library.
Related posts
- JavaScript Drag and Drop example 4
- JavaScript Drag and Drop example 3
- Drag and Drop table content with JavaScript
- Autocomplete without AJAX
- Image hover using Javascript
very cool & good js code, thank you very much for sharing.
Can you share this update on my JavaScript library?
Awaiting your response. Thanks
I will gladly post update to your JS library site ...
Excellent Code!
PS: Where's my badge?
Hi,
love this example. How would you edit this code so that the dragabble element can be cloned an infinite number of times?
@Marc Love - Please see "School timetable" JavaScript Drag and Drop example 3. Left table contains school subjects with infinity cloning possibility.
Hi...I have use your Example work very Well....But only one feature reuired is tht the controls of the two adjacent should be swapped ...
Suppose i have table with column 2 and i drag coloumn2 element to column1 thn both should swapp there position..
Plzzz Help me out with this .....