简体   繁体   English

为嵌套对象创建新行

[英]Create new rows for nested objects

I have found that ng-repeat-start and ng-repeat-end are available for this but they don't seem to be working. 我发现ng-repeat-start和ng-repeat-end可用于此但它们似乎不起作用。 I have this structure: 我有这个结构:

Events Array: Object, Object, Object. 事件数组:对象,对象,对象。

Each Event Object has properties: Start Time, End Time, User and Comments. 每个事件对象都具有以下属性:开始时间,结束时间,用户和注释。

(Start Time, End Time and User has a string value while Comments is another array which contains Objects.) (开始时间,结束时间和用户具有字符串值,而注释是包含对象的另一个数组。)

Comments Array: Object, Object, Object. 注释数组:对象,对象,对象。

Within the Comments Array each object has these properties: id and comment. 在Comments Array中,每个对象都有以下属性:id和comment。

What I need to do is display every Event Object's Start Time, End Time, and User in a row. 我需要做的是连续显示每个事件对象的开始时间,结束时间和用户。 When I come across a Comment Array in the Event Object that has multiple Comment Objects, I want to display every id and comment in a NEW row. 当我在事件对象中遇到具有多个注释对象的注释数组时,我想在新行中显示每个id和注释。

<tbody>
      <tr ng-repeat-start="value in Events">
           <td>{{value.startTime}}</td>
           <td>{{value.endTime}}</td>
           <td>{{value.User}}</td>
       </tr>
       <tr ng-repeat-end="comments in value.Comments">
            <td>{{comments.id}}</td>
            <td>{{comments.comment}}</td>
       </tr>
 </tbody>

Any help would be great! 任何帮助都会很棒! I'm fairly new to JavaScript so any examples would be nice. 我对JavaScript很新,所以任何例子都会很好。

You need to structure things a bit differently for this to work, but in so doing you'll find you don't need ng-repeat-start and ng-repeat-end . 为了实现这一点,你需要对事物进行一些不同的构造,但这样做你会发现你不需要ng-repeat-startng-repeat-end Instead put the ng-repeat on the <tbody> tag and then your nested ng-repeat on the second <tr> tag. 而是将ng-repeat放在<tbody>标签上,然后将嵌套的ng-repeat放在第二个<tr>标签上。 Like so: 像这样:

<tbody ng-repeat="value in Events">
      <tr>
           <td>{{value.startTime}}</td>
           <td>{{value.endTime}}</td>
           <td>{{value.User}}</td>
       </tr>
       <tr ng-repeat="comments in value.Comments">
            <td>{{comments.id}}</td>
            <td colspan="2">{{comments.comment}}</td>
       </tr>
 </tbody>

You can also try this. 你也可以试试这个。

 angular .module('app', []) .controller('MainCtrl', function($scope) { // generating sample data $scope.Events = [...new Array(10)] .map(() => { const date = Date.now(); return { startTime: `Start time: ${date}`, endTime: `End time: ${date + 500}`, User: `User: ${date}`, Comments: [...new Array(2)] .map(() => { return { id: `Comment id: ${date}`, comment: `Comment: ${date} bla bla` }; }) }; }); }); 
 table { border: 1px solid; border-collapse: collapse; } td { border: 1px solid; } tr:nth-child(3n+1) td { background: blue; color: white; } 
 <script src="//code.angularjs.org/1.6.2/angular.js"></script> <div ng-app="app" ng-controller="MainCtrl"> <table> <tbody> <tr ng-repeat-start="value in Events"> <td>{{value.startTime}}</td> <td>{{value.endTime}}</td> <td>{{value.User}}</td> </tr> <tr ng-repeat-end ng-repeat="comment in value.Comments"> <td>{{comment.id}}</td> <td colspan="2">{{comment.comment}}</td> </tr> </tbody> </table> </div> 

AngularJS documentation helped... AngularJS文档帮助...

"The ng-repeat-start directive works the same as ng-repeat, but will repeat all the HTML code (including the tag it's defined on) up to and including the ending HTML tag where ng-repeat-end is placed." “ng-repeat-start指令的工作方式与ng-repeat相同,但会重复所有HTML代码(包括它定义的标记),包括放置ng-repeat-end的结束HTML标记。”

<tbody>
      <tr ng-repeat-start="value in Events">
           <td>{{value.startTime}}</td>
           <td>{{value.endTime}}</td>
           <td>{{value.User}}</td>
      </tr>
      <tr ng-repeat="comments in value.Comments">
            <td>{{comments.id}}</td>
            <td>{{comments.comment}}</td>
      </tr>
     <tr ng-repeat-end>
     </tr>
 </tbody>

Working Fiddle : http://jsfiddle.net/fqfh4cv6/ 工作小提琴http//jsfiddle.net/fqfh4cv6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM