使用 rowspan 对分层数据进行分组 [英] Use of rowspan to group hierarchical data
问题描述
是否可以对数据进行分组(使用 rowspan 如此处) 在用 angularjs 呈现的表格中.数据是分层的,state
有很多counties
,每个counties有多个zipcodes
.我想要一个只有州、县、邮编等列的表格(因此请为集合的长度提供一个行跨度).我不确定 ng-repeat-start
和 ng-repeat-end
是否可以用来实现这一点.请在此处
Is it possible to group data (using rowspan as explained here) in a table rendered with angularjs. Data is hierarchical, with state
having many counties
and each counties has multiple zipcodes
. I want a table with only column for state, county, zip etc (so give a rowspan for length of the collection). I am not sure ng-repeat-start
and ng-repeat-end
can be used to achieve this. Please see the starter template here
<table>
<thead>
<tr>
<th>State</th>
<th>County</th>
<th>Zip</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='st in states'>
<td>{{st.name}}</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
数据
var oh_counties = [
{name: "Franklin", zips: [111,222,333,444,555]},
{name: "Adams", zips: [111,222,333,444]},
{name: "Allen", zips: [111,222,333]}
],
wi_counties = [
{name: "Dane", zips: [111]},
{name: "Adams", zips: [111,222,333,444]}
]
$scope.states = [
{name: "OH", counties: oh_counties},
{name: "WI", counties: wi_counties},
];
所需输出的手工版本在这里http://plnkr.co/edit/T7toND0odx6qr8MVC121?p=preview
推荐答案
这是 Holly Cummins 答案的变体.使用 ng-repeat-start
和 ng-repeat-end
将重复移入 tr
以防止 tbody
> 避免重复.也使用了稍微不同的方法来隐藏第一行.
This is a variant of Holly Cummins' answer. The repeat is moved into the tr
with ng-repeat-start
and ng-repeat-end
to prevent the tbody
from being repeated. Also uses a slightly different method to hide the first row.
<tbody>
<tr ng-repeat-start='st in states'>
<th rowspan="{{st.counties.length}}">{{st.name}}</th>
<td>{{st.counties[0].name}}</td>
</tr>
<tr ng-repeat-end ng-repeat='county in st.counties' ng-hide="$first">
<td>{{county.name}}</td>
</tr>
</tbody>
这篇关于使用 rowspan 对分层数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!