我需要使用DataTable实现某些单元格跨列,绘制如图(1-1)所示的表格,并且数据是动态加载的,在这里数据格式如data。
注意:下面片段代码的else必须添加,否则DataTable会抛出warning异常:requested unkonw parameters "2" from the data source for row 2.
@if(mark==true){
mark =false;
<tdrowspan="@item.list.Count()"id="@item.NodeItemId"class="nodeId">@item.DealStepsName</td>
}else{
<tdstyle="display:none"></td>
}
}
列1 | 列2 | 列3 |
---|---|---|
a | a2 | a3 |
bd | b2 | b3 |
d2 | d3 |
图(1-1)
代码片段:
data={
[ {value1:a, list:[ { value2:a2,value3:a3 } ] },
{value1:bd, list:[ { value2:b2,value3:b3 },{ value2:d2,value3:d3 } ] } ]
}
$(document).ready(function () {
$("#tblDealList").dataTable();
}
<table id="tblDealList" class="display">
<thead>
<tr>
<th>列1</th>
<th>列2</th>
<th>列3</th>
</tr>
</thead>
<tbody>
@{
var mark=true;
if (data!= null)
{
foreach (var item in data)
{
mark = true;
foreach(var u in item.list){
<tr>
@if(mark==true){
mark = false;
<td rowspan="@item.list.Count()">@item.value1</td>
}else{
<td style="display:none"></td>
}
<td>@u.value2</td>
<td>@u.value3</td>
</tr>
}
}
}
}
</tbody>
</table>