87,910
社区成员
发帖
与我相关
我的任务
分享
function SetTableWH(divID,tableID,rowCount,columnCount)
{
//设置div、table的宽度高度
var divMain = $("#" + divID)[0];
var dTop = $("#" + divID + " tr:first th")[0];
//alert($("#" + divID + " table").height());
var tmpDivWidth; //div的总宽度
var tmpDivHeight; //div的总高度
var tmpRowWidth; //锁定行的table的宽度
var tmpRowHeight = 0; //锁定行的table的高度
var tmpColWidth = 0; //锁定列的table的宽度
var tmpColHeight; //锁定列的table的高度
tmpDivWidth = $("#" + divID).width();
tmpDivHeight = $("#" + divID).height();
tmpRowWidth =$("#" + divID + " table").width();
tmpColHeight = $("#" + divID + " table").height();
for (i=0;i<rowCount;i++)
{//锁定行的总高度
tmpRowHeight += getRowMaxHeight($("#" + divID + " table tr:eq("+i+") td"));
//alert("总高度_" + i + "_" + tmpRowHeight);
}
for (i=0;i<columnCount;i++)
{
//锁定列的总宽度
tmpColWidth += $("#" + divID + " table tr:eq(0) td:eq("+i+")").width();
}
//设置行的宽高
$("#Div_Top").width(tmpDivWidth - 24);
$("#Tbl_Top").width(tmpRowWidth);
$("#Div_Top").height(tmpRowHeight);
$("#Tbl_Top").height(tmpRowHeight);
//设置列宽高
$("#Div_Left").width(tmpColWidth);
$("#Tbl_Left").width(tmpColWidth);
$("#Div_Left").height(tmpDivHeight - 24);
$("#Tbl_Left").height(tmpColHeight);
alert($("#Tbl_Left").height() + "_"+ tmpColHeight + "_" + $("#" + divID + " table").height());
//交叉的部分
$("#Div_TopLeft").width(tmpColWidth);
$("#Tbl_TopLeft").width(tmpColWidth);
$("#Div_TopLeft").height(tmpRowHeight);
$("#Tbl_TopLeft").height(tmpRowHeight);
//设置多顶行的每个td的宽度
$("#Tbl_Top tr:eq(0) td").each(function (i){
$(this).width($("#" + divID + " table tr:eq(0) td:eq(" + i + ")").width());
});
//设置锁定列的每个tr的高度
/*
$("#Tbl_Left tr").each(function (i){
//$(this).height(getRowMaxHeight($("#" + divID + " table tr:eq("+i+") td")));
//alert($("#" + divID + " table tr(" + i + ")"));
$(this).height($("#" + divID + " table tr:eq(" + i + ")").height());
});
*/
var trIndex = 0;
var tmpH = 0;
//var tdIndex = 0;
//设置锁定列的每个td的高度
$("#Tbl_Left tr td").each(function (i){
trIndex = parseInt(i / columnCount);
tmpH = $("#" + divID + " table tr:eq(" + trIndex + ") td").height();
//$(this).height(tmpH);
this.style.height = tmpH;
//查看高度
a = i+ "_" + $(this).height() + "——" + tmpH + ":";
a += $("#" + divID + " table tr:eq(" + trIndex + ") td").height();
//alert (a);
});
alert($("#Tbl_Left").height());
alert($("#" + divID + " table").height());
}
function SetTableXY(divID,tableID,rowCount,columnCount)
{
alert("设置位置");
var divTop=$("#Div_Top")[0];
var divLeft=$("#Div_Left")[0];
var divTopLeft=$("#Div_TopLeft")[0];
var offset = $("#"+divID).offset(); //左上角的坐标
divTop.style.top = offset.top;
divTop.style.left = offset.left;
divLeft.style.top = offset.top;
divLeft.style.left = offset.left;
divTopLeft.style.top = offset.top;
divTopLeft.style.left = offset.left;
}
function SetTableInfo(divID,tableID,rowCount,columnCount)
{
alert("开始复制属性");
//复制三个div的属性
$("#Div_Top,#Div_Left,#Div_TopLeft").each(function (){
this.mergeAttributes( $("#"+divID)[0]);
});
//移除scroll事件
$("#Div_Top,#Div_Left,#Div_TopLeft").removeAttr("onscroll");
$("#Div_Top,#Div_Left,#Div_TopLeft").removeAttr("style");
$("#Div_Top,#Div_Left,#Div_TopLeft").attr("style","clear: none; overflow: hidden; width: 0px; height: 0px;POSITION:absolute;LEFT: 100px; TOP: 100px;background-color:#ffffff");
//复制三个table的属性
$("#Tbl_Top,#Tbl_Left,#Tbl_TopLeft").each(function (){
this.mergeAttributes($("#"+tableID)[0]);
});
//复制锁定行的每个tr的属性
$("#Tbl_Top tr").each(function (i){
//alert(this.innerHTML);
this.mergeAttributes($("#"+tableID + " tr:eq("+i+")")[0]);
});
//复制锁定行的每个td的属性
var trIndex = 0;
var tdIndex = 0;
var tdCount = $("#"+ tableID +" tr:eq(0) td").length;
$("#Tbl_Top td").each(function (i){
trIndex = parseInt(i / tdCount);
tdIndex = i % tdCount;
//alert(i +"_"+ trIndex +"_"+ tdIndex);
this.mergeAttributes($("#"+tableID + " tr:eq("+trIndex+") td:eq("+ tdIndex +")")[0]);
});
//复制锁定列的每个tr的属性
$("#Tbl_Left tr").each(function (i){
//alert(this.innerHTML);
this.mergeAttributes($("#"+tableID + " tr:eq("+i+")")[0]);
});
//复制锁定列的每个td的属性
trIndex = 0;
tdIndex = 0;
$("#Tbl_Left td").each(function (i){
trIndex = parseInt(i / columnCount);
tdIndex = i % columnCount;
//alert(i +"_"+ trIndex +"_"+ tdIndex);
this.mergeAttributes($("#"+tableID + " tr:eq("+trIndex+") td:eq("+ tdIndex +")")[0]);
});
}
function divScroll(divMain)
{
//alert("外部滚动事件");
var divTop=$("#Div_Top");
var divLeft=$("#Div_Left");
//alert(divMain.scrollLeft);
divTop.scrollLeft(divMain.scrollLeft);
divLeft.scrollTop(divMain.scrollTop);
}
function getRowMaxHeight(tr)
{
//获取指定的行的最大的高度
//alert(tr.length);
var maxHeight = 0;
tr.each(function (){
//alert($(this).height());
if ($(this).height() > maxHeight)
maxHeight = $(this).height();
});
return maxHeight;
}
function myScroll(divID,tableID,rowCount,columnCount)
{
alert("开始添加行");
//copy 三个div和table
CreatDiv(divID,tableID,rowCount,columnCount)
//copy属性、css和事件
SetTableInfo(divID,tableID,rowCount,columnCount);
//设置table的宽度和高度
SetTableWH(divID,tableID,rowCount,columnCount);
//定位
SetTableXY(divID,tableID,rowCount,columnCount);
}
function CreatDiv(divID,tableID,rowCount,columnCount)
{
//锁定行列的三个DIV、三个table
var DivTop=$("<div></div>");
var DivLeft=$("<div></div>");
var DivTopLeft=$("<div></div>");
var TblTop=$("<table></table>");
var TblLeft=$("<table></table>");
var TblTopLeft=$("<table></table>");
DivTop.attr("id","Div_Top");//POSITION:absolute;LEFT: 10px; TOP: 10px;
//DivTop.attr("style","clear: none; overflow: hidden; width: 0px; height: 0px;POSITION:absolute;LEFT: 100px; TOP: 100px;");
DivLeft.attr("id","Div_Left");
//DivLeft.attr("style","clear: none; overflow: hidden; width: 0px; height: 0px;POSITION:absolute;LEFT: 100px; TOP: 100px;");
DivTopLeft.attr("id","Div_TopLeft");
//DivTopLeft.attr("style","clear: none; overflow: hidden; width: 0px; height: 0px;POSITION:absolute;LEFT: 100px; TOP: 100px;");
TblTop.attr("id","Tbl_Top");
TblTop.attr("style","clear: none; width: 0px; height: 0px;");
TblLeft.attr("id","Tbl_Left");
TblLeft.attr("style","clear: none; width: 0px; height: 0px;");
TblTopLeft.attr("id","Tbl_TopLeft");
TblTopLeft.attr("style","clear: none; width: 0px; height: 0px;");
//注册事件
//alert($("#"+ tableID +" tr:eq(0) td:eq(0)")[0].innerHTML);
for (i =0;i<rowCount;i++)
{
//添加要锁定的行
TblTop.append("<tr>"+$("#"+ tableID +" tr:eq("+ i +")")[0].innerHTML+"</tr>");
}
//添加要锁定的列
var tableRowsCount ;
var tmpTD;
tableRowsCount = $("#"+ tableID +" tr").length; //.children("tr").length ;
//alert (tableRowsCount);
for (a =0;a<tableRowsCount;a++)
{
tmpTD="";
for (b =0;b<columnCount;b++)
{
//添加要锁定的列
tmpTD += "<td>" + $("#"+ tableID +" tr:eq("+ a +") td:eq("+ b +")")[0].innerHTML + "</td>";
}
TblLeft.append("<tr>"+tmpTD+"</tr>");
}
//添加行列重叠的部分
if (rowCount + columnCount >1)
{
for (a =0;a<rowCount;a++)
{
tmpTD="";
for (b =0;b<columnCount;b++)
{
//添加要锁定的列
tmpTD += "<td>" + $("#"+ tableID +" tr:eq("+ a +") td:eq("+ b +")")[0].innerHTML + "</td>";
}
TblTopLeft.append("<tr>"+tmpTD+"</tr>");
}
}
DivTop.append(TblTop);
DivLeft.append(TblLeft);
DivTopLeft.append(TblTopLeft);
$("body").append(DivTop);
$("body").append(DivLeft);
$("body").append(DivTopLeft);
}