2016-05-13 16 views
0

我想使用树行程来查找页面上的表,插入列,然后根据每行第1个单元格中的值填充该列中的单元格。我做了很多研究,并且一直在失败。任何命中将我推向正确的方向将不胜感激。使用树行程修改表

document.ondblclick = addElement; 
 
var i = 1; 
 
function addElement() { 
 

 
    var allTextNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT), 
 
    // some temp references for performance 
 
    tmptxt, 
 
    tmpnode, 
 
    // compile the RE and cache the replace string, for performance 
 
    cakeRE = "Investment" 
 
    replaceValue = "TestReplace"; 
 
    var allElementNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT); 
 

 
    // iterate through all text nodes 
 
while (allTextNodes.nextNode()) { 
 
    tmpnode = allTextNodes.currentNode; 
 
    tmptxt = tmpnode.nodeValue; 
 
    tmpnode.nodeValue = tmptxt.replace(cakeRE, replaceValue); 
 
} 
 

 
    // iterate through all Element nodes 
 
while (allElementNodes.nextNode()) { 
 
    tmpnode = allElementNodes.currentNode; 
 

 
    //I know this is not even close to real code but I think it explains what i'm trying to do 
 

 
    IF (tmpnode.tagvalue = "table") 
 
    { 
 
    tmpnode.insertcolumn 
 
    } 
 
    If (tempnode.tagvalue = "tr" && Row.contains("Active")) 
 
    { 
 
    pupulate the cell from the previously created column with "Active Pending"; 
 
    } 
 

 
    
 
} 
 
}

function PopulateTable() { 
 
    var table = document.getElementsByTagName("table")[0]; 
 
    var rows = table.getElementsByTagName("tr")[0]; 
 
    for(var i = 0; i< rows.length; i++) { 
 
     var firstColumn = rows[0].getElementsByTagName("td")[0]; 
 
     //do your data aggregation here 
 
     var newCell = document.createElement("td"); 
 
     newCell.innerHTML = "some data";//create row here 
 
     rows[i].appendChild(newCell); 
 
    } 
 

 
}

回答

1

我认为你可能在错误的方式去这件事。我建议不要在这种特殊情况下使用树木行者。相反,你可以做一些简单的事情,如下所示:

function PopulateTable() { 
    var table = document.getElementsByTagName("table")[0]; 
    var rows = table.getElementsByTagName("tr")[0]; 
    for(var i = 0; i< rows.length; i++) { 
     var firstColumn = rows[0].getElementsByTagName("td")[0]; 
     //do your data aggregation here 
    } 
    var newRow = document.createElement("tr"); 
    newRow.innerHTML = //create row here 
    table.appendChild(newRow); 
} 
+0

谢谢托马斯,我会试试这个,我现在摆弄它。 – keatklein

+0

嗨托马斯,newRow方法确实有效。我希望为每一行添加一个新的单元格,像这样(上面添加了新的代码片断)。 – keatklein