分享
 
 
 

关于web页面表格列拖动的解决方法

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

在新开发的基于J2ee的应用系统中,客户端页面输出数据用表格是在也普通不过的事了!排序的问题已经解决了,由于原来项目采用的是pb开发的,大家习惯了数据窗口的功能,提出了新的需求,为方便浏览数据,数据列是否可以随意拖动顺序由用户自动排列!

的确,这个问题相对比较困难,我本人从来没有见过这种程序。经过仔细思考,这种情况不能在服务器端处理,用户排列规则无法保存也是不断变化的。于是决定在客户端采用javascript进行处理,这样速度也快!

上网好不容易找到了一篇文章,采用的cell的swapnode方法对列进行交换,然而经过测试发觉表格前好像不能有别的页面元素,否则不能正确排序。后来在csdn上找到一篇文章是在。net下扩展客户端功能的,细想客户端不是也用javascript控制的吗,于是拿过来分析一下,经过稍微修改后,成功实现所需功能。在此向原作者表示感谢,同与大家共享,希望对大家有所帮助。至于原文大家在csdn搜索可得到。

源码如下:

var tbody, headRow;

var bDragMode = false;

var objDragItem;

var arrHitTest = new Array();

var iArrayHit = false;

var ColumnCount = null;

var mustRefresh = false;

var dragColor=null;

var hitColor=null;

var HasTopMostPager=null;

var PowerTable=null;

//

// Init function.. Fills out variables with data

// loaded with oncontentready.

//将table初始化

function initDragTable(table)

{

var i;

PowerTable=table;

if (dragColor == null)

dragColor = "white";

if (hitColor == null)

hitColor = "lightblue";

// get TBODY - take the first TBODY for the table

tbody = PowerTable.tBodies(0);

if (!tbody) return;

// get THEAD - take the unique THEAD for the table

var click = PowerTable.tHead;

if (!click) return;

// Determine the row to use (read from HasPager)

if (HasTopMostPager == "true")

headRow = click.children[1];

else

headRow = click.children[0];

if (headRow.tagName != "TR") return;

headRow.runtimeStyle.cursor = "hand"; //"move";

ColumnCount = headRow.children.length;

for(i=0; i < ColumnCount ; i++)

{

arrHitTest[i] = new Array();

}

var cx=0;

var cy=0;

var c;

defaultTitleColor = headRow.children[0].currentStyle.backgroundColor;

for (i=0; i<ColumnCount ; i++) {

var clickCell = headRow.children[i];

clickCell.selectIndex = i;

c = clickCell.offsetParent;

if (cx == 0 && cy == 0)

{

while (c.offsetParent != null) {

cy += c.offsetTop;

cx += c.offsetLeft;

c = c.offsetParent;

}

}

arrHitTest[i][0] = cx + clickCell.offsetLeft;

arrHitTest[i][1] = cy + clickCell.offsetTop;

arrHitTest[i][2] = clickCell;

arrHitTest[i][3] = cx + clickCell.offsetLeft + clickCell.clientWidth;

clickCell.attachEvent("onmousedown",onMouseDown);

}

PowerTable.attachEvent("onmousemove",onMouseMove);

PowerTable.attachEvent("onmouseup",onMouseUp);

///////

if (arrHitTest[0][0] == arrHitTest[0][3])

mustRefresh = true;

}

function InitHeader()

{

var cx=0;

var cy=0;

var c;

for (i=0; i<ColumnCount ; i++) {

var clickCell = headRow.children[i];

clickCell.selectIndex = i;

c = clickCell.offsetParent;

if(cx == 0 && cy == 0 )

{

while (c.offsetParent != null) {

cy += c.offsetTop;

cx += c.offsetLeft;

c = c.offsetParent;

}

}

arrHitTest[i][0] = cx + clickCell.offsetLeft;

arrHitTest[i][1] = cy + clickCell.offsetTop;

arrHitTest[i][2] = clickCell;

arrHitTest[i][3] = cx + clickCell.offsetLeft + clickCell.clientWidth;

}

}

function ChangeHeader(iChange)

{

for(var y = 0; y < arrHitTest.length; y++)

{

if (arrHitTest[y][2].currentStyle.backgroundColor == hitColor)

arrHitTest[y][2].style.backgroundColor = defaultTitleColor;

}

if(iChange == "-1") return;

arrHitTest[iChange][2].style.backgroundColor = hitColor;

}

function onMouseUp(e)

{

if(!bDragMode) return;

bDragMode = false;

var iSelected = objDragItem.selectIndex;

objDragItem.removeNode(true);

objDragItem = null;

ChangeHeader(-1);

if( (iArrayHit - 1) < 0 || iSelected < 0) return; // default faliure

// iSelected is the 0-based index of the column being moved

// (iArrayHit-1) is the 0-based index of the column being replaced

CopyRow(iSelected, (iArrayHit - 1) );

// Reset our variables

iSelected = 0;

iArrayHit = -1;

}

function onMouseDown(e)

{

// If the grid is contained in an invisible panel (other DHTML stuff)

// the initialization step must be repeated to take real values

if( mustRefresh) {

InitHeader();

mustRefresh = false;

}

bDragMode = true;

var src = e.srcElement;

var c = e.srcElement;

while (src.tagName != "TD")

src = src.parentElement;

// Create our header on the fly

objDragItem = document.createElement("DIV");

objDragItem.innerHTML = src.innerHTML;

objDragItem.style.height = src.offsetParent.clientHeight;

objDragItem.style.width = src.clientWidth;

objDragItem.style.background = dragColor;

objDragItem.style.fontColor = src.currentStyle.fontColor;

objDragItem.style.position = "absolute";

objDragItem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=75)";

objDragItem.selectIndex = src.selectIndex;

objDragItem.style.border = "1 solid black";

while (c.offsetParent != null)

{

objDragItem.style.y += c.offsetTop;

objDragItem.style.x += c.offsetLeft;

c = c.offsetParent;

}

objDragItem.style.borderStyle = "dashed";

objDragItem.style.borderWidth = "1px";

objDragItem.style.display = "none";

src.insertBefore(objDragItem);

}

function onMouseMove(e)

{

if(!bDragMode || !objDragItem) return;

// If we aren't dragging or our object is null, we return

// Hardcoded value for height difference

var midWObj = objDragItem.style.posWidth / 2;

var midHObj = 12;

// Save mouse's position in the document

var intTop = e.clientY +document.body.scrollTop;

var intLeft = e.clientX + document.body.scrollLeft;

var cx=0,cy=0;

var elCurrent = objDragItem.offsetParent;

if (elCurrent != null)

{

while (elCurrent.offsetParent != null) {

cx += elCurrent.offsetTop;

cy += elCurrent.offsetLeft;

elCurrent = elCurrent.offsetParent;

}

}

objDragItem.style.pixelTop = intTop - cx - midHObj;

objDragItem.style.pixelLeft = intLeft - cy - midWObj;

if(objDragItem.style.display == "none")

objDragItem.style.display = "";

iArrayHit = CheckHit(intTop , intLeft , e);

e.cancelBubble = false;

e.returnValue = false;

}

function CheckHit(x,y,e)

{

midWObj = objDragItem.style.posWidth / 2;

midHObj = 12;

for(var i=0; i < ColumnCount; i++)

{

if( (y) > (arrHitTest[i][0]) && (y) < (arrHitTest[i][3] ))

{

ChangeHeader(i);

return i + 1;

}

}

return -1;

}

//

// Copy from row to row.. Does the Header also.

//

function CopyRow(from, to)

{

if(from == to) return;

var origfrom = from;

var origto = to;

var iDiff = 0;

if( from > to )

{

iDiff = from - to;

var saveObj = headRow.children[from].innerHTML;

var saveWidth = headRow.children[from].width;

for(var i = 0 ; i < iDiff; i++)

{

headRow.children[from].innerHTML = headRow.children[from - 1].innerHTML;

headRow.children[from].width = headRow.children[from - 1].width;

from--;

}

headRow.children[to].innerHTML = saveObj;

headRow.children[to].width = saveWidth;

}

else

{

iDiff = to - from;

var saveObj = headRow.children[from].innerHTML;

var saveWidth = headRow.children[from].width;

for(var i = 0 ; i < iDiff; i++)

{

headRow.children[from].innerHTML = headRow.children[from + 1].innerHTML;

headRow.children[from].width = headRow.children[from + 1].width;

from++;

}

headRow.children[to].innerHTML = saveObj;

headRow.children[to].width = saveWidth;

}

for(var i = 0 ; i < headRow.children.length; i++)

headRow.children[i].selectIndex = i;

InitHeader();

for ( var iRowInsert = 0 ; iRowInsert < tbody.rows.length; iRowInsert++ )

{

from = origfrom;

to = origto;

if( from > to )

{

iDiff = from - to;

var saveObj;

try {

saveObj = tbody.children[iRowInsert].children[from].innerHTML;

} catch(e) { saveObj = null; }

for(var i = 0 ; i < iDiff; i++)

{

try {

tbody.children[iRowInsert].children[from].innerHTML = tbody.children

[iRowInsert].children[from - 1].innerHTML;

from--;

} catch(e) {}

}

if (saveObj != null)

tbody.children[iRowInsert].children[to].innerHTML = saveObj;

}

else

{

iDiff = to - from;

var saveObj;

try {

saveObj = tbody.children[iRowInsert].children[from].innerHTML;

} catch(e) {}

for(var i = 0 ; i < iDiff; i++)

{

try {

tbody.children[iRowInsert].children[from].innerHTML = tbody.children

[iRowInsert].children[from + 1].innerHTML;

from++;

} catch(e) {}

}

try {

tbody.children[iRowInsert].children[to].innerHTML = saveObj;

} catch(e) {}

}

}

////////

var buf = "";

for(var i=0; i<headRow.children.length; i++)

{

var td = headRow.children[i];

tmp = td.innerHTML;

pos = tmp.indexOf("<");

if (pos > 0)

tmp = tmp.substring(0, pos);

else

{

if (pos == 0)

tmp = td.innerText;

}

buf += tmp;

if (i < headRow.children.length-1)

buf += ",";

}}

另外你的表格必须由thead、tbody组成。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有