JavaScript-如何实现表格的行自由上下拖动

JavaScript-如何实现表格的行自由上下拖动

清晨说ぺ晚安 发布于 2017-03-21 字数 573 浏览 1134 回复 2

行拖动的实现思路非常简单,选中一行,往上拖就与上面的行交换位置,往下拖就与下面的行交换位置。问题是如何得到交换行。我见过一个非常详细的教程,它会把表格里的每一行的高度与Y坐标计算出来,换言之,都时是比较e.pageX是否在[rowTop,rowBottom]区间之内。但这也带来第二个问题,有多少行就有多个这样的区间。于是解法就变成取事件源对象,然后再往上取其父对象,如果其父对象是TR元素,就取其[rowTop,rowBottom]区间....思路非常直接,同时也客观做出一个限制——不能使用代理拖动。先不谈是拖动代理还是拖动实物,光是在移动鼠标的过程要做的计算量就非常大,幸好还在IE的承受范围之内。有更好的方法吗?

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2017-10-04 2 楼

给你个我以前使用过的吧:

<!doctype html>
<html>
<head>
<title>表格行拖动</title>
<script>
window.onload = function(){
//绑定事件
var addEvent = document.addEventListener ? function(el,type,callback){
el.addEventListener( type, callback, !1 );
} : function(el,type,callback){
el.attachEvent( "on" + type, callback );
}
//移除事件
var removeEvent = document.removeEventListener ? function(el,type,callback){
el.removeEventListener( type, callback );
} : function(el,type,callback){
el.detachEvent( "on" + type, callback);
}
//精确获取样式
var getStyle = document.defaultView ? function(el,style){
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
} : function(el,style){
style = style.replace(/-(w)/g, function($, $1){
return $1.toUpperCase();
});
return el.currentStyle[style];
}
var dragManager = {
clientY:0,
draging:function(e){//mousemove时拖动行
var dragObj = dragManager.dragObj;
if(dragObj){
e = e || event;
if(window.getSelection){//w3c
window.getSelection().removeAllRanges();
}else if(document.selection){
document.selection.empty();//IE
}
var y = e.clientY;
var down = y > dragManager.clientY;//是否向下移动
var tr = document.elementFromPoint(e.clientX,e.clientY);
if(tr && tr.nodeName == "TD"){
tr = tr.parentNode
dragManager.clientY = y;
if( dragObj !== tr){
tr.parentNode.insertBefore(dragObj, (down ? tr.nextSibling : tr));
}
};
}
},
dragStart:function(e){
e = e || event;
var target = e.target || e.srcElement;
if(target.nodeName === "TD"){
target = target.parentNode;
dragManager.dragObj = target;
if(!target.getAttribute("data-background")){
var background = getStyle(target,"background-color");
target.setAttribute("data-background",background)
}
//显示为可移动的状态
target.style.backgroundColor = " #ccc";
target.style.cursor = "move";
dragManager.clientY = e.clientY;
addEvent(document,"mousemove",dragManager.draging);
addEvent(document,"mouseup",dragManager.dragEnd);
}
},
dragEnd:function(e){
var dragObj = dragManager.dragObj
if (dragObj) {
e = e || event;
var target = e.target || e.srcElement;
if(target.nodeName === "TD"){
target = target.parentNode;
dragObj.style.backgroundColor = dragObj.getAttribute("data-background");
dragObj.style.cursor = "default";
dragManager.dragObj = null;
removeEvent(document,"mousemove",dragManager.draging);
removeEvent(document,"mouseup",dragManager.dragEnd);
}
}
},
main:function(el){
addEvent(el,"mousedown",dragManager.dragStart);
}
}
var el = document.getElementById("table");
dragManager.main(el);
}

</script>
<style>
.table{
width:60%;
border: 1px solid red;
border-collapse: collapse;
}
.table td{
border: 1px solid red;
height: 20px;
}
</style>
</head>
<body>
<h1>表格行拖动</h1>
<table id="table" class="table">
<tbody>
<tr>
<td>1</td>
<td>One</td>
<td>dom.require</td>
</tr>
<tr id="2" >
<td class="2">2</td>
<td>Two</td>
<td>ControlJS </td>
</tr>
<tr id="3" >
<td class="3">3</td>
<td>Three</td>
<td>HeadJS</td>
</tr>
<tr id="4" >
<td class="4">4</td>
<td>Four</td>
<td>LAB.js</td>
</tr>
<tr id="5" >
<td class="5">5</td>
<td>Five</td>
<td>$script.js</td>
</tr>
<tr id="6" >
<td class="6">6</td>
<td>Six</td>
<td>NBL.js</td>
</tr>
</tbody>
</table>
</body>
</html>

瑾兮 2017-04-26 1 楼

window.onload = function(){
//绑定事件
var addEvent = document.addEventListener ? function(el,type,callback){
el.addEventListener( type, callback, !1 );
} : function(el,type,callback){
el.attachEvent( "on" + type, callback );
}
//精确获取样式
var getStyle = document.defaultView ? function(el,style){
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
} : function(el,style){
style = style.replace(/-(w)/g, function($, $1){
return $1.toUpperCase();
});
return el.currentStyle[style];
}
var dragManager = {
y:0,
draging:function(e){//mousemove时拖动行
var handler = dragManager.handler;
if(handler){
e = e || event;
if(window.getSelection){//w3c
window.getSelection().removeAllRanges();
}else if(document.selection){
document.selection.empty();//IE
}
var y = e.clientY;
var down = y > dragManager.y;//是否向下移动
var tr = document.elementFromPoint(e.clientX,e.clientY);
if(tr && tr.nodeName == "TD"){
tr = tr.parentNode
dragManager.y = y;
if( handler !== tr){
tr.parentNode.insertBefore(handler, (down ? tr.nextSibling : tr));
}
};
}
},
dragStart:function(e){
e = e || event;
var handler = e.target || e.srcElement;
if(handler.nodeName === "TD"){
handler = handler.parentNode;
dragManager.handler = handler;
if(!handler.getAttribute("data-background")){
handler.setAttribute("data-background",getStyle(handler,"background-color"))
}
//显示为可移动的状态
handler.style.backgroundColor = " #ccc";
handler.style.cursor = "move";
dragManager.y = e.clientY;
}
},
dragEnd:function(){
var handler = dragManager.handler
if (handler) {
handler.style.backgroundColor = handler.getAttribute("data-background");
handler.style.cursor = "default";
dragManager.handler = null;
}
},
main:function(el){
addEvent(el,"mousedown",dragManager.dragStart);
addEvent(document,"mousemove",dragManager.draging);
addEvent(document,"mouseup",dragManager.dragEnd);

}
}
var el = document.getElementById("table");
dragManager.main(el);
}