473,386 Members | 1,652 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,386 developers and data experts.

Table - Lines/Rows drag & drop

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!
Nov 13 '06 #1
0 8410

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

Similar topics

1
by: Targa | last post by:
I have an ASP scheduling application in which there are blocks of time, represented with a table, within another table which is the timeline. In other words, I have a table with a vertical...
1
by: James Allen Bressem | last post by:
- hehe - When you're explaining every angle of a socio economic scenario in the context of a review of current technical issues it can take a little time to get it right - slibS, slibW, slibD (so...
0
by: James Allen Bressem | last post by:
Do drag n drop in VB.Net in ten lines of code - (too easy) I was searching through the MSDN documentation trying to figure out how to do drag n drop and I found a sample program. The sample...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
17
by: Romulo NF | last post by:
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...
2
by: David McDivitt | last post by:
I downloaded all the express editions and am doing the walk throughs with visual web developer. In an earlier walk through, when I dragged a table from the standard section of the toolbox, it would...
8
by: active | last post by:
I have a table on my page with rows and cells. I try to drag a table from the toolbox into one of the cells but the drag symbol shows it is not allowed (and the table does not drop) Can I...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.