使用rowspan对分层数据进行分组

bsr*_*bsr 15 javascript angularjs

是否可以在使用angularjs呈现的表中对数据进行分组(使用此处所述的 rowspan ).数据是分层的,state有许多counties,每个县有多个zipcodes.我想要一个只包含州,县,邮政等列的表(所以给集合的长度为rowpan).我不确定ng-repeat-start,ng-repeat-end可以用来实现这一目标.请在此处查看入门模板

 <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>
Run Code Online (Sandbox Code Playgroud)

数据

 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},
      ];
Run Code Online (Sandbox Code Playgroud)

编辑:

这里有一个手工制作的所需输出版本http://plnkr.co/edit/T7toND0odx6qr8mVC121?p=preview

Rob*_*ker 22

这是Holly Cummins答案的变体.将重复移入trwith ng-repeat-startng-repeat-end防止tbody重复.还使用稍微不同的方法来隐藏第一行.

<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>
Run Code Online (Sandbox Code Playgroud)


Hol*_*ins 9

KayakDave的这个变体的答案通过拉出第一个嵌套行来共享<tr>标题来改进结构:

<tbody ng-repeat='st in states'>
    <tr>
      <th rowspan="{{st.counties.length}}">{{st.name}}</th>
      <td>{{county[0].name}}</td>
    </tr>
    <tr ng-repeat='county in st. counties' ng-show="$index > 0">
       <td>{{county.name}}</td>
    </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)


Kay*_*ave 4

当然。您的想法是这样的吗:

<tbody ng-repeat='st in states'>
    <td rowspan="{{st.counties.length+1}}">{{st.name}}</td>
    <tr ng-repeat='county in st.counties'>
       <td>{{county.name}}</td>
    </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)

我让这个例子保持简单,嵌套了 2 层。但在下面的小提琴中,我将其嵌套了 3 层(因此它包括 zip)。

演示小提琴