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

标签 javascript angularjs

是否可以在用 angularjs 呈现的表中对数据进行分组(使用 rowspan,如 here 所述)。数据是分层的,state 有许多 county,每个县有多个 zipcodes。我想要一个只有州、县、 zip 等列的表(所以给出集合长度的行跨度)。我不确定 ng-repeat-startng-repeat-end 可用于实现此目的。请参阅入门模板 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-startng-repeat-end 将重复移动到 tr 以防止 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>

关于javascript - 使用 rowspan 对分层数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104750/

相关文章:

javascript - 使用 Node js 和 fs 列出目录、子目录及其文件

javascript - 选择子元素

javascript - 从 Ajax 调用 PHP 函数

javascript - R Shiny 仪表板: specifying div style width argument as percentage to fit a resizeable JS plot

javascript - Angular JS - 使用服务保存\共享数据

javascript - 如何在 Angular Js 中不使用指令的情况下调用作用域值的 onchange 事件?

javascript - 为什么 AngularUI Bootstrap 日期选择器没有弹出?

javascript - Angular Json 比较

javascript - 如何重新渲染快速路线上的页面并显示错误消息?

javascript - Angular UI-Router从子级更新父级范围