2009-08-13 178 views
0

我有一個數據表,我想寫一個循環,我可以呈現一個html表,我想從頭開始(而不是抽象的數據源)。datatable與foreach循環

我想讓每行的項目數量變量。

什麼是正確的循環語法給定一個數據表與X數量的記錄,其中每個記錄是一個單元格。

所以如果我有20條記錄和我的NumberOfItemsPerRow = 5,我會有一個4行的HTML表。

回答

3

這就是循環如何使用可用數據創建表格。最後一行用空單元格填滿整行。

int index = 0; 
while (index < theDataTable.Rows.Count) { 
    // start of table row 
    for (int column = 0; column < numberOfColumns; i++) { 
     if (index < theDataTable.Rows.Count) { 
     // table cell with data from theDataTable.Rows[index] 
     } else { 
     // empty cell 
     } 
     index++; 
    } 
    // end of table row 
} 
0

使用JavaScript庫可也有幫助,

舉例來說,在jQuery的:

$("#theDataTable tr").each(function(){ //loop though rows 
    $(this).find("td").each(function(){ //loops through cells 
    }); 
}); 

更少的代碼!

+0

我使用的是asp.net mvc,所以沒有必要在HTML之外打破這個代碼並進入視圖幫助器甚至控制器 – leora 2009-08-13 10:04:25