473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with dynamic table resizing code

7
Hello,

I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from http://blogs.crankygoblin.com/blogs/...ges/50712.aspx. I've also read the post on this topic by bggraphics, but he doesn't arrive at a final result. The main problem I am having is that the layerX and layerY event properties don't work. They're supposed to return the position of the mouse event with respect to the enclosing element. I've set the div element to be "position: absolute" in the css stylesheet so I don't know why this isn't working. I set up a textarea in the code that outputs the layerX value when you move over a table header to illustrate the problem.

Any help would be greatly appreciated.

My code is as follows:

/-----

Expand|Select|Wrap|Line Numbers
  1. var sResizableElement = "TH"; //Elements to be resized
  2. var iResizeThreshold = 8;
  3. var iEdgeThreshold = 8;
  4. var iSizeThreshold = 20;
  5. var sVBarID = "VBar";
  6.  
  7. var oResizeTarget = null; //position and distance moved
  8. var iStartX = null;
  9. var iEndX = null;
  10. var iSizeX = null;
  11. var oAdjacentCell = null;
  12.  
  13. //Creates VBar on load 
  14. function TableResize_CreateVBar() {
  15.     var objItem = document.getElementById(sVBarID);
  16.     if(!objItem) {
  17.         alert("VBar created");
  18.         objItem = document.createElement("SPAN");
  19.         objItem.id = sVBarID;
  20.         objItem.style.position = "absolute";
  21.         objItem.style.top = "0px";
  22.         objItem.style.left = "0px";
  23.         objItem.style.height = "0px";
  24.         objItem.style.width = "2px";
  25.         objItem.style.background = "silver";
  26.         objItem.style.borderleft = "1px solid black";
  27.         objItem.style.display = "none";
  28.         document.body.appendChild(objItem);
  29.         //alert(objItem);
  30.     }
  31. }
  32. window.addEventListener("load", TableResize_CreateVBar,0);
  33.  
  34. function TableResize_GetOwnerHeader(objReference) {
  35.     var oElement = objReference;
  36.     if(oElement.tagName.toUpperCase() == sResizableElement) {
  37.         return oElement; //print alert?
  38.     }
  39.     return null;
  40.  
  41. function TableResize_GetFirstColumnCell(objTable, iCellIndex) {
  42.     var oHeaderCell = objTable.rows[0].cells[iCellIndex];
  43.     return oHeaderCell;
  44. }
  45.  
  46. function TableResize_CleanUp() {
  47.     var oVBar = document.getElementById(sVBarID);
  48.     if(oVBar) {
  49.         oVBar.style.display = "none";
  50.     }
  51.     iEndX = null;
  52.     iSizeX = null;
  53.     iStartX = null;
  54.     oResizeTarget = null;
  55.     oAdjacentCell = null;
  56.     return true;
  57. }
  58.  
  59. function TableResize_OnMouseMove(objTable, event) {
  60.     document.getElementById('debug').value += 'Entered OnMouseMove\n';
  61.     var objTH = TableResize_GetOwnerHeader(event.target);
  62.     //var objTH = objTable.createTHead(objTable, event);
  63.     if(!objTH) return; 
  64.     document.getElementById('debug').value += '\tPASSED objTH\n';
  65.     var oVBar = document.getElementById(sVBarID); 
  66.     if (!oVBar) return;   
  67.     document.getElementById('debug').value += '\tPASSED oVBar\n';
  68.     var oAdjacentCell = objTH.nextSibling;
  69.  
  70.     alert("Testing movement");
  71.  
  72.     document.getElementById('coords').value += "Event.clientX " + event.clientX + "\n";
  73.     document.getElementById('coords').value += "Event.layerX " + event.layerX+ "\n";
  74.     document.getElementById('coords').value += "offsetWidth " + objTH.offsetWidth+ "\n";
  75.     //alert(event.clientX);
  76.     //alert(event.layerX);
  77.     //alert(objTH.innerHTML);
  78.     //alert(objTH.offsetWidth);
  79.     // Show the resize cursor if we are within the edge threshold. // && (oAdjacentCell != null)
  80.     if(event.layerX >= (objTH.offsetWidth - iEdgeThreshold)) {
  81.         objTH.style.cursor = "e-resize"; 
  82.     } else {
  83.         document.body.style.cursor = "pointer";  // Change this to ""
  84.     }
  85.     // We want to keep the right cursor if resizing and
  86.     // don't want resizing to select any text elements
  87.     if(oVBar.style.display == "inline") {
  88.         //oVBar.style.left = event.clientX + document.body.scrollLeft - document.body.clientLeft;
  89.         // We have to add the body.scrollLeft in case the table is wider than the view window
  90.         // where the table is entirely within the screen this value should be zero...
  91.         oVBar.style.left = event.clientX + window.pageXOffset;
  92.         document.body.focus(); //window.event?
  93.     }
  94.     return true;
  95. }
  96.  
  97. /*
  98. MouseDown event.
  99. This fills the globals with tracking information, and displays the
  100. vertical bar. This is only done if you are allowed to start resizing.
  101. */
  102. function TableResize_OnMouseDown(objTable, event) {
  103.     var oTargetCell = event.target; 
  104.     if(!oTargetCell) return; // event.currentTarget
  105.     var oVBar = document.getElementById(sVBarID); 
  106.     if(!oVBar) return;
  107.  
  108.     var oHeaderCell = TableResize_GetFirstColumnCell(objTable, oTargetCell.cellIndex);
  109.     if ((oHeaderCell.tagName.toUpperCase() == sResizableElement) && (oTargetCell.style.cursor == "e-resize")) { 
  110.         iStartX = event.screenX;
  111.         oResizeTarget = oHeaderCell;
  112.  
  113.         // Mark the table with the resize attribute and show the resizer VBar.
  114.         // We also capture all events on the table we are resizing because Internet
  115.         // Explorer sometimes forgets to bubble some events up.
  116.         // Now all events will be fired on the table we are resizing.
  117.         objTable.setAttribute("Resizing","true");
  118.         //objTable.setCapture();  // THIS IS NOT SUPPORTED IN NETSCAPE
  119.  
  120.         // We have to add the body.scrollLeft in case the table is wider than the view window
  121.         // where the table is entriely within the screen this value should be zero...
  122.         oVBar.style.left = event.clientX + window.pageXOffset;
  123.         oVBar.style.top = objTable.parentNode.offsetTop + window.pageYOffset;
  124.         oVBar.style.height = objTable.parentNode.offsetHeight;
  125.         oVBar.style.display = "inline"; //This is crucial
  126.     }
  127.     return true;
  128. }
  129.  
  130. /*
  131. MouseUp event.
  132. This finishes the resize.
  133. */
  134. function TableResize_OnMouseUp(objTable, event) {
  135.     //document.getElementById('debug').value += 'Entered OnMouseUp\n';
  136.     // Resize the column and its adjacent sibling if position and size are within threshold values
  137.     var oAdjacentCell = null;
  138.     var iAdjCellOldWidth = 0;
  139.     var iResizeOldWidth = 0;
  140.     if(iStartX != null && oResizeTarget != null) {
  141.         iEndX = event.screenX;
  142.         iSizeX = iEndX - iStartX;
  143.  
  144.         // Mark the table with the resize attribute for not resizing
  145.         objTable.setAttribute("Resizing", "false");
  146.         if ((oResizeTarget.offsetWidth + iSizeX) >= iSizeThreshold) {
  147.             if (Math.abs(iSizeX) >= iResizeThreshold) {
  148.                 if (oResizeTarget.nextSibling != null) {
  149.                     oAdjacentCell = oResizeTarget.nextSibling;
  150.                     iAdjCellOldWidth = (oAdjacentCell.offsetWidth);
  151.                 } else {
  152.                     oAdjacentCell = null;
  153.                 }
  154.                 iResizeOldWidth = (oResizeTarget.offsetWidth);
  155.                 oResizeTarget.style.width = iResizeOldWidth + iSizeX;
  156.                 if ((oAdjacentCell != null) && (oAdjacentCell.tagName.toUpperCase() == sResizableElement)) {
  157.                     oAdjacentCell.style.width = (((iAdjacentCellOldWidth - iSizeX) >= iSizeThreshold) ? (iAdjCellOldWidth - iSizeX):(oAdjacentCell.style.width = iSizeThreshold))
  158.                 }
  159.             }
  160.         } else {
  161.             oResizeTarget.style.width = iSizeThreshold;
  162.         }
  163.     }
  164.     TableResize_CleanUp();
  165.     return true;
  166. }
/------

Here is the HTML file:

/------

[HTML]<head>

<title>Table Test</title>

<!--<link rel="stylesheet" type="text/css" href="tabletest.css" /> -->
<style type="text/css">
.tablecontainer
{
position: absolute;
}

.mytable
{
table-layout: fixed;
}

.mytable TD, .mytable TH
{
border: solid 1px black;
width: 120px;
}

.mytable TH
{
background-color: #e0e0e0;
}
</style>

<script type="text/javascript" src="tableresize2.js"></script>

</head>

<body>

<div class="tablecontainer">

<table border="0" cellspacing="0" cellpadding="0" class="mytable"
onmousemove="TableResize_OnMouseMove(this,event)"
onmouseup="TableResize_OnMouseUp(this,event)"
onmousedown="TableResize_OnMouseDown(this,event)">
<tr>
<th>Column 1</th><th>Column 2</th><th>Column 3</th><th>Column 4</th><th>Column 5</th>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
<tr>
<td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
</tr>
</table>
<TEXTAREA ID="debug" rows="20" cols="20"> Debug text </TEXTAREA>
<TEXTAREA ID="coords" rows="20" cols="20"> Coords </TEXTAREA>
</div>
[/HTML]/-----
Jun 25 '07 #1
9 5252
pbmods
5,821 Expert 4TB
Heya, dli07.

I don't generally like to rely on layerX and layerY. What I would do is get the mouse coordinates relative to the top-left corner of the document, then subtract the top-left corner of your target element.

Since the top-left corner of the div is not likely to change, you can reliably save these values to a variable to save some CPU cycles.
Jun 26 '07 #2
dli07
7
Thanks so much for your advice pbmods. I actually found an alternative solution which is to set style="position:relative" for each of the th elements. This gets layerX to work for some reason.

Now I when I click on a th border I can see the e-resize bar and I can move it, but the new bar position is not recorded when I release my mouse. I have to click elsewhere in order to get the change to appear and even then, the resizing isn't correct. Apparently, there is some problem with onmouseup. I'm not sure if the onmousedown event is overriding the onmouseup event or if I am doing something else wrong with the firefox event model. Here's my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // resize.js
  3. var sResizableElement = "TH"; //Elements to be resized
  4. var iResizeThreshold = 8;
  5. var iEdgeThreshold = 8;
  6. var iSizeThreshold = 20;
  7. var sVBarID = "VBar";
  8.  
  9. var oResizeTarget = null; //position and distance moved
  10. var iStartX = null;
  11. var iEndX = null;
  12. var iSizeX = null;
  13. var oAdjacentCell = null;
  14.  
  15. //Creates VBar on load 
  16. function TableResize_CreateVBar() {
  17.     var objItem = document.getElementById(sVBarID);
  18.     if(!objItem) {
  19.         alert("VBar created");
  20.         objItem = document.createElement("SPAN");
  21.         objItem.id = sVBarID;
  22.         objItem.style.position = "absolute";
  23.         objItem.style.top = "0px";
  24.         objItem.style.left = "0px";
  25.         objItem.style.height = "0px";
  26.         objItem.style.width = "2px";
  27.         objItem.style.background = "silver";
  28.         objItem.style.borderleft = "1px solid black";
  29.         objItem.style.display = "none";
  30.         document.body.appendChild(objItem);
  31.         //alert(objItem);
  32.     }
  33. }
  34. window.addEventListener("load", TableResize_CreateVBar,0);
  35.  
  36. /*
  37. Returns a valid resizable element, even if it contains another element
  38. which was actually clicked otherwise it returns the top body element.
  39. */
  40. /* function TableResize_GetOwnerHeader(objReference) {
  41.     var oElement = objReference;
  42.     while(oElement != null && oElement.tagName != null && oElement.tagName != "BODY") {
  43.         if(oElement.tagName.toUpperCase() == sResizableElement) {
  44.             return oElement; //print alert?
  45.         }
  46.         oElement = oElement.parentNode;
  47.     }
  48.     return null;
  49. }  */
  50.  
  51. function TableResize_GetOwnerHeader(objReference) {
  52.     var oElement = objReference;
  53.     if(oElement.tagName.toUpperCase() == sResizableElement) {
  54.         return oElement; //print alert?
  55.     }
  56.     return null;
  57.  
  58. /*
  59. Find cell at column iCellIndex in the first row of the table
  60. needed because you can only resize a column from the first row.
  61. by using this, we can resize from any cell in the table if we want to.
  62. */
  63. function TableResize_GetFirstColumnCell(objTable, iCellIndex) {
  64.     //alert("Entered getFirstColumnCell");
  65.     //alert(iCellIndex);
  66.     //alert(objTable.rows[0])
  67.     //alert(objTable.rows[0].cells[iCellIndex]);
  68.     var oHeaderCell = objTable.rows[0].cells[iCellIndex];
  69.     //alert(oHeaderCell.innerHTML);
  70.     return oHeaderCell;
  71. }
  72.  
  73. function TableResize_CleanUp() {
  74.     document.getElementById('debug').value += 'Entered CleanUp\n';
  75.     var oVBar = document.getElementById(sVBarID);
  76.     if(oVBar) {
  77.         oVBar.style.display = "none";
  78.     }
  79.     iEndX = null;
  80.     iSizeX = null;
  81.     iStartX = null;
  82.     oResizeTarget = null;
  83.     oAdjacentCell = null;
  84.     return true;
  85. }
  86.  
  87. /*
  88. MouseMove event.
  89. On resizable table This checks if you are in an allowable 'resize start' position.
  90. It also puts the vertical bar (visual feedback) directly under the mouse cursor.
  91. The vertical bar may NOT be currently visible, that depnds on if you're resizing.
  92. */
  93. function TableResize_OnMouseMove(objTable, event) {
  94.     //document.getElementById('debug').value += 'Entered OnMouseMove\n';
  95.     var objTH = TableResize_GetOwnerHeader(event.target);
  96.     //var objTH = objTable.createTHead(objTable, event);
  97.     if(!objTH) return; 
  98.     //document.getElementById('debug').value += '\tPASSED objTH\n';
  99.     var oVBar = document.getElementById(sVBarID); 
  100.     if (!oVBar) return;   
  101.     //document.getElementById('debug').value += '\tPASSED oVBar\n';
  102.     var oAdjacentCell = objTH.nextSibling;
  103.  
  104.     //document.getElementById('coords').value += "event.clientX " + event.clientX + "\n";
  105.     //document.getElementById('coords').value += "event.layerX " + event.layerX + "\n";
  106.     //document.getElementById('coords').value += "offsetWidth " + objTH.offsetWidth+ "\n";
  107.     //alert(event.clientX);
  108.     //alert(event.layerX);
  109.     //alert(objTH.innerHTML);
  110.     //alert(objTH.offsetWidth);
  111.     // Show the resize cursor if we are within the edge threshold. // && (oAdjacentCell != null)
  112.     // This is the main thing that checks for whether the mousemoves will be recorded
  113.     if(event.layerX >= (objTH.offsetWidth - iEdgeThreshold)) {
  114.         objTH.style.cursor = "e-resize"; 
  115.     } else {
  116.         document.body.style.cursor = "";  // Change this to ""
  117.     }
  118.     // We want to keep the right cursor if resizing and
  119.     // don't want resizing to select any text elements
  120.     if(oVBar.style.display == "inline") {
  121.         //oVBar.style.left = event.clientX + document.body.scrollLeft - document.body.clientLeft;
  122.         // We have to add the body.scrollLeft in case the table is wider than the view window
  123.         // where the table is entirely within the screen this value should be zero...
  124.         oVBar.style.left = event.clientX + window.pageXOffset;
  125.         document.body.focus(); // WHAT DOES THIS DO?????
  126.     }
  127.     return true;
  128. }
  129.  
  130. /*
  131. MouseDown event.
  132. This fills the globals with tracking information, and displays the
  133. vertical bar. This is only done if you are allowed to start resizing.
  134. */
  135. function TableResize_OnMouseDown(objTable, event) {
  136.     document.getElementById('debug').value += 'Entered OnMouseDown\n';
  137.     var oTargetCell = event.target; 
  138.     //alert(oTargetCell.tagName);
  139.     //alert(oTargetCell.innerHTML);
  140.     if(!oTargetCell) return; // event.currentTarget
  141.     var oVBar = document.getElementById(sVBarID); 
  142.     if(!oVBar) return;
  143.  
  144. /*     if(oTargetCell.parentNode.tagName.toUpperCase() == sResizableElement) {
  145.         oTargetCell = oTargetCell.parentNode;
  146.     } */
  147.  
  148.     var oHeaderCell = TableResize_GetFirstColumnCell(objTable, oTargetCell.cellIndex);
  149.     //alert(oHeaderCell);
  150.     //alert(oHeaderCell.innerHTML);
  151.     //alert(oTargetCell.style.cursor);
  152.     if ((oHeaderCell.tagName.toUpperCase() == sResizableElement) && (oTargetCell.style.cursor == "e-resize")) { 
  153.         iStartX = event.screenX;
  154.         oResizeTarget = oHeaderCell;
  155.  
  156.         // Mark the table with the resize attribute and show the resizer VBar.
  157.         // We also capture all events on the table we are resizing because Internet
  158.         // Explorer sometimes forgets to bubble some events up.
  159.         // Now all events will be fired on the table we are resizing.
  160.         objTable.setAttribute("Resizing","true");
  161.         //objTable.setCapture();  // THIS IS NOT SUPPORTED IN NETSCAPE
  162.  
  163.         // We have to add the body.scrollLeft in case the table is wider than the view window
  164.         // where the table is entriely within the screen this value should be zero...
  165.         oVBar.style.left = event.clientX + window.pageXOffset;
  166.         //alert("clientX: " + oVBar.style.left);
  167.         document.getElementById('coords').value += 'clientX: ' + oVBar.style.left + '\n';
  168.         oVBar.style.top = objTable.parentNode.offsetTop + window.pageYOffset;
  169.         //alert("offsetTop: " + oVBar.style.top);
  170.         document.getElementById('coords').value += 'offsetTop: ' + oVBar.style.top + '\n';
  171.         oVBar.style.height = objTable.parentNode.offsetHeight;
  172.         //alert("offsetHeight: " + oVBar.style.height);
  173.         document.getElementById('coords').value += 'offsetHeight: ' + oVBar.style.height + '\n';
  174.         oVBar.style.display = "inline"; // THIS IS CRUCIAL
  175.     }
  176.     return true;
  177. }
  178.  
  179. /*
  180. MouseUp event.
  181. This finishes the resize.
  182. */
  183. function TableResize_OnMouseUp(objTable, event) {
  184.     document.getElementById('debug').value += 'Entered OnMouseUp\n';
  185.     // Resize the column and its adjacent sibling if position and size are within threshold values
  186.     var oAdjacentCell = null;
  187.     var iAdjCellOldWidth = 0;
  188.     var iResizeOldWidth = 0;
  189.     if(iStartX != null && oResizeTarget != null) {
  190.         iEndX = event.screenX;
  191.         iSizeX = iEndX - iStartX;
  192.  
  193.         // Mark the table with the resize attribute for not resizing
  194.         objTable.setAttribute("Resizing", "false");
  195.         if ((oResizeTarget.offsetWidth + iSizeX) >= iSizeThreshold) {
  196.             if (Math.abs(iSizeX) >= iResizeThreshold) {
  197.                 if (oResizeTarget.nextSibling != null) {
  198.                     oAdjacentCell = oResizeTarget.nextSibling;
  199.                     iAdjCellOldWidth = (oAdjacentCell.offsetWidth);
  200.                 } else {
  201.                     oAdjacentCell = null;
  202.                 }
  203.                 iResizeOldWidth = (oResizeTarget.offsetWidth);
  204.                 oResizeTarget.style.width = iResizeOldWidth + iSizeX;
  205.                 if ((oAdjacentCell != null) && (oAdjacentCell.tagName.toUpperCase() == sResizableElement)) {
  206.                     oAdjacentCell.style.width = (((iAdjacentCellOldWidth - iSizeX) >= iSizeThreshold) ? (iAdjCellOldWidth - iSizeX):(oAdjacentCell.style.width = iSizeThreshold))
  207.                 }
  208.             }
  209.         } else {
  210.             oResizeTarget.style.width = iSizeThreshold;
  211.         }
  212.     }
  213.     TableResize_CleanUp();
  214.     return true;
  215. }
Also, out of curiosity, how do I set my code to be javascript instead of text in the CODE tags? Someone else fixed this for me in my first post.
Jun 26 '07 #3
pbmods
5,821 Expert 4TB
Heya, dli07.

Also, out of curiosity, how do I set my code to be javascript instead of text in the CODE tags? Someone else fixed this for me in my first post.
Use [code=javascript]. http://www.thescripts.com/forum/faq....ask_a_question

-----

I don't see any removeEventListener() calls in there. How does your script know when you're done dragging the bar? Are you using a global variable?
Jun 26 '07 #4
dli07
7
Hey pbmods,

I'm trying to convert this code from IE so I'm still figuring out how it works: http://blogs.crankygoblin.com/blogs/...ges/50712.aspx. I'm also new to javascript.

Do you mean that I should call removeEventListener("mouseup", TableResize_OnMouseUp, false) at the end of my TableResize_OnMouseUp code after TableResize_Cleanup? (this seems like an illegal call of a function within itself). If I remove the event listener, isn't it true that I won't be able to detect future mouseup events when I want to resize again?

In IE, the coder used releaseCapture and setCapture. I deleted these in the firefox version since I don't know their equivalent. These two functions are probably the source of the problems. I tested it out using alert statements and in the firefox version, the mouseup event is somehow triggered right after the mousedown event, while in the IE version, this is not the case. What do you suggest I do?

/------- A little while later

I think I see what you mean. The original code is too specific for the IE. I'll try to make some drastic changes for firefox.
Jun 26 '07 #5
pbmods
5,821 Expert 4TB
Heya, dli07.

I don't have a particularly long leg to stand on because the drag-and-drop library I wrote only works in Firefox. But, here's sort of the general flow of things:
  • Call Element::makeDraggable() to assign a mousedown event handler:
    • Clone the element and use a global variable to store it.
    • Hide the original element.
    • Attach a mousemove event handler to window:
      • Change the cloned element's position relative to the mouse cursor.
    • Attach a mouseup event to window:
      • Set original element's position.
      • Destroy cloned element and set global variable to null.
      • Remove window mousemove event handler.
      • Remove window mouseup event handler.

The idea is to remove event handlers whenever possible to avoid slowing down and/or confusing the browser.

Removing an event handler is not the same thing as deleting a function. When you remove the event handler, you are detaching the function from that object, but you can still attach the function to other objects (or even the object you just detached it from).

For an explanation of why this works, check out this article.

Incidentally, you can delete a function from within the function because of the way JavaScript handles variables. When you do this:
Expand|Select|Wrap|Line Numbers
  1. delete someVar;
you're not actually *deleting* the variable so much as deleting a reference to that variable.

The variable does get eventually deleted because the JavaScript engine will run 'garbage collection' and free up memory that nobody's using.

Keeping in mind that functions are also variables (objects) in JavaScript, you can do this:
Expand|Select|Wrap|Line Numbers
  1. function doSomething() {
  2.     delete doSomething;
  3. }
  4.  
When you call doSomething(), it will delete the variable ('reference') named doSomething (which happens to hold the function that's currently being evaluated). Note that we're not actually deleting the function; we're deleting the reference to that function. The function is still there until the JavaScript garbage collection process runs (which it won't do until the function is finished running).
Jun 26 '07 #6
pbmods
5,821 Expert 4TB
Here's a copy of my drag and drop library. It's not really finished, nor is it anywhere near what I want it to be, but I'm much more of a PHP guy than a JavaScript guy these days, so it will have to do.

Note that LB_DragAndDrop.js relies on LB_Core.js to function, which I am not releasing just yet (sorry; there's a piece of it available here, though). So it's not technically functional as is.

But maybe you'll find it useful.
Attached Files
File Type: txt LB_DragAndDrop.txt (7.1 KB, 435 views)
Jun 27 '07 #7
dli07
7
Hey pbmods,

Thanks for the advice. I've rewritten the code, but now I'm having problems with the mouseup event. The mousedown and mousemove events are recorded. But after I release the mouse, nothing happens; it seems that the mouseup event isn't recorded until after I click somewhere in the table. Does this have something to do with the way I'm adding the event listener? I added the listener for mouseup in TableResize_OnMouseDown, but I also added the listener for mousemove in TableResize_OnMouseDown so I don't know if they're interefering with each other?

Expand|Select|Wrap|Line Numbers
  1. function TableResize_OnMouseMoveBefore (objTable, event) {
  2.     //document.getElementById('debug').value += 'Entered OnMouseMoveBefore\n';
  3.     var objTH = TableResize_GetOwnerHeader(event.target); if(!objTH) return;  
  4.  
  5.     //document.getElementById('coords').value += "event.clientX " + event.clientX + "\n";
  6.     //document.getElementById('coords').value += "event.layerX " + event.layerX + "\n";
  7.     //document.getElementById('coords').value += "offsetWidth " + objTH.offsetWidth+ "\n";
  8.     if (event.layerX >= (objTH.offsetWidth - iEdgeThreshold)) {
  9.         objTH.style.cursor = "e-resize"; 
  10.     }
  11.     else {
  12.         objTH.style.cursor = "";
  13.     }
  14.     document.getElementById("table1").addEventListener("mousedown", TableResize_OnMouseDown, false);
  15.     event.stopPropagation();
  16. }
  17.  
  18. function TableResize_OnMouseDown(event) {
  19.     document.getElementById('debug').value += 'Entered OnMouseDown\n';
  20.     var objTable = this;
  21.     var objTH = TableResize_GetOwnerHeader(event.target); if (!objTH) return;
  22.     var oVBar = document.getElementById(sVBarID); if(!oVBar) return;
  23.     var oTargetCell = event.target; if(!oTargetCell) return; // event.currentTarget
  24.     var oHeaderCell = TableResize_GetFirstColumnCell(objTable, oTargetCell.cellIndex);
  25.     // var oAdjacentCell = objTH.nextSibling.nextSibling;
  26.     // alert(objTH.innerHTML);
  27.     // alert(oAdjacentCell.innerHTML);
  28.  
  29.     if ((oHeaderCell.tagName.toUpperCase() == sResizableElement) && (oTargetCell.style.cursor == "e-resize")) { 
  30.         iStartX = event.screenX;
  31.         oResizeTarget = oHeaderCell;
  32.  
  33.         // Mark the table with the resize attribute and show the resizer VBar.
  34.         // We also capture all events on the table we are resizing because Internet
  35.         // Explorer sometimes forgets to bubble some events up.
  36.         // Now all events will be fired on the table we are resizing.
  37.         objTable.setAttribute("Resizing","true");
  38.         //objTable.setCapture();  // THIS IS NOT SUPPORTED IN NETSCAPE
  39.  
  40.         oVBar.style.left = event.clientX + window.pageXOffset;
  41.         //alert("clientX: " + oVBar.style.left);
  42.         //document.getElementById('coords').value += 'clientX: ' + oVBar.style.left + '\n';
  43.         oVBar.style.top = objTable.parentNode.offsetTop + window.pageYOffset;
  44.         //alert("offsetTop: " + oVBar.style.top);
  45.         //document.getElementById('coords').value += 'offsetTop: ' + oVBar.style.top + '\n';
  46.         oVBar.style.height = objTable.parentNode.offsetHeight;
  47.         //alert("offsetHeight: " + oVBar.style.height);
  48.         //document.getElementById('coords').value += 'offsetHeight: ' + oVBar.style.height + '\n';
  49.         oVBar.style.display = "inline"; // THIS IS CRUCIAL
  50.         document.getElementById("table1").addEventListener("mouseup", TableResize_OnMouseUp, true);
  51.         document.getElementById("table1").addEventListener("mousemove", TableResize_OnMouseMoveAfter, true);  
  52.         event.stopPropagation();
  53.     }
  54. }
  55.  
  56.  
  57. function TableResize_OnMouseMoveAfter(event) {
  58.     document.getElementById('debug').value += 'Entered OnMouseMoveAfter\n';
  59.     var objTH = TableResize_GetOwnerHeader(event.target); if (!objTH) return;
  60.     var oVBar = document.getElementById(sVBarID); if (!oVBar) return;   
  61.  
  62.     if (oVBar.style.display == "inline") {
  63.         oVBar.style.left = event.clientX + window.pageXOffset;
  64.         document.getElementById("coords").value += oVBar.style.left + "\n";
  65.         //document.body.focus(); // WHAT DOES THIS DO?????
  66.     }
  67.     event.stopPropagation();
  68. }
  69.  
  70. function TableResize_OnMouseUp(event) {
  71.     document.getElementById('debug').value += 'Entered OnMouseUp\n';
  72.     // Resize the column and its adjacent sibling if position and size are within threshold values
  73.     //var oAdjacentCell = null;
  74.     var iAdjCellOldWidth = 0;
  75.     var iResizeOldWidth = 0;
  76.     var objTable = this;
  77.     var oVBar = document.getElementById(sVBarID); //if (!oVBar) return; 
  78.     oVBar.style.display == "none";
  79.  
  80.     alert(oResizeTarget);
  81.     var oAdjacentCell = oResizeTarget.nextSibling.nextSibling;
  82.     alert(oAdjacentCell);
  83.  
  84.  
  85.     //alert(oResizeTarget);
  86.     /*
  87.     if(iStartX != null && oResizeTarget != null) {
  88.         iEndX = event.clientX;
  89.         //alert(event.clientX);
  90.         iSizeX = iEndX - iStartX;
  91.  
  92.         // Mark the table with the resize attribute for not resizing
  93.         objTable.Resizing = false;
  94.         if ((oResizeTarget.offsetWidth + iSizeX) >= iSizeThreshold)
  95.         {
  96.             if (Math.abs(iSizeX) >= iResizeThreshold) 
  97.             {
  98.                 if (oResizeTarget.nextSibling != null) 
  99.                 {
  100.                     oAdjacentCell = oResizeTarget.nextSibling;
  101.                     iAdjCellOldWidth = (oAdjacentCell.offsetWidth);
  102.                 } else {
  103.                     oAdjacentCell = null;
  104.                 }
  105.                 iResizeOldWidth = (oResizeTarget.offsetWidth);
  106.                 oResizeTarget.style.width = iResizeOldWidth + iSizeX;
  107.                 if ((oAdjacentCell != null) && (oAdjacentCell.tagName.toUpperCase() == sResizableElement))
  108.                 {
  109.                     oAdjacentCell.style.width = (((iAdjacentCellOldWidth - iSizeX) >= iSizeThreshold) ? (iAdjCellOldWidth - iSizeX):(oAdjacentCell.style.width = iSizeThreshold))
  110.                 }
  111.             }
  112.         } else {
  113.             oResizeTarget.style.width = iSizeThreshold;
  114.         }
  115.     }*/
  116.     TableResize_CleanUp();
  117.     document.getElementById("table1").removeEventListener("mousemove", TableResize_OnMouseMoveAfter, true);
  118.     document.getElementById("table1").removeEventListener("mouseup", TableResize_OnMouseUp, true);
  119.     event.stopPropagation();
  120. }
  121.  
I wrote this problem rather hastily, so if you have any questions about what I mean, then please respond.
Jun 27 '07 #8
dli07
7
Apparently, if I delete the oVBar.style.height variable value, then everything works. It's just that I can't see the bar anymore, but at least the code stops.

If I set the default height in createVBar to something else, I still get the same problem. However, in this case, I have to click outside the height of the bar in order to get it to call mouseup. This is very odd.
Jun 27 '07 #9
dli07
7
I've gotten a crude version of the table resizing firefox code to work. The problem is that each reset adds a little to the entire table width. I'm posting it so that other people can look at it and use it in their code or make suggestions or improvements. **** (email removed)

Expand|Select|Wrap|Line Numbers
  1. var resizeElement = "TH"; //Elements to be resized
  2. var edgeThreshold = 2;
  3. var rBarID = "rBar";
  4.  
  5. var resizeTarget = null; //position and distance moved
  6. var iStartX = null;
  7. var iEndX = null;
  8. var iSizeX = null;
  9. var adjacentCell = null;
  10.  
  11. //Creates rBar on load 
  12. function TableResize_CreaterBar() {
  13.     var objItem = document.getElementById(rBarID);
  14.     if(!objItem) {
  15.         objItem = document.createElement("DIV");
  16.         objItem.id = rBarID;
  17.         objItem.style.position = "absolute";
  18.         objItem.style.top = "0px";
  19.         objItem.style.left = "0px";
  20.         objItem.style.height = "100px";
  21.         objItem.style.border = "1px solid black";
  22.         objItem.style.display = "none";
  23.         document.body.appendChild(objItem);
  24.     }
  25. }
  26. window.addEventListener("load", TableResize_CreaterBar,0);
  27.  
  28. function TableResize_GetHeader(objReference) {
  29.     var oElement = objReference;
  30.     if(oElement.tagName.toUpperCase() == resizeElement) {
  31.         return oElement; //print alert?
  32.     }
  33.     return null;
  34. }
  35.  
  36. function TableResize_CleanUp() {
  37.     //document.getElementById('debug').value += 'Entered CleanUp\n';
  38.     var rBar = document.getElementById(rBarID);
  39.     if(rBar) {
  40.         rBar.style.display = "none";
  41.     }
  42.     iEndX = null;
  43.     iSizeX = null;
  44.     iStartX = null;
  45.     resizeTarget = null;
  46.     adjacentCell = null;
  47.     return true;
  48. }
  49.  
  50. function TableResize_OnMouseMoveBefore (objTable, event) {
  51.     //document.getElementById('debug').value += 'Entered OnMouseMoveBefore\n';
  52.     var objTH = TableResize_GetHeader(event.target); if (!objTH ) return;
  53.  
  54.     //document.getElementById('coords').value += "event.clientX " + event.clientX + "\n";
  55.     //document.getElementById('coords').value += "event.layerX " + event.layerX + "\n";
  56.     //document.getElementById('coords').value += "offsetWidth " + objTH.offsetWidth+ "\n";
  57.     if (event.layerX >= (objTH.offsetWidth - edgeThreshold)) {
  58.         objTH.style.cursor = "e-resize";
  59.     }
  60.     else {
  61.         objTH.style.cursor = "";
  62.     }
  63.     return true;
  64. }
  65.  
  66. ///// HERE"S THE IMPORTANT CODE
  67. // In order to use layerX, each TH must have style="position: relative";
  68.  
  69. function TableResize_OnMouseDown(objTable, event) {
  70.     //document.getElementById('debug').value += 'Entered OnMouseDown\n';
  71.     var objTH = TableResize_GetHeader(event.target); if (!objTH) return;
  72.     var rBar = document.getElementById(rBarID); if(!rBar) return;
  73.     // adjacentCell = objTH.nextSibling.nextSibling;
  74.     // alert(objTH.innerHTML);
  75.     // alert(adjacentCell.innerHTML);
  76.  
  77.     if ((objTH.tagName.toUpperCase() == resizeElement) && (objTH.style.cursor == "e-resize")) { 
  78.         iStartX = event.clientX;
  79.         resizeTarget = objTH;
  80.  
  81.         rBar.style.left = event.clientX + window.pageXOffset;
  82.         rBar.style.top = objTable.parentNode.offsetTop + window.pageYOffset;
  83.         rBar.style.height = objTable.parentNode.clientHeight;
  84.         rBar.style.display = "inline"; // THIS IS CRUCIAL
  85.     }
  86.     document.getElementById("tabulated_data").addEventListener("mousemove", TableResize_OnMouseMoveAfter, true);  
  87.     document.getElementById("tabulated_data").addEventListener("mouseup", TableResize_HeaderProblem, true); //this handles a problem with clickling on the header and then not exiting OnMouseMoveAfter
  88.     document.getElementById(rBarID).addEventListener("mouseup", TableResize_OnMouseUp, true);
  89. }
  90.  
  91. function TableResize_HeaderProblem (event) {
  92.     //document.getElementById('debug').value += 'Entered HeaderProblem\n';
  93.     document.getElementById("tabulated_data").removeEventListener("mousemove", TableResize_OnMouseMoveAfter, true);
  94. }
  95.  
  96. // doesn't seem like I ever enter OnMouseAfter
  97.  
  98. function TableResize_OnMouseMoveAfter(event) {
  99.     //document.getElementById('debug').value += 'Entered OnMouseMoveAfter\n';
  100.     rBar.style.left = event.clientX + window.pageXOffset;
  101.     //document.getElementById("coords").value += rBar.style.left + "\n"; 
  102.     event.stopPropagation();
  103. }
  104.  
  105. function TableResize_OnMouseUp(event) {
  106.     //document.getElementById('debug').value += 'Entered OnMouseUp\n';
  107.     var iAdjCellOldWidth = 0;
  108.     var iResizeOldWidth = 0;
  109.     var objTable = this;
  110.     var rBar = document.getElementById(rBarID); 
  111.  
  112.     //resize target is already defined in onmousedown
  113.     var adjacentCell = resizeTarget.nextSibling.nextSibling;
  114.     //alert(adjacentCell);
  115.  
  116.     //--------------------------------------
  117.     // set the width on resizeTarget
  118.     // the distance that resizeTarget moves is iStartX+iEndX
  119.     var iEndX = event.clientX;
  120.     // iStartX is already defined
  121.     var distanceMoved = iEndX - iStartX;
  122.     var newPosition = iStartX + distanceMoved;
  123.     var oldCellWidth = resizeTarget.offsetWidth;
  124.     //document.getElementById("coords").value += "iStartX " + iStartX + "\n";
  125.     //document.getElementById("coords").value += "iEndX " + iEndX + "\n";
  126.     //document.getElementById("coords").value += "oldCellWidth " + oldCellWidth + "\n"
  127.     resizeTarget.style.width = oldCellWidth + distanceMoved;
  128.     //document.getElementById("coords").value += "newCellWidth " + newCellWidth + "\n"; 
  129.  
  130.     //-------------------------------------
  131.     // set the width on oAdjacent Cell
  132.     // the distance that the adjacentCell moves is 
  133.     var oldAdjacentWidth = adjacentCell.offsetWidth;
  134.     adjacentCell.style.width = oldAdjacentWidth - distanceMoved;
  135.  
  136.     document.getElementById("tabulated_data").removeEventListener("mousemove", TableResize_OnMouseMoveAfter, true);
  137.     document.getElementById(rBarID).removeEventListener("mouseup", TableResize_OnMouseUp, true);
  138.     event.stopPropagation();
  139.     TableResize_CleanUp();
  140. }
  141.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE HTML PUBLIC   "-//W3C//DTD HTML 4.0 Transitional//EN">
  3.  
  4. <html>
  5.  
  6. <head>
  7.  
  8. <title>Table Test</title>
  9.  
  10. <!-- <link rel="stylesheet" type="text/css" href="tabletest.css" />  -->
  11. <style type="text/css">
  12. .tablecontainer
  13. {
  14.     position: absolute;
  15.     width: 100%;
  16.     display: block; 
  17. }
  18.  
  19. .mytable
  20. {
  21.     table-layout: fixed; 
  22.     width: 100%;    
  23. }
  24.  
  25. .mytable TD, .mytable TH
  26. {
  27.     position: relative;
  28.     border: solid 1px black;
  29.     width: 120px;
  30. }
  31.  
  32. .mytable TH
  33. {
  34.     background-color: #e0e0e0;   
  35. }
  36. </style>
  37.  
  38. <script type="text/javascript" src="tableresize4.js">
  39.  
  40. </script>
  41.  
  42. </head>
  43.  
  44. <body>
  45.  
  46. <div class="tablecontainer" > 
  47.     <table id="tabulated_data" border="0" cellspacing="0" cellpadding="0" class="mytable"
  48.     onmousemove="TableResize_OnMouseMoveBefore(this, event);" 
  49.     onmousedown="TableResize_OnMouseDown(this, event);">
  50.         <tr>
  51.             <th>Column 1</th>
  52.             <th>Column 2</th>
  53.             <th>Column 3</th>
  54.             <th>Column 4</th>
  55.             <th>Column 5</th>
  56.         </tr>
  57.         <tr>
  58.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  59.         </tr>       
  60.         <tr>
  61.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  62.         </tr>       
  63.         <tr>
  64.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  65.         </tr>       
  66.         <tr>
  67.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  68.         </tr>       
  69.         <tr>
  70.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  71.         </tr>       
  72.         <tr>
  73.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  74.         </tr>       
  75.         <tr>
  76.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  77.         </tr>       
  78.         <tr>
  79.             <td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td><td>Some Data</td>
  80.         </tr>            
  81.     </table>
  82.     <TEXTAREA ID="debug" rows="20" cols="30"> </TEXTAREA>
  83.     <TEXTAREA ID="coords" rows="20" cols="30"> </TEXTAREA>
  84. </div>
  85.  
  86. </body>
  87.  
  88. </html>
  89.  
Jun 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: john T | last post by:
Is there anyway to vertically center a html table using css in such a way it does not alter the html table. When I tryied it just screws up.
6
by: Robert J. O'Hara | last post by:
I'm one of those people who practices what some consider "dull" and others consider "elegantly conservative" page design. I appreciate good traditional typography and standards-compliant liquid...
0
by: TJ Talluto | last post by:
<facts> I have a "month calendar" that always displays exactly 42 days... and alongside is a vertical box that displays the detail (form fields) of any particular select event that appears on the...
18
by: chimalus | last post by:
I am using a table with no column widths specified, letting the table layout manager do its thing for figuring out the column widths, and this works just fine. Now I want to make the table...
3
by: PCgeek | last post by:
sorry moved this over to javascript forum, didn't mean to post 2x! Hi guys, I'm trying to put the finishing touches on my website and could really use some help on this particular issue. My page...
13
by: bgraphics2031 | last post by:
I'm trying to get this iframe to dynamically resize by dragging a vertical bar, but it's not working in Mozilla (It originally worked in IE but I've been trying to port it over). Any help will be...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.