[英]how can I prevent Nested tables inside ng-repeat from populating in every row?
OK, so I want to create a dynamic nested hierarchy of tables. 好的,所以我想创建一个动态嵌套的表层次结构。 I get they data no problem, but using ng-repeat at each level causes the parent table to insert child data for a specific row into each row of the parent table. 我得到的数据没有问题,但是在每个级别上使用ng-repeat会使父表将特定行的子数据插入父表的每一行。 I want to prevent this, I have tried using ng-repeat-start and ng-repeat-end, however, because of the table nesting I cannot add the end tag in an appropriate place to stop the repeat. 我想防止这种情况,但我尝试使用ng-repeat-start和ng-repeat-end,但是由于表嵌套的原因,我无法在适当的位置添加end标签来停止重复。
UPDATE Let me clarify a bit here, I have 3 nested tables, the top level table has list of groups, the first child table is a list of all of the items that belong to a specific group in the parent table. UPDATE让我在这里澄清一下,我有3个嵌套表,顶层表具有组列表,第一个子表是属于父表中特定组的所有项目的列表。 When the user clicks the expand button, I populate the child table based on which row in the parent table was clicked, this is OK, however the child table now shows up in each of the parent table rows instead of just the row that was clicked. 当用户单击展开按钮时,我会根据单击父表中的哪一行来填充子表,这是可以的,但是现在,子表将显示在每个父表行中,而不仅仅是单击的行。
Plunker Link http://plnkr.co/edit/RVOnf9wBF3TzXauzvMfF Plunker链接 http://plnkr.co/edit/RVOnf9wBF3TzXauzvMfF
HTML: HTML:
<table id="sicGroupTable" class="RadGrid_Metro">
<thead>
<tr>
<td>Sic Code Group</td>
</tr>
</thead>
<tbody ng-repeat="group in sicCodeGroup">
<tr class="rgRow">
<td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(group.GroupId)" ng-model="hideTwoDigitSub" /></td>
<td><input type="checkbox" ng-model="SelectGroup" /></td>
<td>{{group.GroupName}}</td>
</tr>
<tr ng-hide="hideTwoDigitSub">
<td></td>
<td>
<table id="sic2DigitTable" class="RadGrid_Metro">
<thead>
<tr>
<th>2 digit sic Code</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="twoDigitSic in twoDigitSicCodes" class="rgRow">
<td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(twoDigitSic.SicCode)" /></td>
<td><input type="checkbox" ng-model="Select2DigitSicCode" /></td>
<td>{{twoDigitSic.SICCode2}} - {{twoDigitSic.Title}}</td>
</tr>
<tr>
<td></td>
<td>
<table id="sic4DigitTable" class="RadGrid_Metro">
<thead>
<tr>
<th>4 digit sic code</th>
</tr>
</thead>
<tbody>
<tr class="rgRow">
<td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(sicCode.SicCode)" /></td>
<td><input type="checkbox" ng-model="Select2DigitSicCode" /></td>
<td>{{sicCode.SicCode}} - {{sicCode.Title}}</td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
JavaScript: JavaScript的:
var app = angular.module("apptSetting", ['ngResource'])
app.factory('dataFactory',
function ($resource) {
return {
getSicGroups: $resource('../Packages/GetJsonSicCodeGroups'),
expandRow: $resource('../Packages/GetExpandedRowData')
}
});
app.controller('aPackageController', ['$scope', 'dataFactory', function ($scope, dataFactory) {
function init() {
$scope.hideTwoDigitSub = true;
$scope.hideFourdigitsub = true;
}
$scope.sicCodeGroup = dataFactory.getSicGroups.query({}, isArray = true);
$scope.twoDigitSicCodes = null;
$scope.expandRow = function (groupId, sicCode) {
if (groupId != undefined)
{
$scope.twoDigitSicCodes = dataFactory.expandRow.query({ GroupId: groupId }, isArray = true);
$scope.hideTwoDigitSub = false;
if (sicCode != null && sicCode != undefined && sicCode != "") {
if (sicCode.length == 2) {
$scope.hideTwoDigitSub = false;
$scope.twoDigitSicCodes = dataFactory.Get2DigitSicCodes.query({ GroupId: groupId }, isArray = true);
}
}
}
}
init();
}])
The issue is that you're using a single boolean hideTwoDigitSub
to control all the tr
s created by your ngRepeat
: 问题是,你使用一个布尔hideTwoDigitSub
来控制所有的tr
S按您所创建的ngRepeat
:
<tr ng-hide="hideTwoDigitSub">
So when you set $scope.hideTwoDigitSub = false;
所以当你设置$scope.hideTwoDigitSub = false;
every ngHide
within your ngRepeat
gets that false and thus all the tr
elements are shown. ngHide
中的每个ngHide
ngRepeat
得到false,因此将显示所有tr
元素。
Radio Button Fix 单选按钮修复
Instead of using a boolean I'd set hideTwoDigitSub
to the groupId
for the row you want to show (and maybe rename hideTwoDigitSub
to showTwoDigitSub
since the variable now indicates which row to show). 我不使用布尔值,而是将hideTwoDigitSub
设置为要显示的行的groupId
(也许将hideTwoDigitSub
重命名为showTwoDigitSub
因为变量现在指示要显示的行)。
So inside your expandRow()
function I'd set which row to show by changing: 因此,在您的expandRow()
函数中,我将通过更改来设置要显示的行:
$scope.hideTwoDigitSub = false;
to 至
$scope.hideTwoDigitSub = groupId;
And change the above tr
to: 并将上面的tr
更改为:
<tr ng-hide="hideTwoDigitSub != group.GroupId">
So the row will be hidden unless your control variable hideTwoDigitSub
is not equal to the current groups GroupId
. 因此,除非您的控制变量hideTwoDigitSub
不等于当前组GroupId
否则该行将被隐藏。
Or it might be clearer to use ngShow
(note I changed the hideTwoDigitSub
to showTwoDigitSub
in this example since it's clearer): 或者,它可能是更清晰的使用ngShow
(请注意,我改变了hideTwoDigitSub
到showTwoDigitSub
在这个例子中,因为它更清晰):
<tr ng-show="showTwoDigitSub == group.GroupId">
Checkbox solution 复选框解决方案
This approach is easiest done switching hideTwoDigitSub
to showTwoDigitSub
- so everything below assumes that. 这种方法最容易完成,将hideTwoDigitSub
切换为showTwoDigitSub
因此下面的所有操作均hideTwoDigitSub
为前提。
For this approach, inside init()
I'd set your control variable to be an array: 对于这种方法,在init()
内部,将您的控制变量设置为数组:
$scope.showTwoDigitSub=[];
And then toggle the appropriate control inside expand
: 然后在expand
内部切换适当的控件:
$scope.showTwoDigitSub[groupId] = !$scope.showTwoDigitSub[groupId];
And use the array inside your html: 并在html中使用数组:
<tr ng-show="showTwoDigitSub[group.GroupId]">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.