如何让一个元素在n -repeat中只重复一次
How do I make an element inside ng-repeat to repeat just once?
嗨,我陷入了一种情况,我必须创建一个表,这样对于每个元集,我必须在表中显示集的行。理想情况下,像元集应该有一个行跨度,其中的行必须跨度。像这样Metaset1 SetAttr1 Setattr2设置Attr2 SetAttr2Metaset2 SetAttr1 Setattr2设置Attr2 SetAttr2我使用了以下结构:
<tbody ng-repeat="(metaset, ids) in metasetHashMapTree">
<tr>
<td rowspan = "{{(ids.length*2) + 1}}"><span style="font-weight:bold">{{metaset}}</span></td>
</tr>
<tr ng-repeat="item in ids" >
<td width="10%" align="{{item.treeLevelStyle['align']}}" bgcolor="{{item.treeLevelStyle['color']}}"> <span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyId}}</span></td>
<td width="2%" class="{{item['A-S0-a']}}"></td>
<td width="2%" class="{{item['A-S0-b']}}"></td>
<td width="2%" class="{{item['A-S0-c']}}"></td>
<td width="2%" class="{{item['A-S0-d']}}"></td>
<td width="2%" class="{{item['A-S0-e']}}"></td>
<td width="2%" class="{{item['A-S0-f']}}"></td>
<td width="2%" class="{{item['A-S0-g']}}"></td>
<td width="2%" class="{{item['A-S1-a']}}"></td>
<td width="2%" class="{{item['A-S1-b']}}"></td>
<td width="2%" class="{{item['A-S2-a']}}"></td>
<td width="2%" class="{{item['A-S3-a']}}"></td>
<td width="2%" class="{{item['A-S4-a']}}"></td>
<td width="2%" class="{{item['A-S5-a']}}"></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyTenant}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyRelease}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyComponentAffected}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyStatus}}</span></td>
<td font-weight : "{{item.treeLevelStyle['font-weight']}}" bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.Match}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.SellDropFix}}</span></td>
</tr>
</tbody>
我现在有一个要求,在一个元集中只有第一行必须首先显示,其余的应该只在单击该元集中的一个按钮后显示。
我如何修改这样的结构来适应这个需求?我已经尝试过将所有内容放入一行并在第二行中显示元集,如下面所示,但是行线出现为行跨度现在丢失了。
<tbody ng-repeat="(metaset, ids) in metasetHashMapTree">
<!-- <tr>
<td rowspan = "{{(ids.length*2) + 1}}"><span style="font-weight:bold">{{metaset}}</span></td>
</tr> -->
<tr ng-repeat="item in ids" >
<td> <span style="font-weight:bold">{{metaset}}</span></td>
<td width="10%" align="{{item.treeLevelStyle['align']}}" bgcolor="{{item.treeLevelStyle['color']}}"> <span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyId}}</span></td>
<td width="2%" class="{{item['A-S0-a']}}"></td>
<td width="2%" class="{{item['A-S0-b']}}"></td>
<td width="2%" class="{{item['A-S0-c']}}"></td>
<td width="2%" class="{{item['A-S0-d']}}"></td>
<td width="2%" class="{{item['A-S0-e']}}"></td>
<td width="2%" class="{{item['A-S0-f']}}"></td>
<td width="2%" class="{{item['A-S0-g']}}"></td>
<td width="2%" class="{{item['A-S1-a']}}"></td>
<td width="2%" class="{{item['A-S1-b']}}"></td>
<td width="2%" class="{{item['A-S2-a']}}"></td>
<td width="2%" class="{{item['A-S3-a']}}"></td>
<td width="2%" class="{{item['A-S4-a']}}"></td>
<td width="2%" class="{{item['A-S5-a']}}"></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyTenant}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyRelease}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyComponentAffected}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.BuyStatus}}</span></td>
<td font-weight : "{{item.treeLevelStyle['font-weight']}}" bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.Match}}</span></td>
<td bgcolor="{{item.treeLevelStyle['color']}}"><span style="font-weight:{{item.treeLevelStyle['font-weight']}}">{{item.SellDropFix}}</span></td>
</tr>
</tbody>`
请帮。
按照文档中的建议,您应该使用LimitTo过滤器。
<tr ng-repeat="item in ids | limitTo:2" >
限制值可以在控制器中指定,也可以直接插入到过滤器中。
如果你想隐藏/显示元素,你可以使用ngHide或ngShow指令。
查看ngShow
查看
用法相对简单,文档中的示例:
<!-- when $scope.myValue is truthy (element is hidden) -->
<div ng-hide="myValue" class="ng-hide"></div>
<!-- when $scope.myValue is falsy (element is visible) -->
<div ng-hide="myValue"></div>
同样适用于ngShow,除了当myValue为true时,它将显示
可以根据详细信息行是否可见来计算rowspan。
将使用变量isVisible
,该变量可以在每个<tbody>
重复中切换,因为ng-repeat
为每个重复元素创建了子作用域。
因此,当行不可见时,表达式将rowspan
设置为1
,并在
<tr>
<!-- rowspan will be "1" if not visible, or calculated if they are -->
<td rowspan = "{{isVisible ? (ids.length*2) + 1 : 1}}">
<span >{{metaset}}</span>
<button ng-click="isvisible = !isVisible">Toggle Details</button>
</td>
</tr>
然后使用ng-if
来切换dom
<tr ng-repeat="item in ids" ng-if="isVisible">
<td></td>
<td></td>
........
</tr>
ng-if
支持ngAnimate
的css动画,所以用一些css规则可以使它像一个平滑的手风琴
相关文章:
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- 引导程序崩溃一次只能看到一个
- 有没有一个抽象层,这样我就可以集成一次,然后使用pusher、pubnub或faye
- 加载器组件仅加载一次
- Ng-Repeat只工作一次
- AngularJS指令在ng-repeat中为每个项目设置不同的值,只重复一次
- ng-change仅在ng-repeat内触发一次 - Angular
- 只显示“;没有结果”;在AngularJS中使用嵌套ng repeat时一次
- 如何让一个元素在n -repeat中只重复一次
- Angular只调用空的ng-repeat一次
- 使用ng-repeat一次显示2个元素
- 将ng repeat中的值设置为另一个值一次(仅在页面加载时)
- 如何使用ng repeat在一次迭代中访问(项中的项)的2个项
- Ng-repeat和指令一次展开一个列表项