2012-07-31 30 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; 
}