现在有个需求需要实现这样的效果:
可以通过拖动A表中的行到B表实现把A表中的某行复制到B表,并且可以对B表中的行进行拖动排序。
即:
在A表的ID为1的行上按下鼠标左键,在拖动过程中被拖动的行的副本应随着鼠标移动,然后拖动到B表*后松开鼠标左键,这时A表中ID为1的行就出现在了B表中,并且可以拖动B表中的ID为1或者5的行进行排序。
请问如何实现?
6 个解决方案
#1
1
#2
#3
自定义拖拽布局 以前在网上找的
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
float:left;
width:20%;
border:1px solid #FF0000;
margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
</style>
<script>
var dragobj={}
window.onerror=function(){return false}
function on_ini(){
String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
var agent=navigator.userAgent
window.isOpr=agent.inc("Opera")
window.isIE=agent.inc("IE")&&!isOpr
window.isMoz=agent.inc("Mozilla")&&!isOpr&&!isIE
if(isMoz){
Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
}
basic_ini()
}
function basic_ini(){
window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.onload=function(){
on_ini()
var o=document.getElementsByTagName("h1")
for(var i=0;i<o.length;i++){
o[i].onmousedown=function(e){
if(dragobj.o!=null)
return false
e=e||event
dragobj.o=this.parentNode
dragobj.xy=getxy(dragobj.o)
dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
dragobj.o.style.width=dragobj.xy[2]+"px"
dragobj.o.style.height=dragobj.xy[3]+"px"
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
dragobj.o.style.position="absolute"
var om=document.createElement("div")
dragobj.otemp=om
om.style.width=dragobj.xy[2]+"px"
om.style.height=dragobj.xy[3]+"px"
dragobj.o.parentNode.insertBefore(om,dragobj.o)
return false
}
}
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
if(dragobj.o!=null){
dragobj.o.style.width="auto"
dragobj.o.style.height="auto"
dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
dragobj.o.style.position=""
oDel(dragobj.otemp)
dragobj={}
}
}
document.onmousemove=function(e){
e=e||event
if(dragobj.o!=null){
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
createtmpl(e)
}
}
function getxy(e){
var a=new Array()
var t=e.offsetTop;
var l=e.offsetLeft;
var w=e.offsetWidth;
var h=e.offsetHeight;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
a[0]=t;a[1]=l;a[2]=w;a[3]=h
return a;
}
function inner(o,e){
var a=getxy(o)
if(e.x>a[1]&&e.x<(a[1]+a[2])&&e.y>a[0]&&e.y<(a[0]+a[3])){
if(e.y<(a[0]+a[3]/2))
return 1;
else
return 2;
}else
return 0;
}
function createtmpl(e){
for(var i=0;i<12;i++){
if($("m"+i)==dragobj.o)
continue
var b=inner($("m"+i),e)
if(b==0)
continue
dragobj.otemp.style.width=$("m"+i).offsetWidth
if(b==1){
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
}else{
if($("m"+i).nextSibling==null){
$("m"+i).parentNode.appendChild(dragobj.otemp)
}else{
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
}
}
return
}
for(var j=0;j<3;j++){
if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
continue
var op=getxy($("dom"+j))
if(e.x>(op[1]+10)&&e.x<(op[1]+op[2]-10)){
$("dom"+j).appendChild(dragobj.otemp)
dragobj.otemp.style.width=(op[2]-10)+"px"
}
}
}
</script>
</head>
<body>
<div class=content>
<div class=left id=dom0>
<div class=mo id=m0>
<h1>dom0</h1>
<div class="nr"></div>
</div>
<div class=mo id=m1>
<h1>dom1</h1><div class="nr"></div>
</div>
<div class=mo id=m2><h1>dom2</h1><div class="nr"></div></div>
<div class=mo id=m3><h1>dom3</h1><div class="nr"></div></div>
</div>
<div class=center id=dom1>
<div class=mo id=m4><h1>dom4</h1><div class="nr"></div></div>
<div class=mo id=m5><h1>dom5</h1><div class="nr"></div></div>
<div class=mo id=m6><h1>dom6</h1><div class="nr"></div></div>
<div class=mo id=m7><h1>dom7</h1><div class="nr"></div></div>
</div>
<div class=right id=dom2>
<div class=mo id=m8><h1>dom8</h1><div class="nr"></div></div>
<div class=mo id=m9><h1>dom9</h1><div class="nr"></div></div>
<div class=mo id=m10><h1>dom10</h1><div class="nr"></div></div>
<div class=mo id=m11><h1>dom11</h1><div class="nr"></div></div>
</div>
</div>
</body>
</html>
#4
谢谢楼上各位童鞋。继续等。
#5
同事已解决:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>拖拽表格的行</title>
</head>
<body>
<table border="1" style="background-color:#999999;" cellpadding="0px;" cellspacing="0px" id="dragBbb" onMouseDown="draginit(this)">
<tr>
<td width="73">序号</td>
<td width="52">姓名</td>
<td width="65">年龄</td>
<td width="83">地址</td>
</tr>
<tr>
<td width="73">1</td>
<td width="52">张三</td>
<td width="65">22</td>
<td width="83">北京海淀</td>
</tr>
<tr>
<td >2</td>
<td>王五</td>
<td>24</td>
<td>上海浦东</td>
</tr>
<tr>
<td>3</td>
<td>朝气</td>
<td>28</td>
<td>广州深圳</td>
</tr>
<tr>
<td>4</td>
<td>反对</td>
<td>27</td>
<td>反对说</td>
</tr>
<tr>
<td>5</td>
<td>离开</td>
<td>25</td>
<td>热舞</td>
</tr>
</table>
<br/>
<br/>
<table border="1" style="background-color:#999999;" cellpadding="0px;" cellspacing="0px" id="dragAaa111" onMouseDown="draginit(this)">
<tr>
<td width="73">序号</td>
<td width="52">姓名</td>
<td width="65">年龄</td>
<td width="83">地址</td>
</tr>
<tr>
<td width="73">1</td>
<td width="52">张三</td>
<td width="65">22</td>
<td width="83">北京海淀</td>
</tr>
<tr>
<td >2</td>
<td>王五</td>
<td>24</td>
<td>上海浦东</td>
</tr>
<tr>
<td>3</td>
<td>朝气</td>
<td>28</td>
<td>广州深圳</td>
</tr>
</table>
<STYLE type=text/css>
table{
cursor:hand;
}
</STYLE>
<script language="javascript" >
var obj;
var xx=0,yy=0;
var tagobj;
var dragobj;
var tableOb;
function draginit(obj){
var tblRows = document.getElementsByTagName("TR"); //获取所有的表格行
tableOb=doClick(obj); //获取表格对象
this.style
for(var i=0;i<tblRows.length;i++){ //遍历每一行
if((tblRows[i].parentNode.parentNode.id).toString().indexOf("drag")!=-1){
tblRows[i].onmousedown=mousedown; //绑定所有的鼠标事件
tblRows[i].ondragover=dragover;
tblRows[i].ondragend=dragend;
tblRows[i].ondrag=dragmove;
tblRows[i].style.position="relative";
tblRows[i].style.zIndex=1;
}
}
}
function mousedown(){ //鼠标按下时的处理
obj = event.srcElement; //获取点击对象
if(obj.tagName=="TD") obj=obj.parentNode; //如果是单元格
if(obj.tagName!="TR") return false; //如果是单元行
if(obj.rowIndex==0) return false; //如果是标题
yy=event.clientY; //鼠标的x坐标和y坐标
xx=event.clientX;
obj.style.zIndex=0;
try{
obj.dragDrop();
}catch(e){
}
}
function dragmove(){ //表格拖拽移动时的位置获取
obj.style.top = event.clientY-yy;
obj.style.left = event.clientX-xx;
}
function dragover(){ //鼠标拖动时的操作
tagobj=event.srcElement;
if(tagobj.tagName=="TD"){tagobj=tagobj.parentNode;}//如果是单元格
if(tagobj.tagName!="TR")return false; //如果是单元行
}
function dragend(){ //拖拽完毕后的处理-高度,宽度等
obj.style.top=0;
obj.style.left=0;
obj.style.zIndex=1;
if(tagobj!=null && tagobj.rowIndex!=0){
var t1=tableOb.rows[obj.rowIndex];
var t2=tableOb.rows[tagobj.rowIndex];
tableOb.getElementsByTagName('tbody')[0].insertBefore(obj,tagobj);//实现表格单元行的互换
tagobj.style.zIndex=1;
}
tagobj=null;
}
//以下是获取表格对象
function searchObjByTagName(obj, tag)
{
while(obj!=null&&typeof(obj.tagName)!="undefind")
{
if(obj.tagName == tag.toUpperCase()) return(obj);
obj = obj.parentElement;
}
return null;
}
function doClick(curTd)
{
ClickedItemName = curTd.id;
var table = searchObjByTagName(curTd,"TABLE");
return table;
}
</script>
</body>
</html>
#6
接分的来了
#1
1
#2
#3
自定义拖拽布局 以前在网上找的
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
float:left;
width:20%;
border:1px solid #FF0000;
margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
</style>
<script>
var dragobj={}
window.onerror=function(){return false}
function on_ini(){
String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
var agent=navigator.userAgent
window.isOpr=agent.inc("Opera")
window.isIE=agent.inc("IE")&&!isOpr
window.isMoz=agent.inc("Mozilla")&&!isOpr&&!isIE
if(isMoz){
Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
}
basic_ini()
}
function basic_ini(){
window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.onload=function(){
on_ini()
var o=document.getElementsByTagName("h1")
for(var i=0;i<o.length;i++){
o[i].onmousedown=function(e){
if(dragobj.o!=null)
return false
e=e||event
dragobj.o=this.parentNode
dragobj.xy=getxy(dragobj.o)
dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
dragobj.o.style.width=dragobj.xy[2]+"px"
dragobj.o.style.height=dragobj.xy[3]+"px"
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
dragobj.o.style.position="absolute"
var om=document.createElement("div")
dragobj.otemp=om
om.style.width=dragobj.xy[2]+"px"
om.style.height=dragobj.xy[3]+"px"
dragobj.o.parentNode.insertBefore(om,dragobj.o)
return false
}
}
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
if(dragobj.o!=null){
dragobj.o.style.width="auto"
dragobj.o.style.height="auto"
dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
dragobj.o.style.position=""
oDel(dragobj.otemp)
dragobj={}
}
}
document.onmousemove=function(e){
e=e||event
if(dragobj.o!=null){
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
createtmpl(e)
}
}
function getxy(e){
var a=new Array()
var t=e.offsetTop;
var l=e.offsetLeft;
var w=e.offsetWidth;
var h=e.offsetHeight;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
a[0]=t;a[1]=l;a[2]=w;a[3]=h
return a;
}
function inner(o,e){
var a=getxy(o)
if(e.x>a[1]&&e.x<(a[1]+a[2])&&e.y>a[0]&&e.y<(a[0]+a[3])){
if(e.y<(a[0]+a[3]/2))
return 1;
else
return 2;
}else
return 0;
}
function createtmpl(e){
for(var i=0;i<12;i++){
if($("m"+i)==dragobj.o)
continue
var b=inner($("m"+i),e)
if(b==0)
continue
dragobj.otemp.style.width=$("m"+i).offsetWidth
if(b==1){
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
}else{
if($("m"+i).nextSibling==null){
$("m"+i).parentNode.appendChild(dragobj.otemp)
}else{
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
}
}
return
}
for(var j=0;j<3;j++){
if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
continue
var op=getxy($("dom"+j))
if(e.x>(op[1]+10)&&e.x<(op[1]+op[2]-10)){
$("dom"+j).appendChild(dragobj.otemp)
dragobj.otemp.style.width=(op[2]-10)+"px"
}
}
}
</script>
</head>
<body>
<div class=content>
<div class=left id=dom0>
<div class=mo id=m0>
<h1>dom0</h1>
<div class="nr"></div>
</div>
<div class=mo id=m1>
<h1>dom1</h1><div class="nr"></div>
</div>
<div class=mo id=m2><h1>dom2</h1><div class="nr"></div></div>
<div class=mo id=m3><h1>dom3</h1><div class="nr"></div></div>
</div>
<div class=center id=dom1>
<div class=mo id=m4><h1>dom4</h1><div class="nr"></div></div>
<div class=mo id=m5><h1>dom5</h1><div class="nr"></div></div>
<div class=mo id=m6><h1>dom6</h1><div class="nr"></div></div>
<div class=mo id=m7><h1>dom7</h1><div class="nr"></div></div>
</div>
<div class=right id=dom2>
<div class=mo id=m8><h1>dom8</h1><div class="nr"></div></div>
<div class=mo id=m9><h1>dom9</h1><div class="nr"></div></div>
<div class=mo id=m10><h1>dom10</h1><div class="nr"></div></div>
<div class=mo id=m11><h1>dom11</h1><div class="nr"></div></div>
</div>
</div>
</body>
</html>
#4
谢谢楼上各位童鞋。继续等。
#5
同事已解决:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>拖拽表格的行</title>
</head>
<body>
<table border="1" style="background-color:#999999;" cellpadding="0px;" cellspacing="0px" id="dragBbb" onMouseDown="draginit(this)">
<tr>
<td width="73">序号</td>
<td width="52">姓名</td>
<td width="65">年龄</td>
<td width="83">地址</td>
</tr>
<tr>
<td width="73">1</td>
<td width="52">张三</td>
<td width="65">22</td>
<td width="83">北京海淀</td>
</tr>
<tr>
<td >2</td>
<td>王五</td>
<td>24</td>
<td>上海浦东</td>
</tr>
<tr>
<td>3</td>
<td>朝气</td>
<td>28</td>
<td>广州深圳</td>
</tr>
<tr>
<td>4</td>
<td>反对</td>
<td>27</td>
<td>反对说</td>
</tr>
<tr>
<td>5</td>
<td>离开</td>
<td>25</td>
<td>热舞</td>
</tr>
</table>
<br/>
<br/>
<table border="1" style="background-color:#999999;" cellpadding="0px;" cellspacing="0px" id="dragAaa111" onMouseDown="draginit(this)">
<tr>
<td width="73">序号</td>
<td width="52">姓名</td>
<td width="65">年龄</td>
<td width="83">地址</td>
</tr>
<tr>
<td width="73">1</td>
<td width="52">张三</td>
<td width="65">22</td>
<td width="83">北京海淀</td>
</tr>
<tr>
<td >2</td>
<td>王五</td>
<td>24</td>
<td>上海浦东</td>
</tr>
<tr>
<td>3</td>
<td>朝气</td>
<td>28</td>
<td>广州深圳</td>
</tr>
</table>
<STYLE type=text/css>
table{
cursor:hand;
}
</STYLE>
<script language="javascript" >
var obj;
var xx=0,yy=0;
var tagobj;
var dragobj;
var tableOb;
function draginit(obj){
var tblRows = document.getElementsByTagName("TR"); //获取所有的表格行
tableOb=doClick(obj); //获取表格对象
this.style
for(var i=0;i<tblRows.length;i++){ //遍历每一行
if((tblRows[i].parentNode.parentNode.id).toString().indexOf("drag")!=-1){
tblRows[i].onmousedown=mousedown; //绑定所有的鼠标事件
tblRows[i].ondragover=dragover;
tblRows[i].ondragend=dragend;
tblRows[i].ondrag=dragmove;
tblRows[i].style.position="relative";
tblRows[i].style.zIndex=1;
}
}
}
function mousedown(){ //鼠标按下时的处理
obj = event.srcElement; //获取点击对象
if(obj.tagName=="TD") obj=obj.parentNode; //如果是单元格
if(obj.tagName!="TR") return false; //如果是单元行
if(obj.rowIndex==0) return false; //如果是标题
yy=event.clientY; //鼠标的x坐标和y坐标
xx=event.clientX;
obj.style.zIndex=0;
try{
obj.dragDrop();
}catch(e){
}
}
function dragmove(){ //表格拖拽移动时的位置获取
obj.style.top = event.clientY-yy;
obj.style.left = event.clientX-xx;
}
function dragover(){ //鼠标拖动时的操作
tagobj=event.srcElement;
if(tagobj.tagName=="TD"){tagobj=tagobj.parentNode;}//如果是单元格
if(tagobj.tagName!="TR")return false; //如果是单元行
}
function dragend(){ //拖拽完毕后的处理-高度,宽度等
obj.style.top=0;
obj.style.left=0;
obj.style.zIndex=1;
if(tagobj!=null && tagobj.rowIndex!=0){
var t1=tableOb.rows[obj.rowIndex];
var t2=tableOb.rows[tagobj.rowIndex];
tableOb.getElementsByTagName('tbody')[0].insertBefore(obj,tagobj);//实现表格单元行的互换
tagobj.style.zIndex=1;
}
tagobj=null;
}
//以下是获取表格对象
function searchObjByTagName(obj, tag)
{
while(obj!=null&&typeof(obj.tagName)!="undefind")
{
if(obj.tagName == tag.toUpperCase()) return(obj);
obj = obj.parentElement;
}
return null;
}
function doClick(curTd)
{
ClickedItemName = curTd.id;
var table = searchObjByTagName(curTd,"TABLE");
return table;
}
</script>
</body>
</html>
#6
接分的来了