【发布时间】:2015-02-03 15:44:55
【问题描述】:
我有以下二维数组:
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时仅重复第一个数组(二维数组的元素0)的内容,当索引为1时重复第二个数组(二维数组的元素1)的内容,如果二维数组有更多元素,以此类推?
这是我用于重复数组的 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 中选项卡外观的活动类。 toggleSelect 方法将当前索引更改为活动选项卡。
<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>
对于每个父节,我都有与子节相似的数组,如果有办法让这个工作,我希望做同样的事情。我想我需要另一个变量来充当第二个索引...
***编辑 2/3/15 下午 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
我是不是又把事情复杂化了?我应该如何努力让合适的孩子和他们的父母在一起?非常感谢您的宝贵时间,非常感谢。
【问题讨论】:
-
在我看来,你要做的事情太复杂了。而不是所有的数组,你不能只使用 json 对象吗?
-
@PatrickFerreira 我目前正在使用jquery-csv 来读取以数组形式存储在应用数据文件夹中的 csv 文件。因此,我为应用程序的每个级别都有一个数组,其中包含每个级别的部分。起初,使用像 tab1SideNavItems(带有 tab1 的所有父部分)和 tab1SideNavItem1Children(带有 tab1 section1 的子部分)这样的数组来“硬编码”这些标签和部分并不困难。我开始将这些数组放入数组中,以便循环创建 csv 中列出的任何部分,这些部分可以更改。
-
@PatrickFerreira 在最后一条评论中达到了字符上限,但这里有更多信息。应用程序的后端会更新用户计算机上的 app.data 文件夹,使其具有特定的文件夹结构,其中包含应用程序的所有内容。前端使用这些文件夹中的 csv 文件来导航此文件夹结构并知道显示内容的正确顺序。
标签: javascript angularjs multidimensional-array angularjs-ng-repeat