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>
-
17 31503
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.
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.
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!
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;
-
}
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;
-
}
-
-
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 ;)
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
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
FOR YOUR NEXT MODIFICATION, CONSIDER FOLLWINGS
MORE THAN ONE TABLES DRAG AND DROP CAPABILITY
SHIFT COLUMN BETWEEN TABLES
CHAMI.
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.
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......
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......
I suggest you start a new thread in the Q&A section.
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: Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
by: VB Programmer |
last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
| |