AngularJS ng-重复2D数组,并根据索引仅显示某些值
AngularJS ng-repeat 2D array and display only certain values based on index
我有以下二维数组:
SideNavItems= [["Parent Section 1", "Parent Section 2"],["Parent Section 3", "Parent Section 4"]]
我想使用 ng-repeat 在具有选项卡式导航的页面上以以下方式显示它们:
期望输出:
选择第一个选项卡时:
Parent Section 1
Parent Section 2
选择第二个选项卡时:
Parent Section 3
Parent Section 4
电流输出:
选择第一个选项卡时:
Parent Section 1
Parent Section 2
Parent Section 3
Parent Section 4
我有一个$index变量,它会根据选择的选项卡而变化。我可以设置它,以便在索引为 0 时仅重复第一个数组(2D 数组的元素 0)的内容,当索引为 1 时重复第二个数组(2D 数组的元素 1)的内容,依此类推如果 2D 数组有更多元素?
这是我用于重复数组的 html:
<div id="field">
<div ng-repeat="row in SideNavItems">
<div ng-repeat="cell in row" >
<a ng-click="level=2">{{cell}}</a>
</div>
</div>
</div>
我不知道这是否非常相关,但这是选项卡的 HTML。getClass 方法提供活动类,这些类更改 CSS 中的选项卡外观。方法将当前索引更改为活动选项卡。
<nav>
<ul>
<li ng-class="getClass($index)" ng-repeat="tab in tabs">
<a ng-click="toggleSelect($index)" ng-class="getLinkClass($index)">{{tab}}</a>
</li>
</ul>
</nav>
对于每个父部分,我都有类似的数组,如果有办法做到这一点,我希望做同样的事情。我想我需要另一个变量来充当第二个索引......
编辑 15-2-3 下午2:00
根据您的建议,我能够获得所需的输出。我现在正在尝试为这些父部分的子部分获得类似的功能,这似乎有点复杂。我实际上有一个 3D 数组:
[
[[Parent1Child1, Parent1Child2],[Parent2Child1, Parent2Child2, Parent2Child3]],
[[Parent3Child1, Parent3Child2],[Parent4Child1, Parent4Child2, Parent4Child3]]
]
期望输出:选择第一个选项卡时:
Parent Section 1
Parent1Child1
Parent1Child2
Parent Section 2
Parent2Child1
Parent2Child2
Parent2Child3
选择第二个选项卡时:
Parent Section 3
Parent3Child1
Parent3Child2
Parent Section 4
Parent4Child1
Parent4Child2
Parent4Child3
每个部分最终都将是一个可折叠/可扩展的手风琴,这就是我开始编码的目标,但也许我需要备份并处理它,吐出上面显示的所需输入,然后我可以使用 ng-click 显示/隐藏展开/折叠功能的正确内容。他就是我目前所拥有的,而且似乎很接近。我在代码之后描述输出。
切换选择代码:
//gets tab tiles order from csv
$scope.tabs= tabsService.mainTabs;
//Assigns active classes to main tabbed navigation
$scope.selectedIndex = 0;
$scope.sideNavItems = productCategoryService.sideNavItems;
$scope.sideNavChildren = productCategoryService.sideNavChildren;
$scope.toggleSelect = function(ind){
$scope.selectedIndex = ind;
$scope.currentTabSideNavItems = $scope.sideNavItems[ind];
$scope.selectedChildIndex = 0;
$scope.toggleChildIndex = function(childIndex){
$scope.selectedChildIndex = childIndex;
$scope.currentSideNavChildren = $scope.sideNavChildren[ind][childIndex];
}
}
.HTML
<div id="field">
<div ng-repeat="item in currentTabSideNavItems">
<a ng-click="toggleChildIndex($index)">{{item}}</a>
<div ng-repeat="child in currentSideNavChildren">
<a ng-click="toggleChildIndex($index)">{{child}}</a>
</div>
</div>
</div>
使用此代码,当我单击相应的父部分标题时,我会看到正确的子项,但子项显示在当前页面上的所有父节下。例如:
选择第一个选项卡并单击父部分 1 时:
Parent Section 1
Parent1Child1
Parent1Child2
Parent Section 2
Parent1Child1
Parent1Child2
选择第一个选项卡并单击父部分 2 时:
Parent Section 1
Parent2Child1
Parent2Child2
Parent2Child3
Parent Section 2
Parent2Child1
Parent2Child2
Parent2Child3
我是不是又把事情复杂化了?我该如何努力让合适的孩子和他们的父母在一起?非常感谢您的时间,非常感谢。
在我看来,您确实希望将其排除在UI之外并将其隐藏在控制器中。 您已经有一个名为 toggleSelect 的操作 在您的控制器中,通过事物的外观,您可以在其中将新的范围属性设置为仅当前所选选项卡的侧导航项,例如;
$scope.toggleSelect = function(index) {
// existing code here
$scope.currentTabSideNavItems = SideNavItems[index];
}
您的标记可以简化为:
<div id="field">
<div ng-repeat="item in currentTabSideNavItems">
<a ng-click="level=2">{{item}}</a>
</div>
</div>
这样,提取要显示的项目的逻辑就可以很好地隐藏在控制器中,并且可以进行测试。
正如@DoctorMick所说,最好"将逻辑与视图分开" 我根据您的需求在 jsfiddle 上制作了一个片段,希望这有所帮助。
<section ng-app="App" ng-controller="Ctrl">
<nav>
<ul>
<li ng-repeat="tab in tabs" ng-click="updateSideNavItems($index)" ng-class="{active: $index === currentIndex}"> <a>{{tab}}</a>
</li>
</ul>
</nav>
<div id="field">
<div ng-repeat="cell in SideNavItems">
{{cell}}
</div>
</div>
</section>
var app = angular.module('App', []);
app.controller('Ctrl', function ($scope) {
var SideNavItems = [
["Parent Section 1", "Parent Section 2"],
["Parent Section 3", "Parent Section 4"]
];
$scope.tabs = ['tab1', 'tab2', 'tab3'];
$scope.updateSideNavItems = function updateSideNavItems(index) {
$scope.currentIndex = index;
if (index === 0) $scope.SideNavItems = SideNavItems[0];
else if (index === 1) $scope.SideNavItems = SideNavItems[1];
else $scope.SideNavItems = [].concat.apply([], SideNavItems);
}
// defaults
$scope.currentIndex = 0;
$scope.updateSideNavItems($scope.currentIndex);
});
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- 用于显示索引视图 Rails 中的页面的下拉链接
- 选择的索引未设置,除非显示警报
- 在网页上切换选项卡ui的最佳方式,z索引与显示
- 从json中获取所有数据并将其显示在索引页面上
- 点击JQuery/HTML5显示元素的索引/位置
- AngularJS ng-重复2D数组,并根据索引仅显示某些值
- 谷歌地图 Z 索引地图不显示
- 在模式窗口中显示页面上单击的值(链接)以及索引值
- 可选择序列化 jQuery UI::如何显示 html 标记中的内容而不是索引值
- 尝试使用PHP / Javascript构建一个可点击的索引,该索引显示单击的每个项目的数据
- 在索引上显示下拉列表更改了另一个下拉列表 .net 2.0
- 获取页脚以显示带有 z 索引的 AFTER 元素
- 查找突出显示文本的索引
- Highcharts,显示/隐藏共享工具提示时获取点索引
- Keystone,在索引页面上显示博客类别
- 绝对定位元素上的 z 索引显示在较高的 z 指数元素上方
- Rails Rendering从索引页以Bootstrap模式显示
- Angular js在点击图库中的照片以显示模态时检测索引
- JSON-根据JSON的索引显示数据