JQuery table column widths can be drag and drop and are firfox compatible

  • 2020-03-30 03:49:09
  • OfStack

This demo USES jQuery package to realize the drag-and-drop function of table column width and relayout when the page is reset. Using jQuery, convenient function call, to the table to be processed to add id, directly call $("#id").

The code is as follows:


<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<mce:script type="text/javascript" src="jquery-1.3.2.min.js" mce_src="jquery-1.3.2.min.js"></mce:script> 
<mce:style><!-- 
.resizeDivClass 
{ 
position:absolute; 
background-color:gray; 
width:2px; 
height:15px; 
z-index:1px; 
display: block; 
cursor:e-resize 
} 
.td1 { 
font-size: 12px; 
white-space:nowrap; 
color:#0000ff; 
} 
--></mce:style><style mce_bogus="1">.resizeDivClass 
{ 
position:absolute; 
background-color:gray; 
width:2px; 
height:15px; 
z-index:1px; 
display: block; 
cursor:e-resize 
} 
.td1 { 
font-size: 12px; 
white-space:nowrap; 
color:#0000ff; 
}</style> 
<mce:script language=javascript><!-- 
/* 
 Title: expand drag column table demo 1.2 
 Design: lu songqiang  
 Blog: http://hi.csdn.net/andensy 
 Date: 2010 years 4 month 26 day  
 Description: modified firfox Compatibility issues, can be very compatible with firefox browser  
*/ 
 
(function($){ 
//Use regular expressions to determine the version of jQuery
if (/1/.(0|1|2)/.(0|1|2|3|4|5)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) { 
alert('movedTh  Need to be  jQuery v1.2.6  Future version support !  What you're using is  v' + $.fn.jquery); 
return; 
} 
me=null; 
var ps=3; 
$.fn.movedTh=function(){ 
me=this; 
var target = null; 
var tempStr = ""; 
var i=0; 
$(me).find("tr:first").find("th").each(function(){ 
tempStr = '<div id="mydiv'+i+'"onmousedown="$().mousedone.movedown(event,this)" ></div>'; 
var div={}; 
$(this).html($(this).html()+tempStr); 
var offset = $(this).offset(); 
var pos=offset.left +$(this).width()+ me.offset().left-ps; 
$("#mydiv"+i).addClass("resizeDivClass"); 
$("#mydiv"+i).css("left",pos); 
$("#mydiv"+i).css("top",(offset.top+2)); 
i++; 
}); //end each 
} //end moveTh 
$.fn.mousedone={ 
movedown:function(e,obj){ 
var d=document; 
var e = window.event||e ; 
var myX = e.clientX||e.pageX; 
obj.mouseDownX=myX ; 
obj.pareneTdW=$(obj).parent().width(); //obj.parentElement.offsetWidth; 
obj.pareneTableW=me.width(); 
if(obj.setCapture){ 
obj.setCapture(); 
}else if(window.captureEvents){ 
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP); 
} 
d.onmousemove=function(e){ 
var dragData=obj; 
var event = window.event||e ; 
if(!dragData.mouseDownX) return false; 
var newWidth=dragData.pareneTdW*1+(event.clientX||event.pageX)*1-dragData.mouseDownX; 
if(newWidth>0) 
{ 
$(obj).parent().width(newWidth); 
me.width(dragData.pareneTableW*1+(event.clientX||event.pageX)*1-dragData.mouseDownX); 
var k=0; 
me.find("tr:first").find("th").each(function(){ 
var offset = $(this).offset(); 
var pos=offset.left*1+$(this).width()*1+me.offset().left*1-ps; 
$("#mydiv"+k).css("left",pos); 
k++; 
}) //end each 
}//end if 
}; 
d.onmouseup=function(e){ 
var dragData=obj; 
if(dragData.setCapture) 
{ 
dragData.releaseCapture(); 
}else if(window.captureEvents){ 
     window.releaseEvents(e.MOUSEMOVE|e.MOUSEUP); 
} 
dragData.mouseDownX=0; 
} 
} 
} //end mousedone 
$(window).resize(function(){ 
setTimeout(function() { 
var target = null; 
var tempStr = ""; 
var i=0; 
$(me).find("tr:first").find("th").each(function(){ 
tempStr = '<div id="mydiv'+i+'"onmousedown="$().mousedone.movedown(event,this)" ></div>'; 
var div={}; 
$(this).html($(this).html()+tempStr); 
var offset = $(this).offset(); 
var pos=offset.left +$(this).width()+ me.offset().left-ps; 
$("#mydiv"+i).addClass("resizeDivClass"); 
$("#mydiv"+i).css("left",pos); 
i++; 
}); //end each 
}, 10); 
}); 
})(jQuery) 
$().ready(function(){ 
$("#tab").movedTh(); 
}) 
// --></mce:script> 
</head> 
<body onload=""> 
<table cellpadding="3" id='' STYLE="table-layout:fixed;" mce_STYLE="table-layout:fixed;" > 
<tr bgcolor=cccccc > 
<th valign=top class="td1" ><nobr> change table The column width of </nobr><span style="color:#069" mce_style="color:#069">|</span></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr><span style="color:#069" mce_style="color:#069">|</span></th> 
</tr></table> 
<br> 
<table cellpadding="3" id='tab' STYLE="table-layout:fixed;" mce_STYLE="table-layout:fixed;" > 
<tr bgcolor=cccccc > 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
<th valign=top class="td1" ><nobr> change table The column width of </nobr></th> 
</tr> 
<tr> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
</tr> 
<tr> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
</tr> 
<tr> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
</tr> 
<tr> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
<td class="td1"><nobr> change table The column width of the </nobr></td> 
</tr> 
</table> 
</body> 
</html>

Related articles: