473,226 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,226 developers and data experts.

Table - Columns drag & drop

I´ve seen some people around the web wondering about a code to reorder table columns using the mouse (drag and drop). Since i had already made one script to reorder table lines (TR´s) i decided to start working in one to reorder the columns. Im sharing this code with you all now. If anyone find any issue needing improvements just email-me and i will try to fix. The implementation is very easy, only change the ID of the table in the tabelaDrag variable and it should work.

ps:english is not my primary language so... :)

file: movecolumns.css

Expand|Select|Wrap|Line Numbers
  1. /* estilo geral da tabela */
  2. table {border-collapse:collapse}
  3. table td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer}
  4.  
  5. /* classe das cores utilizadas na movimentação */
  6. .hover {background:lightblue}
  7. .selecionado {background:lightgreen}
  8.  
file:movecolumns.js

Expand|Select|Wrap|Line Numbers
  1. //----------------------------------------------//
  2. //    Created by: Romulo do Nascimento Ferreira    //
  3. //    Email: romulo.nf@gmail.com                    //
  4. //----------------------------------------------//
  5.  
  6. // NOTICE: This code may be use dto any purpose without further
  7. // permission from the author. You may remove this notice from the
  8. // final code, however its appreciated if you keep the author name/email.
  9. // Email me if theres something needing improvement
  10.  
  11. //set the id of the table that is gonna have the 
  12. //moving column function in the tabelaDrag variable
  13. document.onmouseup = soltar;
  14. var drag = false;
  15.  
  16. window.onload = init
  17.  
  18. function init() {
  19. tabelaDrag = document.getElementById("tabela");
  20. linhas = tabelaDrag.getElementsByTagName("TR");
  21. celulas = tabelaDrag.getElementsByTagName("TD");
  22. linhaUm = tabelaDrag.rows[0]
  23. ordenacaoMaxima = linhaUm.cells.length
  24.  
  25.   tabelaDrag.onselectstart = function () { return false; } 
  26.   tabelaDrag.onmousedown = function () { return false; }
  27.  
  28.       for (x=0; x<celulas.length;x++) {
  29.         arrastar(celulas[x])
  30.         celulas[x].onmouseover = pintar
  31.         celulas[x].onmouseout = pintar
  32.     }
  33. }
  34.  
  35. function capturarColuna(obj) {
  36. coluna = obj.cellIndex
  37. return coluna
  38. }
  39.  
  40. function orderTd (obj) {
  41. destino = obj.cellIndex
  42.  
  43. if (destino == null) return
  44. if (coluna == destino) return
  45.  
  46.     for (x=0; x<linhas.length; x++) {
  47.     tds = linhas[x].cells
  48.     var celula = linhas[x].removeChild(tds[coluna])
  49.         if (destino >= ordenacaoMaxima || destino + 1 >= ordenacaoMaxima) {
  50.         linhas[x].appendChild(celula)
  51.         }
  52.         else {
  53.         linhas[x].insertBefore(celula, tds[destino])
  54.         }
  55.     }
  56. }
  57.  
  58. function soltar(e){
  59.     if (!e) e=window.event
  60.     if (e.target) targ = e.target
  61.     else if (e.srcElement) targ=e.srcElement
  62.     orderTd(targ)
  63.     drag = false
  64.  
  65.     for(x=0; x<linhas.length; x++) {
  66.         for (y=0; y<linhas[x].cells.length; y++) {
  67.         linhas[x].cells[y].className="";
  68.         }
  69.     }
  70. }
  71.  
  72. function arrastar(obj){
  73.     if(!obj) return;
  74.     obj.onmousedown = function(ev){
  75.         colunaAtual = this.cellIndex
  76.             for (x=0; x<linhas.length; x++) {
  77.             linhas[x].cells[this.cellIndex].className="selecionado"
  78.             }
  79.         drag = true
  80.         capturarColuna(this);
  81.         return false;
  82.     }
  83. }
  84.  
  85. function pintar(e) {
  86. if (!e) e=window.event
  87. ev = e.type
  88.  
  89.     if (ev == "mouseover") {
  90.         if (drag) {
  91.             for (x=0; x<linhas.length; x++) {
  92.                 if (this.className !="selecionado") {
  93.                 linhas[x].cells[this.cellIndex].className="hover"
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     else if (ev == "mouseout") {
  100.         for (x=0; x<linhas.length; x++) {
  101.             if (this.className !="selecionado") {
  102.             linhas[x].cells[this.cellIndex].className=""
  103.             }
  104.         }
  105.     }
  106. }
  107.  
  108.  
file:movecolumns.html

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5.     <title>Drag and Drop Columns</title>
  6.  
  7. <script type="text/javascript" src="movecolumn.js"></script>
  8.  
  9. <link rel="StyleSheet" href="movecolumn.css" type="text/css" />
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <table id="tabela">
  16.     <tr>
  17.         <td>Coluna 1</td>
  18.         <td>Coluna 2</td>
  19.         <td>Coluna 3</td>
  20.         <td>Coluna 4</td>
  21.         <td>Coluna 5</td>
  22.         <td>Coluna 6</td>
  23.         <td>Coluna 7</td>
  24.         <td>Coluna 8</td>
  25.         <td>Coluna 9</td>
  26.         <td>Coluna 10</td>
  27.     </tr>
  28.     <tr>
  29.         <td>A1</td>
  30.         <td>A2</td>
  31.         <td>A3</td>
  32.         <td>A4</td>
  33.         <td>A5</td>
  34.         <td>A6</td>
  35.         <td>A7</td>
  36.         <td>A8</td>
  37.         <td>A9</td>
  38.         <td>A10</td>
  39.     </tr>
  40.     <tr>
  41.         <td>B1</td>
  42.         <td>B2</td>
  43.         <td>B3</td>
  44.         <td>B4</td>
  45.         <td>B5</td>
  46.         <td>B6</td>
  47.         <td>B7</td>
  48.         <td>B8</td>
  49.         <td>B9</td>
  50.         <td>B10</td>
  51.     </tr>
  52.     <tr>
  53.         <td>C1</td>
  54.         <td>C2</td>
  55.         <td>C3</td>
  56.         <td>C4</td>
  57.         <td>C5</td>
  58.         <td>C6</td>
  59.         <td>C7</td>
  60.         <td>C8</td>
  61.         <td>C9</td>
  62.         <td>C10</td>
  63.     </tr>
  64.  
  65. </table>
  66.  
  67. </body>
  68.  
  69. </html>
  70.  
Nov 1 '06 #1
17 31425
Tested for macintosh:

Works in Firefox 1.5.0.6 for mac.
Works in Opera 9.01 for mac.
Does not work in Safari 2.0.4.

It's pretty nice, and simple. I might take a crack at debugging Safari and posting the results.
Dec 10 '06 #2
Great code.
But can you enhance it so that when a column is dragged(the selected column), it is visible when you hover over a different column.
Dec 12 '06 #3
Great code.
But can you enhance it so that when a column is dragged(the selected column), it is visible when you hover over a different column.
The code is actually just using some css to change colors. The selected column becomes green while the mouse is pressed and the place where it will drop is blue.

I think you are talking about making the whole column to be "floating" with the mouse cursor. To do that we need a function to get the mouse coords and make the element that we click and drag to use position:absolute and to follow these coords. The functionality will be the same in end.

An example of that function to move the elements:
Drag & Drop Example

ps: im supposing i can post that link here, sorry if i don´t!
Dec 13 '06 #4
The problem is that Safari does not (correctly) implement td.cellIndex. It always returns zero. Lame.

Change all foo.cellIndex to cellIndex(foo) and add these two methods:

Expand|Select|Wrap|Line Numbers
  1. function cellIndex(td) {
  2.     var ci = -1;
  3.     var parent_row = ascendDOM(td, 'tr');
  4.     for (var i = 0; i < parent_row.cells.length; i++) {
  5.         if (td === parent_row.cells[i]) {
  6.             ci = i;
  7.         }
  8.     }
  9.     return ci;
  10. }
  11.  
  12. function ascendDOM(e,target) {
  13.     while (e.nodeName.toLowerCase() !=target &&
  14.            e.nodeName.toLowerCase() !='html')
  15.         e=e.parentNode;
  16.     return (e.nodeName.toLowerCase() == 'html')? null : e;
  17. }
Feb 2 '07 #5
Thanks for the fix.
Feb 2 '07 #6
Here is a custom version, which highlight selected column to move, show insertion point as a vertical line (left cells border style update) during column drag (like firefox tab drag&drop) and keep original table styles. Seems to work on my stuff, hope it will help ;)

Expand|Select|Wrap|Line Numbers
  1. //----------------------------------------------//
  2. //    Created by: Romulo do Nascimento Ferreira    //
  3. //    Email: romulo.nf@gmail.com                    //
  4. //----------------------------------------------//
  5.  
  6. // NOTICE: This code may be use dto any purpose without further
  7. // permission from the author. You may remove this notice from the
  8. // final code, however its appreciated if you keep the author name/email.
  9. // Email me if theres something needing improvement
  10.  
  11.  
  12. var drag = false;
  13. var colOrder=[];
  14. var borderLeftStyle=[];
  15. var borderLeftColor=[];
  16. var cellBackgroundColor=[];
  17. var borderLeftWidth=[];
  18. var colNames=[];
  19.  
  20. function setTableColMovable(tableName,ColOrderFieldName,colNames) {
  21.   tabelaDrag = document.getElementById(tableName);
  22.   ColOrderField = document.getElementById(ColOrderFieldName);
  23.   linhas = tabelaDrag.getElementsByTagName("TR");
  24.   celulas = tabelaDrag.getElementsByTagName("TD");
  25.   linhaUm = tabelaDrag.rows[0]
  26.   ordenacaoMaxima = linhaUm.cells.length
  27.  
  28.   //store columns order
  29.   for (x=0;x<linhaUm.cells.length;x++) 
  30.     {
  31.       arrastar(celulas[x]);
  32.       celulas[x].onselectstart = function () { return false; } //prevent text selection
  33.       celulas[x].onmouseover = pintar
  34.       celulas[x].onmouseout = pintar
  35.       celulas[x].onmouseup = pintar
  36.     colOrder[x]=colNames[x];
  37.     linhaUm.cells[x].style.cursor="pointer";
  38.     }
  39.  
  40.     //store cells styles
  41.     for(x=0; x<linhas.length; x++) {
  42.       borderLeftStyle[x]=[];
  43.       borderLeftColor[x]=[];    
  44.       cellBackgroundColor[x]=[];
  45.       borderLeftWidth[x]=[];    
  46.         for (y=0; y<linhas[x].cells.length; y++) {
  47.          borderLeftStyle[x][y]=linhas[x].cells[y].style.borderLeftStyle;
  48.          borderLeftColor[x][y]=linhas[x].cells[y].style.borderLeftColor;
  49.          cellBackgroundColor[x][y]=linhas[x].cells[y].style.backgroundColor;
  50.          borderLeftWidth[x][y]=linhas[x].cells[y].style.borderLeftWidth;
  51.          }
  52.     }
  53.   }
  54.  
  55. function getColOrder()
  56.   {
  57.   return colOrder
  58.   }
  59.  
  60. function moveArrayCol(myArray,source,destination)
  61.   {
  62.   val=myArray.splice(source,1);
  63.   if (source < destination)
  64.     {myArray.splice(destination-1,0,val);}
  65.   else
  66.     {myArray.splice(destination,0,val);}
  67.   return myArray
  68.   }
  69.  
  70. function capturarColuna(obj) {
  71.   coluna = cellIndex(obj)
  72. return coluna
  73. }
  74.  
  75. function orderTd (obj) {
  76.   destino = cellIndex(obj)
  77.   if (destino == null) return
  78.   if (coluna == destino) return
  79.  
  80.   for (x=0; x<linhas.length; x++) 
  81.     {
  82.       tds = linhas[x].cells
  83.       var celula = linhas[x].removeChild(tds[coluna])
  84.           if (coluna < destino) 
  85.         {
  86.             linhas[x].insertBefore(celula, tds[destino-1])
  87.             }
  88.           else 
  89.         {
  90.             linhas[x].insertBefore(celula, tds[destino])
  91.             }
  92.       }
  93.   moveArrayCol(colOrder,coluna,destino);
  94.   //alert(colOrder);
  95. }
  96.  
  97. function soltar(e){
  98.     if (!e) e=window.event
  99.     if (e.target) targ = e.target
  100.     else if (e.srcElement) targ=e.srcElement
  101.     orderTd(targ)
  102.     drag = false
  103.  
  104.     for(x=0; x<linhas.length; x++) {
  105.         for (y=0; y<linhas[x].cells.length; y++) {
  106.         linhas[x].cells[y].style.borderLeftStyle=borderLeftStyle[x][y];
  107.         linhas[x].cells[y].style.borderLeftColor=borderLeftColor[x][y];
  108.         linhas[x].cells[y].style.backgroundColor=cellBackgroundColor[x][y];
  109.         linhas[x].cells[y].style.borderLeftWidth=borderLeftWidth[x][y];
  110.         }
  111.     }
  112. }
  113.  
  114. function arrastar(obj){
  115.     if(!obj) return;
  116.     obj.onmousedown = function(ev){
  117.         //colunaAtual = cellIndex(this))
  118.             for (x=0; x<linhas.length; x++) {
  119.             linhas[x].cells[cellIndex(this)].style.backgroundColor="yellow"
  120.             }
  121.         drag = true
  122.         capturarColuna(this);
  123.         return false;
  124.     }
  125. }
  126.  
  127. function pintar(e) {
  128. if (!e) e=window.event
  129. ev = e.type
  130.  
  131.     if (ev == "mouseover") {
  132.         if (drag) {
  133.             for (x=0; x<linhas.length; x++) {
  134.                 if (cellIndex(this) !=coluna) { //(this.style.borderStyle !="dashed") {
  135.                 linhas[x].cells[cellIndex(this)].style.borderLeftStyle="solid";
  136.                 linhas[x].cells[cellIndex(this)].style.borderLeftColor="red";
  137.                 linhas[x].cells[cellIndex(this)].style.borderLeftWidth="2px";
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     else if (ev == "mouseout") {
  144.             if (drag) 
  145.         {for (x=0; x<linhas.length; x++)  
  146.           {if (cellIndex(this) !=coluna) //(this.borderStyle !="dashed") 
  147.             {
  148.             linhas[x].cells[cellIndex(this)].style.borderLeftStyle=borderLeftStyle[x] [cellIndex(this)];
  149.             linhas[x].cells[cellIndex(this)].style.borderLeftColor=borderLeftColor[x] [cellIndex(this)];
  150.             linhas[x].cells[cellIndex(this)].style.backgroundColor=cellBackgroundColor[x] [cellIndex(this)];            
  151.             linhas[x].cells[cellIndex(this)].style.borderLeftWidth=borderLeftWidth[x] [cellIndex(this)];            
  152.             }
  153.           }
  154.         }
  155.   }
  156.  
  157.  
  158.     else if (ev == "mouseup") {
  159.       if (e.target) targ = e.target
  160.       else if (e.srcElement) targ=e.srcElement
  161.       orderTd(targ)
  162.       drag = false
  163.       //put columns new order in hidden field
  164.       ColOrderField.value=colOrder;
  165.       for(x=0; x<linhas.length; x++) {
  166.           for (y=0; y<linhas[x].cells.length; y++) {
  167.           linhas[x].cells[y].style.borderLeftStyle=borderLeftStyle[x][y];
  168.           linhas[x].cells[y].style.borderLeftColor=borderLeftColor[x][y];
  169.           linhas[x].cells[y].style.backgroundColor=cellBackgroundColor[x][y];
  170.           linhas[x].cells[y].style.borderLeftWidth=borderLeftWidth[x][y];
  171.           }
  172.       }
  173.    }
  174.  
  175. }
  176.  
  177. function cellIndex(td) {
  178.     var ci = -1;
  179.     var parent_row = ascendDOM(td, 'tr');
  180.     for (var i = 0; i < parent_row.cells.length; i++) {
  181.         if (td === parent_row.cells[i]) {
  182.             ci = i;
  183.         }
  184.     }
  185.     return ci;
  186. }
  187.  
  188. function ascendDOM(e,target) {
  189.     while (e.nodeName.toLowerCase() !=target &&
  190.            e.nodeName.toLowerCase() !='html')
  191.         e=e.parentNode;
  192.     return (e.nodeName.toLowerCase() == 'html')? null : e;
  193. }
  194.  
  195.  
Mar 10 '07 #7
I forgot to write that you need to call following function to activate DD on table, example :
setTableColMovable("my_table_id","one_html_field",[0,1,2,3]);

this call initiate table DD and also specify an HTML field (hidden by example) to store reordered columns numbers list. So initial column numbers are to be passed into last param (here [0,1,2,3]) in setTableColMovable call

hope it's clear cause it is time to launch ;)
Mar 10 '07 #8
Hello again folks!
I´ve received some emails about issues on the performance of the table (especially in ie) while working with big tables (30+ columns / 100+ lines) and i´ve worked a bit more into the script to improve it!

In the older version i was using classnames to give a visual effect of the column that is being moved and to where it was being moved, having to use some loops to change all classes and then change em back to the original afterwards! All those loops were causing an annoying delay!

There are still some changes to be done like: allowing more than 1 table in the same page to have the "drag & drop" implemented! I will work on that soon!

Email me with suggestions / issues so i can improve the script
I´ve tested it only under ie/firefox/mozilla/netscape, so some feedback from other browsers are welcome

call the script using:
allowDragging(tableId)


file: movecolumn.js

Expand|Select|Wrap|Line Numbers
  1. //----------------------------------------------//
  2. //    Created by: Romulo do Nascimento Ferreira    //
  3. //    Email: romulo.nf@gmail.com                    //
  4. //----------------------------------------------//
  5. //
  6. // Drag & Drop Columns
  7. //
  8. // NOTICE: This code may be use to any purpose without further
  9. // permission from the author. You may remove this notice from the
  10. // final code, however its appreciated if you keep the author name/email
  11. // Email me if there´s something needing improvement
  12.  
  13. var drag = false;
  14. document.onmouseup = release;
  15. document.onmousemove = mouseCoords;
  16.  
  17. function allowDragging(tableId) {
  18. dragTable = document.getElementById(tableId)
  19.  
  20. dragTableHandler = dragTable.getElementsByTagName("thead")[0] ? dragTable.getElementsByTagName("thead")[0].getElementsByTagName("tr")[0].cells : dragTable.getElementsByTagName("tr") ? dragTable.getElementsByTagName("tr")[0].cells : null
  21.  
  22. maxIndex = dragTableHandler.length
  23.  
  24. tableRows = dragTable.getElementsByTagName("tr");
  25.  
  26.     for (x=0; x<dragTableHandler.length; x++) {
  27.     makeDraggable(dragTableHandler[x])
  28.     dragTableHandler[x].onselectstart = function() { return false; }
  29.     }
  30.  
  31. dragTable.onmouseup = release;
  32. }
  33.  
  34. function makeDraggable(obj) {
  35.     if(!obj) return;
  36.     obj.onmousedown = function(ev){
  37.         if (drag == true) return
  38.         captureColumnIndex(this);    
  39.         createDraggedColumn(this)
  40.         drag = true
  41.         return false;
  42.     }
  43. }
  44.  
  45. function release(e) {
  46.     if (drag == false) return
  47.     if (!e) e=window.event
  48.     if (e.target) targ = e.target
  49.     else if (e.srcElement) targ=e.srcElement
  50.     orderTd(targ)
  51.     drag = false
  52.  
  53.     if (document.getElementById("drag")) {
  54.     corpo = document.getElementsByTagName("body")[0];
  55.     remover = document.getElementById("drag");
  56.     corpo.removeChild(remover)
  57.     }
  58. }
  59.  
  60. function captureColumnIndex(obj) {
  61. columnIndex = obj.cellIndex
  62. return columnIndex
  63. }
  64.  
  65. function orderTd(obj) {
  66. newIndex = obj.cellIndex
  67.  
  68. if (newIndex == null) return
  69. if (columnIndex == newIndex) return
  70.  
  71.     for (x=0; x<tableRows.length; x++) {
  72.     tds = tableRows[x].cells
  73.     var cell = tableRows[x].removeChild(tds[columnIndex])
  74.         if (newIndex >= maxIndex || newIndex + 1 >= maxIndex) {
  75.         tableRows[x].appendChild(cell)
  76.         }
  77.         else {
  78.         tableRows[x].insertBefore(cell, tds[newIndex])
  79.         }
  80.     }
  81. }
  82.  
  83. function createDraggedColumn(obj) {
  84.  
  85. draggedTable = document.createElement("table");
  86. draggedTable.id = "drag"
  87.  
  88. draggedThead = document.createElement("thead");
  89. draggedTheadTr = document.createElement("tr");
  90. draggedTheadTd = document.createElement("td");
  91. draggedTheadTd.innerHTML = tableRows[0].cells[columnIndex].innerHTML
  92. draggedTheadTr.appendChild(draggedTheadTd)
  93. draggedThead.appendChild(draggedTheadTr)
  94. draggedTable.appendChild(draggedThead)
  95.  
  96. draggedTbody = document.createElement("tbody");    
  97.  
  98. for (x=1; x<tableRows.length; x++) {
  99.     draggedTr = document.createElement("tr");
  100.     draggedTd = document.createElement("td");
  101.     draggedTd.innerHTML = tableRows[x].cells[columnIndex].innerHTML
  102.     draggedTr.appendChild(draggedTd)
  103.     draggedTbody.appendChild(draggedTr)
  104. }
  105.  
  106. draggedTable.appendChild(draggedTbody)
  107. draggedTable.style.filter = "alpha(opacity=70)";
  108. draggedTable.style.opacity = "0.7"
  109. draggedTable.style.mozOpacity = "0.7"
  110.  
  111. document.getElementsByTagName("body")[0].appendChild(draggedTable)
  112. draggedTable.style.top = posY
  113. draggedTable.style.left = posX
  114. }
  115.  
  116. function mouseCoords(e) {
  117. if (!e) e = window.event
  118.  
  119. if(e.pageY) {posX=e.pageX; posY=e.pageY;}
  120.  
  121. else if (e.clientY) {posX=e.clientX + ietruebody().scrollLeft; posY=e.clientY + ietruebody().scrollTop;}
  122.  
  123.     if (document.getElementById("drag")) {
  124.     dragTable = document.getElementById("drag");
  125.     dragTable.style.top = posY + 3 + "px"
  126.     dragTable.style.left = posX + 7 + "px"
  127.     }
  128.  
  129. return {x:posX, y:posY}
  130. }
  131.  
  132. function ietruebody(){
  133. return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
  134. }
  135.  
Apr 17 '07 #9
file: movecolumns.css

Expand|Select|Wrap|Line Numbers
  1. /* table general style */
  2. table {border-collapse:collapse}
  3. table td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer; font:bold 12px verdana;}
  4. table thead td {background:#1864E0; color:#fff;}
  5.  
  6. /* drag td style */
  7.  
  8. #drag {position:absolute; border:1px solid #000; text-align:center; cursor:pointer; font:bold 12px verdana; color:#fff;}
  9. #drag thead td {background:#1864E0; color:#fff;}
  10. #drag td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer; font:bold 12px verdana; color:#000}
  11.  
file: movecolumns.html

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5.     <title>Drag and Drop Columns</title>
  6.  
  7. <script type="text/javascript" src="movecolumn.js"></script>
  8.  
  9. <link rel="StyleSheet" href="movecolumn.css" type="text/css" />
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <table id="tabela">
  16.     <thead>
  17.         <tr>
  18.             <td>Column1</td>
  19.             <td>Column2</td>
  20.             <td>Column3</td>
  21.             <td>Column4</td>
  22.             <td>Column5</td>
  23.             <td>Column6</td>
  24.             <td>Column7</td>
  25.             <td>Column8</td>
  26.             <td>Column9</td>
  27.         </tr>
  28.     </thead>
  29.     <tbody>    
  30.         <tr>
  31.             <td>A1</td>
  32.             <td>A2</td>
  33.             <td>A3</td>
  34.             <td>A4</td>
  35.             <td>A5</td>
  36.             <td>A6</td>
  37.             <td>A7</td>
  38.             <td>A8</td>
  39.             <td>A9</td>            
  40.         </tr>
  41.         <tr>
  42.             <td>B1</td>
  43.             <td>B2</td>
  44.             <td>B3</td>
  45.             <td>B4</td>
  46.             <td>B5</td>
  47.             <td>B6</td>
  48.             <td>B7</td>
  49.             <td>B8</td>
  50.             <td>B9</td>            
  51.         </tr>
  52.         <tr>
  53.             <td>C1</td>
  54.             <td>C2</td>
  55.             <td>C3</td>
  56.             <td>C4</td>
  57.             <td>C5</td>
  58.             <td>C6</td>
  59.             <td>C7</td>
  60.             <td>C8</td>
  61.             <td>C9</td>            
  62.         </tr>
  63.         <tr>
  64.             <td>D1</td>
  65.             <td>D2</td>
  66.             <td>D3</td>
  67.             <td>D4</td>
  68.             <td>D5</td>
  69.             <td>D6</td>
  70.             <td>D7</td>
  71.             <td>D8</td>
  72.             <td>D9</td>            
  73.         </tr>
  74.         <tr>
  75.             <td>E1</td>
  76.             <td>E2</td>
  77.             <td>E3</td>
  78.             <td>E4</td>
  79.             <td>E5</td>
  80.             <td>E6</td>
  81.             <td>E7</td>
  82.             <td>E8</td>
  83.             <td>E9</td>            
  84.         </tr>
  85.         <tr>
  86.             <td>F1</td>
  87.             <td>F2</td>
  88.             <td>F3</td>
  89.             <td>F4</td>
  90.             <td>F5</td>
  91.             <td>F6</td>
  92.             <td>F7</td>
  93.             <td>F8</td>
  94.             <td>F9</td>            
  95.         </tr>        
  96.     </tbody>
  97. </table>
  98.  
  99. <script>
  100. allowDragging("tabela")
  101. </script>
  102.  
  103. </body>
  104.  
  105. </html>
  106.  
  107.  

ps: for some reason i couldnt put everything in 1 post
Apr 17 '07 #10
chami
1
FOR YOUR NEXT MODIFICATION, CONSIDER FOLLWINGS

MORE THAN ONE TABLES DRAG AND DROP CAPABILITY
SHIFT COLUMN BETWEEN TABLES

CHAMI.
Dec 15 '07 #11
drhowarddrfine
7,435 Expert 4TB
It should be noted that there are problems with the html shown above.

1) The doctype is incomplete and will throw IE into 'quirks mode' making it not behave like modern browsers. The correct doctype is discussed under the html Howto and this is the correct one to use:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2.    "http://www.w3.org/TR/html4/strict.dtd">
2) The <link> ends with a XHTML closing tag: /> . Since XHTML is not declared in the doctype, this may cause browsers to ignore some of the markup after it. (Perhaps causing the Safari problem?)

In any case, both of the items cause the page to be invalid.
Dec 16 '07 #12
If you got into this topic consider looking my new column drag & drop script
Dec 19 '07 #13
jigar1
2
Hello All,
I face 1 problem. this script working fine in both IE and mozilla
but i want to maintain the order of columns on page refresh so for that i set order in cookies and on page refresh i read that cookies order and pass that order in function but it return me our default order means in which order i have write my columns . can anyone plz help me.. my id is [email removed] . you can contact me here also.

Thanks in advance......
Feb 15 '09 #14
jigar1
2
Hello folks,
I face 1 problem. this script working fine in both IE and mozilla
but i want to maintain the order of columns on page refresh so for that i set order in cookies and on page refresh i read that cookies order and pass that order in function but it return me our default order means in which order i have write my columns . can anyone plz help me.. my id is [ removed email] . you can contact me here also. and i dont know your Email Id so can you plz tell me your email id.

Thanks in advance......
Feb 15 '09 #15
acoder
16,027 Expert Mod 8TB
I suggest you start a new thread in the Q&A section.
Feb 16 '09 #16
Hello! i'm Jamara and I'm with a problem: tryed implement your code, but I can't drag the columns for the last or first positions...Do you can help me??

Thank's!!
The code is this:


Expand|Select|Wrap|Line Numbers
  1. var drag = false; 
  2. document.onmouseup = release; 
  3. document.onmousemove = mouseCoords; 
  4.  
  5. function allowDragging(tableId) { 
  6.  
  7. var dragTable = document.getElementById(tableId); 
  8.  // alert("dragTable  :  "+ dragTable.getElementsByTagName("thead")[0]);
  9. dragTableHandler = dragTable.getElementsByTagName("thead")[0] ? dragTable.getElementsByTagName("thead")[0].getElementsByTagName("tr")[0].cells : dragTable.getElementsByTagName("tr") ? dragTable.getElementsByTagName("tr")[0].cells : null 
  10.  
  11. maxIndex = dragTableHandler.length 
  12.  
  13. tableRows = dragTable.getElementsByTagName("tr"); 
  14.  
  15.     for (x=0; x<dragTableHandler.length; x++) { 
  16.     makeDraggable(dragTableHandler[x]) 
  17.     dragTableHandler[x].onselectstart = function() { return false; } 
  18.     } 
  19.  
  20. dragTable.onmouseup = release; 
  21.  
  22. function makeDraggable(obj) { //diz que o evento do mouse deve produzir um movimento na coluna obj
  23.     if(!obj) return; 
  24.     obj.onmousedown = function(ev){ 
  25.         if (drag == true) return 
  26.         captureColumnIndex(this);     
  27.         createDraggedColumn(this) 
  28.         drag = true 
  29.        // alert(" captureColumnIndex em makeDraggable(): "+captureColumnIndex(this));         
  30.         return false; 
  31.     } 
  32.  
  33. function release(e) { 
  34.     if (drag == false) return 
  35.     if (!e) e=window.event 
  36.     if (e.target) targ = e.target 
  37.     else if (e.srcElement) targ=e.srcElement 
  38.     orderTd(targ) 
  39.     drag = false 
  40.     alert(" remover em release() : "+ document.getElementById("drag") );
  41.     alert("corpo em release() : "+ document.getElementsByTagName("body")[0] );
  42.     if (document.getElementById("drag")) { 
  43.     corpo = document.getElementsByTagName("body")[0]; 
  44.     remover = document.getElementById("drag"); 
  45.     corpo.removeChild(remover); 
  46.     } 
  47.  
  48. function captureColumnIndex(obj) { //insere na variavel columnIndex a posição em que ela se encontra/(antes ela é apenas um objeto)...
  49.     //alert("objeto em captureColumnIndex(): "+obj );
  50.     //alert("indice da coluna em captureColumnIndex(): "+obj.cellIndex );
  51. columnIndex = obj.cellIndex ;
  52. return columnIndex ;
  53.  
  54. function orderTd(obj) { 
  55.       var qtdLinhas= document.getElementById('nQtdLinhas').value;
  56.       var firstRow=2;
  57.       var totalLinhas=new Number( qtdLinhas);
  58.       var primeiraLinha= new Number( firstRow);
  59.       var i=new Number(1 );
  60. alert("indice da celula em orderTd(): "+obj.cellIndex );    
  61. newIndex = obj.cellIndex ;//indice da coluna destino
  62.  
  63. if (newIndex == null){alert("newIndex == null"); return ;}
  64. else{//se coluna for inexistente
  65. if (columnIndex == newIndex) {alert("columnIndex == newIndex"); return ;}//se coluna for posicao destino for a mesma que a de origem
  66. else{
  67.     for (x=firstRow; x<(totalLinhas+primeiraLinha+i); x++) { 
  68.         tds = tableRows[x].cells; //todas as células da tabela    
  69.         var indice=tds[newIndex];
  70.         var cell = tableRows[x].removeChild(tds[columnIndex]) //remova da linha X o valor da celula correspondente na coluna origem. Cell é iguala ao valor removido
  71.          alert("removido: "+ tds[columnIndex]);
  72.         if (newIndex >= maxIndex || newIndex + 1 >= maxIndex) { 
  73.             tableRows[x].appendChild(cell) 
  74.             alert("newIndex >= maxIndex || newIndex + 1 >= maxIndex: "+tableRows[x].appendChild(cell));    
  75.         } 
  76.         else { 
  77.             // alert(" indiceColuna: "+indiceColuna);
  78.             //alert("else: "+tableRows[x].insertBefore(cell, tds[indiceColuna]));//insira os valores da coluna clicada na coluna cuja posicao é correspondente ao indice informado
  79.             alert("tds[newIndex]: "+ indice +" cell: "+ cell +" no loop x="+ x);
  80.             tableRows[x].insertBefore(cell, indice) ;
  81.             //alert("else: "+tableRows[x].insertBefore(cell, tds[newIndex]));    
  82.  
  83.         }
  84.         //indiceColuna=newIndex;
  85.     } 
  86. }
  87. }
  88. function createDraggedColumn(obj) {  //permite à coluna ser arrastável
  89.   var conteudoCelula1 ;
  90.   var conteudoCelula2 ;
  91.   var qtdLinhas= document.getElementById('nQtdLinhas').value;
  92.   var firstRow=2;
  93.  
  94.  
  95. draggedTable = document.createElement("table"); 
  96. draggedTable.id = "drag"; 
  97. //var qtdLinhasTabela =tableRows.length;
  98. draggedThead = document.createElement("thead"); 
  99. draggedTheadTr = document.createElement("tr"); 
  100. //alert("draggedTheadTr em createDraggedColumn() : "+draggedTheadTr);
  101. draggedTheadTd = document.createElement("td"); 
  102. conteudoCelula1 = new String(tableRows[firstRow].cells[columnIndex].innerHTML);
  103. draggedTheadTd.innerHTML = conteudoCelula1; 
  104. //alert("conteudoCelula1 em createDraggedColumn():  "+conteudoCelula1+" passou 1");
  105. draggedTheadTr.appendChild(draggedTheadTd);//associando valores das linhas abaixo ao titulo da coluna 
  106. //alert(" passou 2");
  107. draggedThead.appendChild(draggedTheadTr) ;
  108. draggedTable.appendChild(draggedThead) ;
  109.   var i =1;
  110.  
  111.  
  112. draggedTbody = document.createElement("tbody");     
  113.  //alert("qtdLinhas:  "+qtdLinhasTabela);
  114.  var totalLinhas=new Number( qtdLinhas);
  115. var primeiraLinha= new Number( firstRow);
  116. result=totalLinhas+primeiraLinha;
  117. // alert("qtdLinhas+firstRow = "+ result);
  118. for (x=firstRow+1; x<=(totalLinhas+primeiraLinha); x++) { 
  119.     //alert("qtd de linhas: "+qtdLinhas+ " e X: "+ x);
  120.     //alert("conteudoCelula2 em createDraggedColumn() antes:  "+conteudoCelula2);
  121.     draggedTr = document.createElement("tr"); 
  122.     draggedTd = document.createElement("td");     
  123.     conteudoCelula2 = new String(tableRows[x].cells[columnIndex].innerHTML);
  124.     draggedTd.innerHTML= conteudoCelula2;
  125.    //idLinha= "'linha_cont_"+conteudoCelula1+"_"+i+"'";   
  126.    // conteudoCelula2 = new String(document.getElementById('linha_cont_'+conteudoCelula1+'_'+i).value);
  127.    //draggedTd.innerHTML =document.getElementById('linha_cont_'+conteudoCelula1+'_'+i).value; 
  128.    // alert("conteudoCelula2 em createDraggedColumn() depois:  "+conteudoCelula2);    
  129.     draggedTr.appendChild(draggedTd); 
  130.     draggedTbody.appendChild(draggedTr) ;
  131.     i++;
  132.  
  133. draggedTable.appendChild(draggedTbody); 
  134. draggedTable.style.filter = "alpha(opacity=70)"; 
  135. draggedTable.style.opacity = "0.7" ;
  136. draggedTable.style.mozOpacity = "0.7" ;
  137.  
  138. document.getElementsByTagName("body")[0].appendChild(draggedTable) 
  139. draggedTable.style.top = posY ;
  140.   draggedTable.style.left = posX ;
  141.  
  142. function mouseCoords(e) { //captura a posição do mouse
  143. if (!e) e = window.event ;
  144.  
  145. if(e.pageY) {
  146.      posX=e.pageX; 
  147.      posY=e.pageY;
  148.     // alert("X : "+posX+" Y: "+posY);
  149.      } 
  150.  
  151. else if (e.clientY) {posX=e.clientX + ietruebody().scrollLeft; //caso o browser seja IE
  152.                      posY=e.clientY + ietruebody().scrollTop;
  153.                      //alert("posX : "+posX+" posY: "+posY); } 
  154.  
  155.     if (document.getElementById("drag")) { 
  156.     dragTable = document.getElementById("drag"); 
  157.     dragTable.style.top = posY + 3 + "px" 
  158.     dragTable.style.left = posX + 7 + "px" 
  159.     } 
  160.    // alert("DiNovo ...posX : "+posX+" posY: "+posY); 
  161. return {x:posX, y:posY} 
  162.  
  163. function ietruebody(){ //captura as dimensões do corpo da página num browser IE, pq ele possui uma pequena borda à margem do corpo.
  164.  
  165. return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body 
  166. }
  167. }
Oct 15 '09 #17
acoder
16,027 Expert Mod 8TB
Have you tried the latest version as linked to earlier?
Oct 26 '09 #18

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

Similar topics

1
by: srikanth | last post by:
Hi all, I have an requirement in one of my projects. We have one aspx page, in that we have two frames, in each frame having a tree structure control(dynamic data). In the left Frame we are...
1
by: Karsten Schramm | last post by:
Hi, if I drag an Outlook.MailItem to a Windows-Explorer window a <subject>.msg file will be created. Now I try to drag & drop a mail item to my own WinForm app. Unfortunately it doesn't work....
2
by: Grey | last post by:
I need to design a workflow application with C#. I want to design an UI with some workflow components which they can be drag & drop anywhere in order to design the workflow for the application...
2
by: Ivo Tcholakov | last post by:
Is it possible to drag and drop controls in an aspx page at runtime ? Meaning i have developed a ASP.NET web form, the web form is now downloaded in IE - now can i have this form to detect mouse...
3
by: Goldwind | last post by:
Hi, I"m trying to use drag & drop of text from one text box to another but without suceess. Microsoft presented an example in "101 code samples" BUT in this example the code select and drag all...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
11
by: Greg P | last post by:
I'm using VB 2005, and have drug and dropped datagridviews onto my form. The default code that is generated for inserts tries to insert a value for the auto generated primary key. Below is the...
0
by: Romulo NF | last post by:
I´ve recently posted a script to move columns from a table using drag and drop javascript. Recently i´ve received a message of a user of this communty showing interest in the script to move lines...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.