How can I merge cells in a table using rowspan inside a Razor view?
- Without using javascript/jQuery.
- The table's source is an IEnumerable.
- Some of the items in the source will have the same value in a certain field and those are the one we want to merge in one cell.
- We don't know how many times a given value is repeated.
The resulting HTML should be something like this:
<table>
<tr>
<td rowspan="2">Column 1 row 1</td>
<td>Column 2 row 1</td>
</tr>
<tr>
<td>Column 2 row 2</td>
</tr>
<tr>
<td rowspan="3">Column 1 row 3</td>
<td>Column 2 row 3</td>
</tr>
<tr>
<td>Column 2 row 4</td>
</tr>
<tr>
<td>Column 2 row 5</td>
</tr>
</table>
EDIT to add the data source and desired result:
The data source may be something like Products with Categories. Ex:
Categories
- Electronics
- Groceries
Products
- Laptop (Electronics)
- iPod (Electronics)
- TV (Electronics)
- Coffee (Groceries)
- Cookies (Groceries)
The result
╔═════════════╦════════╗
║ Electronics ║ Laptop ║
║ ║ iPod ║
║ ║ TV ║
╠═════════════╬════════╣
║ Groceries ║ Coffee ║
║ ║ Cookies║
╚═════════════╩════════╝
The only way I have in mind is using Linq to count how many times a value is on the list, but I wanted to know if someone have a better solution. Thanks!