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:movecolumn s.js
file:movecolumn s.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 31622
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:absolu te 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 :
setTableColMova ble("my_table_i d","one_html_fi eld",[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 setTableColMova ble 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(t ableId)
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.htm l
ps: for some reason i couldnt put everything in 1 post
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 displaying all the
documentcollections(dynamic) in the Treestructure, In the
Right side we are displaying all the HolderGroups
(dynamic).We have to assing Any one of the
Documentcollections to HolderGroups, through Drag and Drop
|
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. In the "drop event" are eight
DataFormats (e.Data.GetFormats()) but the important one (imho)
"FileContents" is "Nothing"/"null".
|
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 users. Moreover, when the user
double click on the component, they can define some routing rules for the
process. Is it possible to have such fancy UI in C#?? Any reference sites or
tutorial??
Million Thanks.
|
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 events, and provide drag
and drop functionality of the controls ?
Thank You
Ivo Tcholakov
|
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 the text in
the TextBox, wether the user wants or not.
I need to drag only the selected text (changing the
example causes it not to work).
| |
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 generated code. I put ***** around
the problematic code. I've tried just removing this and it doesn't seem to
work. There may be more generated code I don't know about. I could remove
it but I am planning on doing a lot of refreshing of the data...
|
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 instead of columns. This post will present the script for you guys.
file: moverows.css
table {border-collapse:collapse; cursor:pointer}
td {width:30px; padding:5px; text-align:center; font:bold 14px verdana; color:#000; border:1px solid #000}...
|
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 drag&drop on the same page (which was not possible). Now i´ve a new concept of the script, more object oriented.
I´ve also commented the whole code so you guys can easier understand it engine.
What exactly we need when trying to make a column drag &...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |