Greetings mighty java scripters!
I have an HTML application that I'd like to incorporate drag and drop functionality. Since this is the first time I've tried this and I didn't want muck up my app, I've created this test html page to expirament a bit.
Here is the code, and at the bottom are my questions:
Ok. If you run this code (yes, I know it's IE only, but that's ok for now), you'll see that if you click on a cell, a new box apears that you can "drag" around the screen. I want to be able to drop that box somewhere else in the table and have the cell I "grabed" switch with the cell where I dropped it. Make sense? I just want to switch two cells via drag and drop.
My problem here is that because I've "grabing" a DIV, the mouse over events for the table behind the box do not fire. Thus, I have no way of knowing where they are droping the box. I do know what the coordinates of the mouse are, and I suppose I could calculate what cell they are hovering over based on those coordinates, but that would get pretty complicated if I have different sized cells - not to mention that the table might be at an unknown location on the screen and the cells might resize.
Is there a way to grab the coordinates of a specific cell? Or would it be best if instead of rendering a table, that I make a table out of DIVs? (I don't really want to do that, but it is an option).
I hope I've been clear enough. Do you guys have any spiffy ideas here?
---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
I have an HTML application that I'd like to incorporate drag and drop functionality. Since this is the first time I've tried this and I didn't want muck up my app, I've created this test html page to expirament a bit.
Here is the code, and at the bottom are my questions:
Code:
<HTML>
<HEAD>
<STYLE>
TR {
height: 200px;
}
TD {
text-align: center;
vertical-align: middle;
width: 200px;
}
.testTable {
left: 0px;
position: absolute;
top: 0px;
}
.log {
border: 1px solid #000000;
height: 100px;
left: 0px;
overflow: auto;
position: absolute;
top: 620px;
width: 700px;
}
.dragCell {
background-color: #555555;
border: 1px solid #000000;
height: 200px;
filter: alpha(opacity=55);
left: 0px;
opacity: 0.05;
position: absolute;
top: 0px;
width: 200px;
Z-Index: 2;
}
</STYLE>
<SCRIPT type=text/javascript>
var clickedCell = null;
function page_onLoad(){
document.getElementById("dragCell").style.display = "NONE";
initTable();
}
function generic_onMouseDown(cell){
document.getElementById("dragCell").style.display = "BLOCK";
document.getElementById("dragCell").style.left = event.clientX - 100;
document.getElementById("dragCell").style.top = event.clientY - 100;
cell.style.backgroundColor = "#999999";
clickedCell = cell;
writeLog("mouse down, " + cell.x);
}
function generic_onMouseUp(){
if(clickedCell != null){
document.getElementById("dragCell").style.display = "NONE";
clickedCell.style.backgroundColor = "#FFFFFF";
clickedCell = null;
}
writeLog("mouse up");
}
function generic_onMouseMove(){
if(clickedCell != null){
document.getElementById("dragCell").style.left = event.clientX - 100;
document.getElementById("dragCell").style.top = event.clientY - 100;
writeLog(document.getElementById("testTable").style.left);
writeLog(event.clientX + ", " + event.clientY);
}
}
function generic_onMouseOver(cell){
cell.style.borderStyle = "dotted";
}
function generic_onMouseOut(cell){
cell.style.borderStyle = "groove";
}
function initTable(){
var strHTML = "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>";
for(var intRow = 0; intRow < 3; intRow++){
strHTML += "<TR>";
for(var intCol = 0; intCol < 3; intCol++){
// strHTML += "<TD ID='cell_" + intRow + "_" + intCol + "' " +
// "onMouseDown='generic_onMouseDown(this)' " +
// "onMouseUp='generic_onMouseUp()' " +
// "onMouseMove='generic_onMouseMove()' >" + intRow + ", " + intCol + "</TD>";
strHTML += "<TD ID='cell_" + intRow + "_" + intCol + "' " +
"onMouseDown='generic_onMouseDown(this)' " +
"onMouseOver='generic_onMouseOver(this)' " +
"onMouseOut='generic_onMouseOut(this)' >" + intRow + ", " + intCol + "</TD>";
}
strHTML += "</TR>";
}
strHTML += "</TABLE>";
document.getElementById("testTable").innerHTML = strHTML;
}
function writeLog(strText){
document.getElementById("debugLog").innerHTML = Date() + " - " + strText + "<BR>" + document.getElementById("debugLog").innerHTML;
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="page_onLoad()" ONMOUSEUP="generic_onMouseUp()" ONMOUSEMOVE="generic_onMouseMove()">
<DIV ID="testTable" CLASS="testTable"></DIV>
<BR>
<DIV ID="debugLog" CLASS="log"></DIV>
<DIV ID="dragCell" CLASS="dragCell"></DIV>
</BODY>
</HTML>
My problem here is that because I've "grabing" a DIV, the mouse over events for the table behind the box do not fire. Thus, I have no way of knowing where they are droping the box. I do know what the coordinates of the mouse are, and I suppose I could calculate what cell they are hovering over based on those coordinates, but that would get pretty complicated if I have different sized cells - not to mention that the table might be at an unknown location on the screen and the cells might resize.
Is there a way to grab the coordinates of a specific cell? Or would it be best if instead of rendering a table, that I make a table out of DIVs? (I don't really want to do that, but it is an option).
I hope I've been clear enough. Do you guys have any spiffy ideas here?
---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon