1.取得当前节点的所有子结点
var childNodes=oSrcRow.getAttribute("domNode").childNodes;
for(var i=0;i<childNodes.length;i++){
var childNode=childNodes[i];
addNewRowInOneTable('tb2',childNode);
}
2.实现addNewRowInOneTable的方法
function addNewRowInOneTable(tableId,childNode){
var oTable = document.getElementById(tableId);
if(oTable.tagName != "TABLE")
alert("Err 5002");
var oList = oTable.children;
var oTBODY;
for(var i=0;i<oList.length;i++) {
if(oList[i].tagName == "TBODY") {
oTBODY = oList[i];
break;
}
}
// var oTR = oTBODY.lastChild; // 取得这个子结点的下一行,以便用insertBefore方法插入 var oTR=getNextTR(oTBODY,childNode);
if(!oTR){
oTR=oTBODY.lastChild;
var newTR=setNewTR(oTR,childNode,oTBODY); oTBODY.insertAdjacentElement("beforeEnd",newTR);
}else{
var newTR=setNewTR(oTR,childNode,oTBODY);
oTBODY.insertBefore(newTR,oTR);
}
}
3.实现setNewTR方法,得到新的一行
function setNewTR(oTR,childNode,oTBODY){
var newTR =oTBODY.firstChild.cloneNode(true);
newTR.style.display="";
newTR.firstChild.firstChild.setAttribute("value",childNode.getAttribute("prikey"));
//设置隐藏域的value为地区的代码
return newTR;
}
4.根据当前节点得到在位置上对应的下一行,下一行位置为当前节点的下一个兄弟节点对应的行或者当前节点的父节点的下一个兄弟节点。
function getNextTR(oTBODY,childNode){
var allTableRows= oTBODY.childNodes;
var thisNode=childNode.parentNode;
var inputvalue=null;
var brothernode=getBrotherNode(thisNode);
if(brothernode){
inputvalue=brothernode.getAttribute("prikey");
for(var i=0;i<allTableRows.length;i++){ if(parseInt(allTableRows[i].firstChild.firstChild.value)==inputvalue){
return allTableRows[i];
}//end if
}
}else{
//alert("dim");
return null;
}
}
5.遍历得到兄弟节点
function getBrotherNode(thisnode){
var brothernode=thisnode.nextSibling;
if(brothernode){
return brothernode;
}
else {
var parentnode=thisnode.parentNode;
if(parentnode){
return getBrotherNode(parentnode); //否则继续找其父节点的下一个同级节点 }else{
return null;
}
}
}
var childNodes=oSrcRow.getAttribute("domNode").childNodes;
for(var i=0;i<childNodes.length;i++){
var childNode=childNodes[i];
addNewRowInOneTable('tb2',childNode);
}
2.实现addNewRowInOneTable的方法
function addNewRowInOneTable(tableId,childNode){
var oTable = document.getElementById(tableId);
if(oTable.tagName != "TABLE")
alert("Err 5002");
var oList = oTable.children;
var oTBODY;
for(var i=0;i<oList.length;i++) {
if(oList[i].tagName == "TBODY") {
oTBODY = oList[i];
break;
}
}
// var oTR = oTBODY.lastChild; // 取得这个子结点的下一行,以便用insertBefore方法插入 var oTR=getNextTR(oTBODY,childNode);
if(!oTR){
oTR=oTBODY.lastChild;
var newTR=setNewTR(oTR,childNode,oTBODY); oTBODY.insertAdjacentElement("beforeEnd",newTR);
}else{
var newTR=setNewTR(oTR,childNode,oTBODY);
oTBODY.insertBefore(newTR,oTR);
}
}
3.实现setNewTR方法,得到新的一行
function setNewTR(oTR,childNode,oTBODY){
var newTR =oTBODY.firstChild.cloneNode(true);
newTR.style.display="";
newTR.firstChild.firstChild.setAttribute("value",childNode.getAttribute("prikey"));
//设置隐藏域的value为地区的代码
return newTR;
}
4.根据当前节点得到在位置上对应的下一行,下一行位置为当前节点的下一个兄弟节点对应的行或者当前节点的父节点的下一个兄弟节点。
function getNextTR(oTBODY,childNode){
var allTableRows= oTBODY.childNodes;
var thisNode=childNode.parentNode;
var inputvalue=null;
var brothernode=getBrotherNode(thisNode);
if(brothernode){
inputvalue=brothernode.getAttribute("prikey");
for(var i=0;i<allTableRows.length;i++){ if(parseInt(allTableRows[i].firstChild.firstChild.value)==inputvalue){
return allTableRows[i];
}//end if
}
}else{
//alert("dim");
return null;
}
}
5.遍历得到兄弟节点
function getBrotherNode(thisnode){
var brothernode=thisnode.nextSibling;
if(brothernode){
return brothernode;
}
else {
var parentnode=thisnode.parentNode;
if(parentnode){
return getBrotherNode(parentnode); //否则继续找其父节点的下一个同级节点 }else{
return null;
}
}
}
本文介绍了一种通过DOM操作实现表格数据更新的方法。具体步骤包括获取当前节点的子节点、实现addNewRowInOneTable方法以在指定表格中添加新行、setNewTR方法创建新行元素以及getNextTR方法定位插入位置。这些方法共同实现了动态更新表格数据的功能。
957

被折叠的 条评论
为什么被折叠?



