Table - Columns drag & drop | Member | | Join Date: Nov 2006
Posts: 53
# 1
Nov 1 '06
| |
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 -
/* estilo geral da tabela */
-
table {border-collapse:collapse}
-
table td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer}
-
-
/* classe das cores utilizadas na movimentação */
-
.hover {background:lightblue}
-
.selecionado {background:lightgreen}
-
file:movecolumns.js
file:movecolumns.html -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-
-
<html>
-
<head>
-
<title>Drag and Drop Columns</title>
-
-
<script type="text/javascript" src="movecolumn.js"></script>
-
-
<link rel="StyleSheet" href="movecolumn.css" type="text/css" />
-
-
</head>
-
-
<body>
-
-
<table id="tabela">
-
<tr>
-
<td>Coluna 1</td>
-
<td>Coluna 2</td>
-
<td>Coluna 3</td>
-
<td>Coluna 4</td>
-
<td>Coluna 5</td>
-
<td>Coluna 6</td>
-
<td>Coluna 7</td>
-
<td>Coluna 8</td>
-
<td>Coluna 9</td>
-
<td>Coluna 10</td>
-
</tr>
-
<tr>
-
<td>A1</td>
-
<td>A2</td>
-
<td>A3</td>
-
<td>A4</td>
-
<td>A5</td>
-
<td>A6</td>
-
<td>A7</td>
-
<td>A8</td>
-
<td>A9</td>
-
<td>A10</td>
-
</tr>
-
<tr>
-
<td>B1</td>
-
<td>B2</td>
-
<td>B3</td>
-
<td>B4</td>
-
<td>B5</td>
-
<td>B6</td>
-
<td>B7</td>
-
<td>B8</td>
-
<td>B9</td>
-
<td>B10</td>
-
</tr>
-
<tr>
-
<td>C1</td>
-
<td>C2</td>
-
<td>C3</td>
-
<td>C4</td>
-
<td>C5</td>
-
<td>C6</td>
-
<td>C7</td>
-
<td>C8</td>
-
<td>C9</td>
-
<td>C10</td>
-
</tr>
-
-
</table>
-
-
</body>
-
-
</html>
-
Last edited by gits; Nov 13 '07 at 08:37 PM.
Reason: added code identifiers
| | Newbie | | Join Date: Sep 2006
Posts: 8
# 2
Dec 10 '06
| | | re: Table - Columns drag & drop
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.
| | Newbie | | Join Date: Dec 2006
Posts: 2
# 3
Dec 12 '06
| | | re: Table - Columns drag & drop
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.
| | Member | | Join Date: Nov 2006
Posts: 53
# 4
Dec 13 '06
| | | re: Table - Columns drag & drop Quote:
Originally Posted by archrajan 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!
| | Newbie | | Join Date: Feb 2007 Location: Vancouver, Canada
Posts: 1
# 5
Feb 2 '07
| | | re: Table - Columns drag & drop
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: - function cellIndex(td) {
-
var ci = -1;
-
var parent_row = ascendDOM(td, 'tr');
-
for (var i = 0; i < parent_row.cells.length; i++) {
-
if (td === parent_row.cells[i]) {
-
ci = i;
-
}
-
}
-
return ci;
-
}
-
-
function ascendDOM(e,target) {
-
while (e.nodeName.toLowerCase() !=target &&
-
e.nodeName.toLowerCase() !='html')
-
e=e.parentNode;
-
return (e.nodeName.toLowerCase() == 'html')? null : e;
-
}
| | Newbie | | Join Date: Sep 2006
Posts: 8
# 6
Feb 2 '07
| | | re: Table - Columns drag & drop
Thanks for the fix.
| | Newbie | | Join Date: Mar 2007
Posts: 2
# 7
Mar 10 '07
| | | re: Table - Columns drag & drop
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 ;) -
//----------------------------------------------//
-
// Created by: Romulo do Nascimento Ferreira //
-
// Email: romulo.nf@gmail.com //
-
//----------------------------------------------//
-
-
// NOTICE: This code may be use dto any purpose without further
-
// permission from the author. You may remove this notice from the
-
// final code, however its appreciated if you keep the author name/email.
-
// Email me if theres something needing improvement
-
-
-
var drag = false;
-
var colOrder=[];
-
var borderLeftStyle=[];
-
var borderLeftColor=[];
-
var cellBackgroundColor=[];
-
var borderLeftWidth=[];
-
var colNames=[];
-
-
function setTableColMovable(tableName,ColOrderFieldName,colNames) {
-
tabelaDrag = document.getElementById(tableName);
-
ColOrderField = document.getElementById(ColOrderFieldName);
-
linhas = tabelaDrag.getElementsByTagName("TR");
-
celulas = tabelaDrag.getElementsByTagName("TD");
-
linhaUm = tabelaDrag.rows[0]
-
ordenacaoMaxima = linhaUm.cells.length
-
-
//store columns order
-
for (x=0;x<linhaUm.cells.length;x++)
-
{
-
arrastar(celulas[x]);
-
celulas[x].onselectstart = function () { return false; } //prevent text selection
-
celulas[x].onmouseover = pintar
-
celulas[x].onmouseout = pintar
-
celulas[x].onmouseup = pintar
-
colOrder[x]=colNames[x];
-
linhaUm.cells[x].style.cursor="pointer";
-
}
-
-
//store cells styles
-
for(x=0; x<linhas.length; x++) {
-
borderLeftStyle[x]=[];
-
borderLeftColor[x]=[];
-
cellBackgroundColor[x]=[];
-
borderLeftWidth[x]=[];
-
for (y=0; y<linhas[x].cells.length; y++) {
-
borderLeftStyle[x][y]=linhas[x].cells[y].style.borderLeftStyle;
-
borderLeftColor[x][y]=linhas[x].cells[y].style.borderLeftColor;
-
cellBackgroundColor[x][y]=linhas[x].cells[y].style.backgroundColor;
-
borderLeftWidth[x][y]=linhas[x].cells[y].style.borderLeftWidth;
-
}
-
}
-
}
-
-
function getColOrder()
-
{
-
return colOrder
-
}
-
-
function moveArrayCol(myArray,source,destination)
-
{
-
val=myArray.splice(source,1);
-
if (source < destination)
-
{myArray.splice(destination-1,0,val);}
-
else
-
{myArray.splice(destination,0,val);}
-
return myArray
-
}
-
-
function capturarColuna(obj) {
-
coluna = cellIndex(obj)
-
return coluna
-
}
-
-
function orderTd (obj) {
-
destino = cellIndex(obj)
-
if (destino == null) return
-
if (coluna == destino) return
-
-
for (x=0; x<linhas.length; x++)
-
{
-
tds = linhas[x].cells
-
var celula = linhas[x].removeChild(tds[coluna])
-
if (coluna < destino)
-
{
-
linhas[x].insertBefore(celula, tds[destino-1])
-
}
-
else
-
{
-
linhas[x].insertBefore(celula, tds[destino])
-
}
-
}
-
moveArrayCol(colOrder,coluna,destino);
-
//alert(colOrder);
-
}
-
-
function soltar(e){
-
if (!e) e=window.event
-
if (e.target) targ = e.target
-
else if (e.srcElement) targ=e.srcElement
-
orderTd(targ)
-
drag = false
-
-
for(x=0; x<linhas.length; x++) {
-
for (y=0; y<linhas[x].cells.length; y++) {
-
linhas[x].cells[y].style.borderLeftStyle=borderLeftStyle[x][y];
-
linhas[x].cells[y].style.borderLeftColor=borderLeftColor[x][y];
-
linhas[x].cells[y].style.backgroundColor=cellBackgroundColor[x][y];
-
linhas[x].cells[y].style.borderLeftWidth=borderLeftWidth[x][y];
-
}
-
}
-
}
-
-
function arrastar(obj){
-
if(!obj) return;
-
obj.onmousedown = function(ev){
-
//colunaAtual = cellIndex(this))
-
for (x=0; x<linhas.length; x++) {
-
linhas[x].cells[cellIndex(this)].style.backgroundColor="yellow"
-
}
-
drag = true
-
capturarColuna(this);
-
return false;
-
}
-
}
-
-
function pintar(e) {
-
if (!e) e=window.event
-
ev = e.type
-
-
if (ev == "mouseover") {
-
if (drag) {
-
for (x=0; x<linhas.length; x++) {
-
if (cellIndex(this) !=coluna) { //(this.style.borderStyle !="dashed") {
-
linhas[x].cells[cellIndex(this)].style.borderLeftStyle="solid";
-
linhas[x].cells[cellIndex(this)].style.borderLeftColor="red";
-
linhas[x].cells[cellIndex(this)].style.borderLeftWidth="2px";
-
}
-
}
-
}
-
}
-
-
else if (ev == "mouseout") {
-
if (drag)
-
{for (x=0; x<linhas.length; x++)
-
{if (cellIndex(this) !=coluna) //(this.borderStyle !="dashed")
-
{
-
linhas[x].cells[cellIndex(this)].style.borderLeftStyle=borderLeftStyle[x] [cellIndex(this)];
-
linhas[x].cells[cellIndex(this)].style.borderLeftColor=borderLeftColor[x] [cellIndex(this)];
-
linhas[x].cells[cellIndex(this)].style.backgroundColor=cellBackgroundColor[x] [cellIndex(this)];
-
linhas[x].cells[cellIndex(this)].style.borderLeftWidth=borderLeftWidth[x] [cellIndex(this)];
-
}
-
}
-
}
-
}
-
-
-
else if (ev == "mouseup") {
-
if (e.target) targ = e.target
-
else if (e.srcElement) targ=e.srcElement
-
orderTd(targ)
-
drag = false
-
//put columns new order in hidden field
-
ColOrderField.value=colOrder;
-
for(x=0; x<linhas.length; x++) {
-
for (y=0; y<linhas[x].cells.length; y++) {
-
linhas[x].cells[y].style.borderLeftStyle=borderLeftStyle[x][y];
-
linhas[x].cells[y].style.borderLeftColor=borderLeftColor[x][y];
-
linhas[x].cells[y].style.backgroundColor=cellBackgroundColor[x][y];
-
linhas[x].cells[y].style.borderLeftWidth=borderLeftWidth[x][y];
-
}
-
}
-
}
-
-
}
-
-
function cellIndex(td) {
-
var ci = -1;
-
var parent_row = ascendDOM(td, 'tr');
-
for (var i = 0; i < parent_row.cells.length; i++) {
-
if (td === parent_row.cells[i]) {
-
ci = i;
-
}
-
}
-
return ci;
-
}
-
-
function ascendDOM(e,target) {
-
while (e.nodeName.toLowerCase() !=target &&
-
e.nodeName.toLowerCase() !='html')
-
e=e.parentNode;
-
return (e.nodeName.toLowerCase() == 'html')? null : e;
-
}
-
-
Last edited by gits; Nov 13 '07 at 08:38 PM.
Reason: added code identifiers
| | Newbie | | Join Date: Mar 2007
Posts: 2
# 8
Mar 10 '07
| | | re: Table - Columns drag & drop
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 ;)
| | Member | | Join Date: Nov 2006
Posts: 53
# 9
Apr 17 '07
| | | re: Table - Columns drag & drop
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
Last edited by gits; Nov 13 '07 at 08:38 PM.
Reason: added code identifiers
| | Member | | Join Date: Nov 2006
Posts: 53
# 10
Apr 17 '07
| | | re: Table - Columns drag & drop
file: movecolumns.css -
/* table general style */
-
table {border-collapse:collapse}
-
table td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer; font:bold 12px verdana;}
-
table thead td {background:#1864E0; color:#fff;}
-
-
/* drag td style */
-
-
#drag {position:absolute; border:1px solid #000; text-align:center; cursor:pointer; font:bold 12px verdana; color:#fff;}
-
#drag thead td {background:#1864E0; color:#fff;}
-
#drag td {border:1px solid #000; padding:2px 5px; text-align:center; cursor:pointer; font:bold 12px verdana; color:#000}
-
file: movecolumns.html
ps: for some reason i couldnt put everything in 1 post
Last edited by gits; Nov 13 '07 at 08:39 PM.
Reason: added code identifiers
| | Newbie | | Join Date: Dec 2007
Posts: 1
# 11
Dec 15 '07
| | | re: Table - Columns drag & drop
FOR YOUR NEXT MODIFICATION, CONSIDER FOLLWINGS
MORE THAN ONE TABLES DRAG AND DROP CAPABILITY
SHIFT COLUMN BETWEEN TABLES
CHAMI.
Last edited by gits; Dec 15 '07 at 11:37 PM.
Reason: removed quote
|  | Expert | | Join Date: Sep 2006
Posts: 5,577
# 12
Dec 16 '07
| | | re: Table - Columns drag & drop
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: - <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
-
"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.
| | Member | | Join Date: Nov 2006
Posts: 53
# 13
Dec 19 '07
| | | re: Table - Columns drag & drop
If you got into this topic consider looking my new column drag & drop script | | Newbie | | Join Date: Feb 2009
Posts: 2
# 14
Feb 15 '09
| | | re: Table - Columns drag & drop
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......
| | Newbie | | Join Date: Feb 2009
Posts: 2
# 15
Feb 15 '09
| | | re: Table - Columns drag & drop
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......
Last edited by acoder; Feb 16 '09 at 09:23 AM.
Reason: Removed email
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
# 16
Feb 16 '09
| | | re: Table - Columns drag & drop
I suggest you start a new thread in the Q&A section.
| | Newbie | | Join Date: Oct 2009
Posts: 1
# 17
Oct 15 '09
| | | re: Table - Columns drag & drop
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:
Last edited by Frinavale; Oct 19 '09 at 09:04 PM.
Reason: Please post code in [code] ... [/code] tags. Added code tags.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
# 18
4 Weeks Ago
| | | re: Table - Columns drag & drop
Have you tried the latest version as linked to earlier?
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|