【发布时间】:2017-02-21 15:14:18
【问题描述】:
我有一个包含对象列表的视图。
例如,如果我有一个人员列表.. 按他们所在的位置和他们所在的部门排序:
| ID | Name | Location | Division | Age |
--------------------------------------------------------------
1 John Building1 Finance 25
2 Alex Building1 Finance 30
3 Chris Building2 ISS 22
4 Justin Building1 Human Resources 41
5 Mary Building2 Accounting 43
6 Ian Building1 Human Resources 27
7 John Building1 Finance 35
所以我的操作返回语句如下所示:
lstOfPersonnel = lstOfPersonnel.OrderBy(x => x.Location).ThenBy(x => x.Division).ThenBy(x => x.Name).ToList();
return View(lstOfPersonnel);
在我看来,我有这个:
<table class="table table-bordered no-border">
@foreach (var item in Model)
{
if ((Model.IndexOf(item) == 0) || ((Model.IndexOf(item) != 0) && (!item.Building.Equals(Model.ElementAt(Model.IndexOf(item) - 1).Building) || !item.Division.Equals(Model.ElementAt(Model.IndexOf(item) - 1).Division))))
{
<thead>
<tr>
<th><b>@item.Building</b></th>
<th><b>@item.Division</b></th>
</tr>
<tr class="no-display"></tr>
<tr>
<th>Name</th>
</tr>
<tr>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
</tr>
</tbody>
}
else
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
</tr>
</tbody>
}
}
</table>
现在,当我打印预览时,它会将同一建筑物和分区中的每个人放在各自的标题下。但是,第一个 <thead> 元素.. 可以说这个例子是 Building1 和 Finance,因为 .OrderBy.. 显示在 每个 页面的顶部下一座建筑。
所以对于视觉效果,这就是我打印预览时的样子:
第 1 页:
// Perfect Render
Building1 | Finance
Name | Age
Alex 30
John 35
John 25
第 2 页:
// Repeat of Page 1's headers
Building1 | Finance
Name | Age
Building1 | Human Resources
Name | Age
Ian 27
Justin 41
第 3 页:
// Repeat of Page 1's headers
Building1 | Finance
Name | Age
Building2 | Accounting
Name | Age
Mary 43
第 4 页:
// Repeat of Page 1's headers
Building1 | Finance
Name | Age
Building2 | ISS
Name | Age
Chris 43
【问题讨论】:
-
您使用的是哪个浏览器?据我记得,Chrome 在每一页上打印标题都有问题。我不知道他们是否修复了它。
-
@jae.phoenix 我使用的是 IE 11。
-
我不知道你是否已经有了,但是看看这个:link。据我了解,最重要的部分是在
thead的每一页上添加您要打印的内容以及您正在使用的 css,以及tbody中的内容以及 cssdisplay:table-row-group; -
@jae.phoenix 我已编辑我的问题以显示我的问题的更具体示例。
-
如果你在你的桌子外面做你的for循环怎么办?这样它就会在每个循环中创建一个新表,而不仅仅是一个头部和一个身体。我认为正在发生的事情是表格中有多个头,这就是为什么它将每个头打印在每一页的顶部。