DOM创建表格填充的数据,合并table首列时单元格内容错位

baidu_38497344 2017-06-29 05:50:22
如题,请问这种情况应该怎么修改?
如图:

页面源码:

填充数据代码:
 rootDest = this.sDataTable.tBodies[0];
rootSource = this.sourceTable.tBodies[0];

for (i = (this.sourceTable.tHead) ? 0 : this.headerRows; i < this.tBodyRowCount; i++) {

dest = rootDest.rows[(this.sourceTable.tHead) ? i : i - this.headerRows];
source = rootSource.rows[i];

for (j = 0, k = (this.columnCount - this.fixedCols); j < k; j++) {
try {
dest.cells[j].firstChild.innerHTML = source.cells[j + this.fixedCols].innerHTML;
} catch (e) { }
}
}

页面js:

<script type="text/javascript" src="../JS/superTables.js"></script>
<script type="text/javascript">
var grid = document.getElementById("<%=avayatel.ClientID%>");
//alert(grid);
if (grid != null && grid != undefined) {
grid.parentNode.className = "fakeContainer";
(function () {
superTable("<%=avayatel.ClientID%>", { cssSkin: "Default" });
})();
}
</script>

superTables.js文件

var superTables = [];
var superTable = function (tableId) {
// add this instance to the supertables array 将此实例添加到supertables数组中
var x = superTables.length;
this.index = x;
superTables[x] = this;

// initialize the parameters 初始化参数
this.sourceTable = document.getElementById(tableId);
this.hasOptions = (arguments.length > 1) ? true : false;
this.skin = (this.hasOptions && arguments[1].cssSkin) ? "_" + arguments[1].cssSkin : "_Default";
this.headerRows = (this.hasOptions && arguments[1].headerRows) ? parseInt(arguments[1].headerRows) : 1;
this.fixedCols = (this.hasOptions && arguments[1].fixedCols) ? parseInt(arguments[1].fixedCols) : 0;
this.columnWidths = (this.hasOptions && arguments[1].colWidths) ? arguments[1].colWidths : [];
this.callbackFunc = (this.hasOptions && arguments[1].onFinish) ? arguments[1].onFinish : function () { return this; };
var i, j, k;

// get the column widths 获取列宽
this.columnCount = (this.sourceTable.tHead) ? this.sourceTable.tBodies[0].rows[0].cells.length : this.sourceTable.tBodies[0].rows[this.headerRows].cells.length;
this.columnWidthSum = 0;
this.sourceTable.className = "sTempTable";
for (i=0; i<this.columnCount; i++) {
if (!this.columnWidths[i] || this.columnWidths[i] === -1) {
this.columnWidths[i] = (this.sourceTable.tHead) ? this.sourceTable.tBodies[0].rows[0].cells[i].offsetWidth : this.sourceTable.tBodies[0].rows[this.headerRows].cells[i].offsetWidth;
}
this.columnWidthSum += this.columnWidths[i];
}

// create the framework dom 创建dom的框架
this.sParent = this.sourceTable.parentNode;
this.sParentHeight = this.sParent.clientHeight - (this.sParent.offsetHeight - this.sParent.clientHeight);
this.sParentWidth = this.sParent.clientWidth - (this.sParent.offsetWidth - this.sParent.clientWidth);
this.sParent.removeChild(this.sourceTable);

this.sBase = document.createElement("DIV");
this.sFHeader = this.sBase.cloneNode(false);
this.sHeader = this.sBase.cloneNode(false);
this.sFData = this.sBase.cloneNode(false);
this.sData = this.sBase.cloneNode(false);

var rowMold = ["<TR>\n"];
for (i=0, j=(this.columnCount - this.fixedCols); i<j; i++) {
rowMold.push("<TD><DIV> </DIV></TD>");
}
rowMold.push("\n</TR>\n");
rowMold = rowMold.join("");

var headerMold = ["\n<TABLE><TBODY>\n" + rowMold];
for (i=1; i<this.headerRows; i++) {
headerMold.push(rowMold);
}
headerMold.push("</TBODY></TABLE>\n");
this.sHeader.innerHTML = headerMold.join("");
this.sHeaderTable = this.sHeader.getElementsByTagName("TABLE")[0];

// fill the header dom framework 填充头dom框架
var rootDest, rootSource, dest, source;

rootDest = this.sHeaderTable.tBodies[0];
rootSource = (this.sourceTable.tHead) ? this.sourceTable.tHead : this.sourceTable.tBodies[0];
for (i=0; i<this.headerRows; i++) {
dest = rootDest.rows[i];
source = rootSource.rows[i];
for (j=0, k=(this.columnCount - this.fixedCols); j<k; j++) {
try {
dest.cells[j].firstChild.innerHTML = source.cells[j + this.fixedCols].innerHTML;
dest.cells[j].style.width = this.columnWidths[j + this.fixedCols] + "px";
} catch (e) { }
}
}

// create the data dom framework 创建数据dom框架
this.tBodyRowCount = this.sourceTable.tBodies[0].rows.length;

var dataMold = ["\n<TABLE><TBODY>\n"];
for (i=(this.sourceTable.tHead) ? 0 : this.headerRows; i<this.tBodyRowCount; i++) {
dataMold.push(rowMold);
}
dataMold.push("</TBODY></TABLE>\n");
this.sData.innerHTML = dataMold.join("");
this.sDataTable = this.sData.getElementsByTagName("TABLE")[0];

// fill the data dom framework 填充数据dom框架
if (this.fixedCols > 0) {
rootDest = this.sFDataTable.tBodies[0];
rootSource = this.sourceTable.tBodies[0];
for (i=(this.sourceTable.tHead) ? 0 : this.headerRows; i<this.tBodyRowCount; i++) {
dest = rootDest.rows[(this.sourceTable.tHead) ? i : i - this.headerRows];
source = rootSource.rows[i];
for (j=0; j<this.fixedCols; j++) {
try {
dest.cells[j].firstChild.innerHTML = source.cells[j].innerHTML;
} catch (e) { }
}
}
for (i=0; i<this.fixedCols; i++) {
rootDest.rows[0].cells[i].style.width = this.columnWidths[i] + "px";
}
}
rootDest = this.sDataTable.tBodies[0];
rootSource = this.sourceTable.tBodies[0];
for (i=(this.sourceTable.tHead) ? 0 : this.headerRows; i<this.tBodyRowCount; i++) {
dest = rootDest.rows[(this.sourceTable.tHead) ? i : i - this.headerRows];
source = rootSource.rows[i];
for (j=0, k=(this.columnCount - this.fixedCols); j<k; j++) {
try {
dest.cells[j].firstChild.innerHTML = source.cells[j + this.fixedCols].innerHTML;
} catch (e) { }
}
}
for (i=0, j=(this.columnCount - this.fixedCols); i<j; i++) {
rootDest.rows[0].cells[i].style.width = this.columnWidths[i + this.fixedCols] + "px";
}

// place everything into the page 把所有东西都放到页面里
if (this.fixedCols > 0) { this.sBase.appendChild(this.sFHeader); }
this.sBase.appendChild(this.sHeader);
if (this.fixedCols > 0) { this.sBase.appendChild(this.sFData); }
this.sBase.appendChild(this.sData);

// style the tables
this.sBase.className = "sBase" + this.skin;
this.sFHeader.className = "sFHeader" + this.skin;
this.sHeader.className = "sHeader" + this.skin;
this.sFData.className = "sFData" + this.skin;
this.sData.className = "sData" + this.skin;

// render and get needed dimensions 渲染和获取所需的维度
this.sParent.appendChild(this.sBase);
if (this.fixedCols > 0) {
this.sFHeaderTableOffsetWidth = this.sFHeaderTable.offsetWidth;
}
this.sHeaderTableOffsetHeight = this.sHeaderTable.offsetHeight;

// align the tables 对齐表
if (this.fixedCols > 0) {
this.sHeader.style.right = (this.sFHeaderTableOffsetWidth * -1) + "px";
this.sData.style.marginLeft = this.sFHeaderTableOffsetWidth + "px";
this.sData.style.width = (this.sParentWidth - this.sFHeaderTableOffsetWidth) + "px";
}
this.sData.style.height = (this.sParentHeight - this.sHeaderTableOffsetHeight) + "px";
this.sData.style.overflow = "auto";
if (this.fixedCols > 0) {
if (this.sParentWidth < this.columnWidthSum) {
this.sHorOverflow = document.createElement("DIV");
this.sHorOverflow.className = "sHorOverflow";
this.sHorOverflow.style.width = this.sFHeaderTableOffsetWidth + "px";
this.sBase.appendChild(this.sHorOverflow);
}
}
if (this.sParentHeight < (this.tBodyRowCount * this.sHeaderTableOffsetHeight)) {
this.sVertOverflow = document.createElement("DIV");
this.sVertOverflow.className = "sVertOverflow";
this.sVertOverflow.style.height = this.sHeaderTableOffsetHeight + "px";
this.sBase.appendChild(this.sVertOverflow);
}

// set up table events 设置表的事件
if (this.fixedCols > 0) {
this.sData.onscroll = function () {
superTables[x].sHeader.style.right = (superTables[x].sData.scrollLeft - superTables[x].sFHeaderTableOffsetWidth) + "px";
superTables[x].sFData.style.top = ((superTables[x].sData.scrollTop * -1) + superTables[x].sHeaderTableOffsetHeight) + "px";
return true;
}
} else {
this.sData.onscroll = function () {
superTables[x].sHeader.style.right = superTables[x].sData.scrollLeft + "px";
return true;
}
}

return this.callbackFunc();
}
...全文
402 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2017-08-31
  • 打赏
  • 举报
回复
列宽自动分配,无法合并
baidu_38497344 2017-06-30
  • 打赏
  • 举报
回复
调试时dest的值: 第一行: <td><div>湖北分部</div></td><td><div>湖北</div></td><td><div>武汉</div></td><td><div>分部经理</div></td><td><div>曹志端</div></td><td><div>6100</div></td> 第二行: <td><div>重庆</div></td><td><div>重庆</div></td><td><div>大团经理</div></td><td><div>张美琳</div></td><td><div>6101</div></td><td><div> </div></td>

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧