本文介绍了AngularJS表上单列多NG-重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从两个父和子对象的列表项显示一个表格。是否有可能做到这一点使用NG-重复?一个for循环会是这个样子。

 的foreach(在列表VAR父)
  的foreach(VAR孩子父母)
     打印(parent.1)
     打印(parent.2)
     打印(child.1)
     打印(child.2)

下面是什么每一行会是什么样子的总体思路。

 <表>
< TR NG重复=父列表中的>
   NG-重复=孩子家长
    < TD> parent.item1< / TD>
    < TD> parent.item2< / TD>
    < TD> parent.item3< / TD>
    < TD> child.item1< / TD>
    < TD> child.item2< / TD>
< / TR>
< /表>


解决方案

是的,很有可能。

假设对象称为数组家长 parent.child 本身就是子阵列的对象,因为它似乎是在你的榜样,你会那么做的用特殊的NG-重复启动以下和NG-重复端形式ngRepeat的。

 <表>
< TR NG重复=父母双亲>
    < TD> parent.item1< / TD>
    < TD> parent.item2< / TD>
    < TD> parent.item3< / TD>
    < TD NG-重复启动=,在parent.child子> child.item1< / TD> &所述;! - 内环的开始 - >
    < TD NG重复端> child.item2< / TD> <! - 内环的结束 - >
< / TR>
< /表>

更新:

由于OP似乎想通过父母单独分组子行,这可能是解决方案寻求:

 <表>
  < TBODY NG重复=父母双亲>
  < TR NG重复=孩子parent.child>
    < TD> parent.item1< / TD>
    < TD> parent.item2< / TD>
    < TD> parent.item3< / TD>
    < TD> child.item1< / TD>
    < TD> child.item2< / TD>
  < / TR>
  < / TBODY>
< /表>

I am trying to display a table with items from both a parent and a list of child objects. Is it possible to do this using ng-repeat? A for loop would look something like this.

foreach(var parent in list)
  foreach (var child in parent)
     print(parent.1)
     print(parent.2)
     print(child.1)
     print(child.2)

Below is the general idea of what each row would look like.

<table>
<tr ng-repeat="parent in list">
   ng-repeat="child in parent"
    <td>parent.item1</td>
    <td>parent.item2</td>
    <td>parent.item3</td>
    <td>child.item1</td>
    <td>child.item2</td>
</tr>
</table>
解决方案

Yes, quite possible.

Assuming an array of parent objects called parents and that parent.child itself is an array of child objects, as it seems to be in your example, you would then do the following using the special ng-repeat-start and ng-repeat-end forms of ngRepeat.

<table>
<tr ng-repeat="parent in parents">  
    <td>parent.item1</td>
    <td>parent.item2</td>
    <td>parent.item3</td>
    <td ng-repeat-start="child in parent.child">child.item1</td> <!-- start of the inner loop -->
    <td ng-repeat-end>child.item2</td> <!-- end of inner loop -->
</tr>
</table>

Update:

Since the OP seems to want separate child rows grouped by parent, this might be the solution sought:

<table>
  <tbody ng-repeat="parent in parents">
  <tr ng-repeat="child in parent.child">
    <td>parent.item1</td>
    <td>parent.item2</td>
    <td>parent.item3</td>
    <td>child.item1</td>
    <td>child.item2</td>
  </tr>
  </tbody>
</table>

这篇关于AngularJS表上单列多NG-重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 22:14