Connecting Tech Pros Worldwide Forums | Help | Site Map

Table - Lines/Rows drag & drop

Member
 
Join Date: Nov 2006
Posts: 53
#1   Nov 13 '06
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 instead of columns. This post will present the script for you guys.

file: moverows.css

Expand|Select|Wrap|Line Numbers
  1. table {border-collapse:collapse; cursor:pointer}
  2. td {width:30px; padding:5px; text-align:center; font:bold 14px verdana; color:#000; border:1px solid #000}
  3. input {width:30px}
  4.  
  5. /* classe das cores utilizadas na movimentação */
  6. .hover {background:lightblue}
  7. .selecionado {background:lightgreen}
  8.  
file: moverows.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 row function in the tabelaDrag variable
  13.  
  14. document.onmouseup = soltar;
  15. var drag = false;
  16.  
  17. window.onload = init
  18.  
  19. function init() {
  20. tabelaDrag = document.getElementById("tabela");
  21. linhas = tabelaDrag.getElementsByTagName("TR");
  22. celulas = tabelaDrag.getElementsByTagName("TD");
  23.  
  24.   tabelaDrag.onselectstart = function () { return false; } 
  25.   tabelaDrag.onmousedown = function () { return false; }
  26.  
  27.       for (x=0; x<celulas.length;x++) {
  28.         arrastar(celulas[x])
  29.         celulas[x].onmouseover = pintar
  30.         celulas[x].onmouseout = pintar
  31.     }
  32. }
  33.  
  34. function capturarLinha(obj) {
  35. origem = obj.parentNode.rowIndex
  36. return origem
  37. }
  38.  
  39. function orderTd (obj) {
  40. destino = obj.cellIndex
  41.  
  42. if (destino == null) return
  43. if (coluna == destino) return
  44.  
  45.     for (x=0; x<linhas.length; x++) {
  46.     tds = linhas[x].cells
  47.     var celula = linhas[x].removeChild(tds[coluna])
  48.         if (destino >= ordenacaoMaxima || destino + 1 >= ordenacaoMaxima) {
  49.         linhas[x].appendChild(celula)
  50.         }
  51.         else {
  52.         linhas[x].insertBefore(celula, tds[destino])
  53.         }
  54.     }
  55. }
  56.  
  57. function soltar(e){
  58.     if (!e) e=window.event
  59.     if (e.target) targ = e.target
  60.     else if (e.srcElement) targ=e.srcElement
  61.  
  62.     orderTr(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.         linhaAtual = this.parentNode.rowIndex
  76.             for (x=0; x<linhas[linhaAtual].cells.length; x++) {
  77.             linhas[linhaAtual].cells[x].className="selecionado"
  78.             }
  79.         drag = true
  80.         capturarLinha(this);
  81.         return false;
  82.     }
  83. }
  84.  
  85. function orderTr(obj) {
  86. obj = obj.parentNode
  87. destino = obj.rowIndex
  88. trs = tabelaDrag.rows;
  89.  
  90. if (destino == null) return
  91. if (origem == destino) return
  92.  
  93.     if(tabelaDrag.moveRow) {
  94.     tabelaDrag.moveRow(origem,destino);
  95.     }
  96.  
  97.     if(!tabelaDrag.moveRow && origem < destino) {
  98.     var qtd = destino - origem
  99.         for (x=0; x<qtd; x++) {
  100.         destinotemp = origem
  101.         origemtemp = destino
  102.         trs[origemtemp].parentNode.insertBefore(trs[origemtemp],trs[destinotemp]);
  103.         }
  104.     }
  105.     else if(!tabelaDrag.moveRow && origem > destino) {
  106.     trs[origem].parentNode.insertBefore(trs[origem],trs[destino]);
  107.     }
  108. }
  109.  
  110. function pintar(e) {
  111. if (!e) e=window.event
  112. ev = e.type
  113.  
  114. linhaDrag = this.parentNode.rowIndex
  115. qtdTd = this.parentNode.cells.length
  116.  
  117.     if (ev == "mouseover") {
  118.         if (drag) {
  119.             if (this.className !="selecionado") {
  120.                 for (x=0; x<qtdTd; x++) {
  121.                 linhas[linhaDrag].cells[x].className="hover"
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.     else if (ev == "mouseout") {
  128.         if (this.className !="selecionado") {
  129.             for (x=0; x<qtdTd; x++) {
  130.             linhas[linhaDrag].cells[x].className=""
  131.             }        
  132.         }
  133.     }
  134. }
  135.  
  136.  
file: moverows.html

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <link rel="StyleSheet" href="moverows.css" type="text/css" />
  5.  
  6. <script type="text/javascript" src="moverows.js"></script>
  7.  
  8. <table id="tabela">
  9.     <tr>
  10.         <td>A1</td>
  11.         <td>A2</td>
  12.         <td>A3</td>
  13.         <td>A4</td>
  14.         <td>A5</td>                
  15.     </tr>
  16.     <tr>
  17.         <td>B1</td>
  18.         <td>B2</td>
  19.         <td>B3</td>
  20.         <td>B4</td>
  21.         <td>B5</td>                
  22.     </tr>
  23.     <tr>
  24.         <td>C1</td>
  25.         <td>C2</td>
  26.         <td>C3</td>
  27.         <td>C4</td>
  28.         <td>C5</td>                
  29.     </tr>
  30.     <tr>
  31.         <td>D1</td>
  32.         <td>D2</td>
  33.         <td>D3</td>
  34.         <td>D4</td>
  35.         <td>D5</td>                
  36.     </tr>
  37.     <tr>
  38.         <td>E1</td>
  39.         <td>E2</td>
  40.         <td>E3</td>
  41.         <td>E4</td>
  42.         <td>E5</td>                
  43.     </tr>
  44.  
  45. </table>
  46.  
  47. </body>
  48. </html>
  49.  
hope that you people find this useful!
the link to the script to movecolumns is:
http://www.thescripts.com/forum/show...37#post2176637

see yall!

Last edited by gits; Nov 13 '07 at 08:42 PM. Reason: added code identifiers



Reply