2012-07-31 72 views
0

我一直在玩Dart,我喜欢它,但现在我感觉有点笨。将新行添加到HTML中的表中

我想新行中具有此结构的HTML页面添加到表:

<table> 
    <thead> 
    ... 
    </thead> 
    <tbody> 
    ... 
    </tbody> 
</table> 

的问题是,当我执行的代码,新行的TBODY标记,然后后出现我失去了CSS风格。

我做这样的:

TableElement myTable = query('#tabTest'); 
TableRowElement newLine = new TableRowElement(); 
newLine.insertCell(0).text = "9"; 
newLine.insertCell(1).text = "aaa"; 
newLine.insertCell(2).text = "bbb"; 
newLine.insertCell(3).text = "ccc"; 
myTable.nodes.add(newLine); 

这是错的?我该怎么做,所以新的线路进入tbody标签? 有人可以指点我一个例子吗?

+0

你尝试像'TableElement myTableBody =( '#tabTestBody'); ... myTableBody.nodes.add(newLine);'? – 2012-07-31 19:29:07

+0

我做到了,但它在tbody之后。 – user1566747 2012-07-31 19:42:41

+0

我有答案,但看起来像我将不得不等待8小时发布 – user1566747 2012-07-31 19:43:44

回答

0

我得到的结果,我希望通过这样做:

TableElement myTable = query('#tabTest'); 
// add at the end 
TableRowElement newLine = myTable.insertRow(myTable.rows.length); 
newLine.insertCell(0).text = "9"; 
newLine.insertCell(1).text = "aaa"; 
newLine.insertCell(2).text = "bbb"; 
newLine.insertCell(3).text = "ccc"; 
0

这种变化仅仅适用于您的主机的HTML文件:

<table id="tabTest"> 
    <thead> 
    </thead> 
    <tbody id="tbody"> 
    </tbody> 
</table> 

而这种变化到DART代码:

// TableElement myTable = query('#tabTest'); 
    var myTable = query('#tbody'); 
    TableRowElement newLine = new TableRowElement(); 
    newLine.insertCell(0).text = "9"; 
    newLine.insertCell(1).text = "aaa"; 
    newLine.insertCell(2).text = "bbb"; 
    newLine.insertCell(3).text = "ccc"; 
    myTable.nodes.add(newLine); 
0

飞镖提供了许多方法来构建元素。你可以做这样的事情:

void TableElement makeTable(List<String> ofStuff){ 
    final s = new StringBuffer(); 

    s.add('<table>'); 
    s.add('<thead></thead>'); 
    for(final stuff in ofStuff){ 
     s.add('<tr><td>${stuff}</td></tr>'); 
    } 
    s.add('</table>'); 

    return new Element.html(s.toString()) as TableElement; 
}