DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 一個可拖拽列寬表格實例演示
一個可拖拽列寬表格實例演示
編輯:關於JavaScript     
復制代碼 代碼如下:
<DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style>
body{
font:12px/1.5 Tahoma;
}
#gannt_left{
width:500px;
}
#left-scroll-panel{
width:520px;
height:100px;
overflow-y: auto;
}
table{
table-layout:fixed;
border-collapse: collapse;
border-spacing: 0px;
text-align:center;
width:500px;
}
table,th,td{
border:1px solid #afe0ea;
}
th,td{
height:20px;
line-height:20px;
overflow: hidden;
text-overflow:ellipsis;
white-space:nowrap;
word-wrap:normal;
word-break:keep-all;
}
th{
background:#adf5ff;
}
td{
background:#f6fcff;
}
#gannt_grid,#gannt_grid td{
border-top:0px none;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<div id="gannt_left">
<div id="left-scroll-panel" class="scroll-panel">
<table id="gannt_grid">
<thead>
<tr>
<th width="30">序號</th>
<th width="35">操作</th>
<th>標題</th>
<th width="80">執行人</th>
<th width="80">開始時間</th>
<th width="80">結束時間</th>
<th width="30">天數</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>編輯</td>
<td>任務標題:阿斯卡是大家啊</td>
<td>Firefox</td>
<td>2012-07-15</td>
<td>2012-08-15</td>
<td>32</td>
</tr>
<tr>
<td>2</td>
<td>編輯</td>
<td>任務標題:阿斯卡是大家啊</td>
<td>Firefox</td>
<td>2012-07-15</td>
<td>2012-08-15</td>
<td>32</td>
</tr>
<tr>
<td>3</td>
<td>編輯</td>
<td>任務標題:阿斯卡是大家啊</td>
<td>Firefox</td>
<td>2012-07-15</td>
<td>2012-08-15</td>
<td>32</td>
</tr>
<tr>
<td>4</td>
<td>編輯</td>
<td>任務標題:阿斯卡是大家啊</td>
<td>Firefox</td>
<td>2012-07-15</td>
<td>2012-08-15</td>
<td>32</td>
</tr>
<tr>
<td>5</td>
<td>編輯</td>
<td>任務標題:阿斯卡是大家啊</td>
<td>Firefox</td>
<td>2012-07-15</td>
<td>2012-08-15</td>
<td>32</td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
(function(){ //fixed table header ,還可以封裝一下做成類
var leftScrollPanel = $("#left-scroll-panel");
var ganntBody = $("#gannt_grid>tbody");
var fixedThead = leftScrollPanel.prev(".fixed-header-tb");
if(!fixedThead.length){
fixedThead = $('<table class="fixed-header-tb"></table>');
fixedThead.append(ganntBody.prev());
leftScrollPanel.before(fixedThead);
}else{
//do not create table head again when gannt body repaint,
//because it is not created in function render
ganntBody.prev().remove();
}
var tds = ganntBody.find("tr:first>td");
var ths = fixedThead.find("th");
var thWidth;
$.each(tds,function(index,td){
//jq width() or css('width') may has 1px disparity, use attr width
thWidth = ths.eq(index).attr("width");
(thWidth!=undefined) && $(td).attr("width",thWidth);
});
})();
(function(){//table header resize
var sideOffset = {
left:null,
right:null,
td:null,
tdLocked:null,
tdLeft:null,
tdRight:null
};
var pos = {
resizedTime:0,
beginPos:0
};
var tableHead = $(".fixed-header-tb").find("tr:first");
var headCellTagName = "th";
var bodyHead = $("#gannt_grid>tbody").find("tr:first");
var minInterVal = 0;
var minWidth = 30;
var borderBeside = 5;
var notResizeCells = [0,1,6];
var freeCells = [2];
var forceSize = false;
var resizeAllow = false;
var resizing = false;
var forbiddenResize = function(){
var frag = false;
var o = sideOffset;
var index = o.td.index();
if($.inArray(index,notResizeCells)>-1){
frag = true;
}else if((index==0||$.inArray(index-1,notResizeCells)>-1)&&o.left<=borderBeside){
frag = true;
}else if((index==o.td.siblings().length||$.inArray(index+1,notResizeCells)>-1)&&o.right<=borderBeside){
frag = true;
}else if(o.left>borderBeside&&o.right>borderBeside){
frag = true;
}
return frag;
};
var stopResize = function(){
if(!resizing){return ;}
resizing = false;
resizeAllow = false;
sideOffset = {
left:null,
right:null,
td:null,
tdLocked:null,
tdLeft:null,
tdRight:null
};
};
var isFreeCell = function(td){
return forceSize==false && $.inArray(td.index(),freeCells)!=-1;
};
tableHead.bind({
mousemove:function(e){
var th = $(e.target).closest(headCellTagName);
if(!th.length){
return;
}
if(!resizing){
sideOffset.td = th;
sideOffset.left = e.pageX - th.offset().left;
sideOffset.right = th.width()-(e.pageX-th.offset().left);
if(forbiddenResize()){
resizeAllow = false;
sideOffset.td.css("cursor","default");
}else{
resizeAllow = true;
sideOffset.td.css("cursor","e-resize");
pos.resizedTime = new Date()*1;
pos.beginPos = e.pageX;
}
return;
}
if(sideOffset.tdLocked){
th = sideOffset.tdLocked;
}
if(new Date()-pos.resizedTime<minInterVal){
return;
}else{
pos.resizedTime = new Date()*1;
}
var offset = (e.pageX-pos.beginPos);
if(!offset){
return;
}else{
pos.beginPos = e.pageX;
}
var leftWidth = sideOffset.tdLeft.width();
var rightWidth = sideOffset.tdRight.width();
if(offset<0&&leftWidth==minWidth){
return;
}else if(offset>0&&rightWidth==minWidth){
return;
}
var fixedLWidth,fixedRWidth;
if(leftWidth-Math.abs(offset)<minWidth&&offset<0){
fixedLWidth = minWidth;
fixedRWidth = rightWidth - (minWidth-leftWidth);
}else if(rightWidth-offset<minWidth&&offset>0){
fixedRWidth = minWidth;
fixedLWidth = leftWidth - (minWidth-rightWidth);
}else{
fixedLWidth = leftWidth+offset;
fixedRWidth = rightWidth-offset;
}
var adjustCells = [
{cell:sideOffset.tdLeft,width:fixedLWidth},
{cell:sideOffset.tdRight,width:fixedRWidth}
];
if(offset<0){
adjustCells = adjustCells.reverse();
}
var inOneTable = bodyHead.parents("table:first").get(0)==tableHead.parents("table:first").get(0);
$.each(adjustCells,function(i,cellConf){
if(isFreeCell(cellConf.cell)){return;}
cellConf.cell.attr("width",cellConf.width);
if(!inOneTable){
bodyHead.children().eq(cellConf.cell.index()).attr("width",cellConf.width);
}
});
},
mousedown:function(){
if(!resizeAllow){
return;
}
sideOffset.tdLocked = sideOffset.td;
if(sideOffset.left<=5){
sideOffset.tdRight = sideOffset.td;
sideOffset.tdLeft = sideOffset.td.prev();
}else{
sideOffset.tdRight = sideOffset.td.next();
sideOffset.tdLeft = sideOffset.td;
}
resizing = true;
return false;
}
});
$(document).bind("mouseup",stopResize);
})();
</script>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved