AngularJS ng-repeat 二维数组并根据索引仅显示某些值 [英] AngularJS ng-repeat 2D array and display only certain values based on index

查看:35
本文介绍了AngularJS ng-repeat 二维数组并根据索引仅显示某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下二维数组:

 SideNavItems= [["Parent Section 1", "Parent Section 2"],["Parent Section 3", "Parent Section 4"]]

我想使用 ng-repeat 在具有标签导航的页面上按以下方式显示它们:

所需的输出:

选择第一个标签时:

父部分 1家长第 2 节

选择第二个标签时:

父部分 3家长第 4 节

当前输出:

选择第一个标签时:

父部分 1家长第 2 节家长第 3 节家长第 4 节

我有一个 $index 变量,它会根据选择的选项卡而变化.我可以设置它,以便在索引为 0 时仅重复第一个数组(二维数组的元素 0)的内容,当索引为 1 时重复第二个数组(二维数组的元素 1)的内容, 等等,如果二维数组有更多元素?

这是我用于重复数组的 html:

 

<div ng-repeat="SideNavItems 中的行"><div ng-repeat="一行中的单元格" ><a ng-click="level=2">{{cell}}</a>

我不知道这是否非常相关,但这里是选项卡的 HTML.getClass 方法提供可更改 CSS 中选项卡外观的活动类.toggleSelect 方法将当前索引更改为活动选项卡.

对于这些父部分中的每一个,我都有类似的带有子部分的数组,如果有办法使这项工作有效,我希望能做同样的事情.我想我需要另一个变量作为第二个索引...

***编辑 2/3/15 下午 2:00

根据您的建议,我能够获得所需的输出.我现在正在尝试为这些父部分的子项获得类似的功能,但似乎有点复杂.我实际上有一个 3D 数组:

<预><代码> [[[Parent1Child1, Parent1Child2],[Parent2Child1, Parent2Child2, Parent2Child3]],[[Parent3Child1, Parent3Child2],[Parent4Child1, Parent4Child2, Parent4Child3]]]

所需的输出:选择第一个选项卡时:

父部分 1父 1 子 1父 1 子 2家长第 2 节父 2 子 1父 2 子 2父 2 子 3

选择第二个标签时:

父部分 3父 3 子 1父 3 子 2家长第 4 节父 4 子 1父 4 子 2父 4 子 3

每个部分最终都会是一个可折叠/可扩展的手风琴,这就是我开始编码的方向,但也许我需要备份并处理它,吐出上面显示的所需输入,然后我可以玩 ng-click 显示/隐藏展开/折叠功能的正确内容.他是我到目前为止所拥有的,而且似乎很接近.我在代码之后描述输出.

切换选择代码:

//从 csv 获取标签块顺序$scope.tabs= tabsService.mainTabs;//将活动类分配给主选项卡式导航$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 ng-repeat="currentTabSideNavItems 中的项目"><a ng-click="toggleChildIndex($index)">{{item}}</a><div ng-repeat="child in currentSideNavChildren"><a ng-click="toggleChildIndex($index)">{{child}}</a>

使用此代码,当我单击相应的父部分标题时,会显示正确的子项,但子项显示在当前页面上的所有父项下.例如:

选择第一个选项卡并单击父部分 1 时:

父部分 1父 1 子 1父 1 子 2家长第 2 节父 1 子 1父 1 子 2

选择第一个选项卡并单击父部分 2 时:

父部分 1父 2 子 1父 2 子 2父 2 子 3家长第 2 节父 2 子 1父 2 子 2父 2 子 3

我是不是又把事情复杂化了?我该如何努力让合适的孩子与他们的父母在一起?非常感谢您抽出宝贵时间,非常感谢.

解决方案

在我看来,您确实希望将其保留在 UI 之外并将其隐藏在控制器中.从外观上看,您的控制器中已经有一个名为 toggleSelect 的操作,在其中您可以将新的范围属性设置为仅当前选定选项卡的侧边导航项,例如;

$scope.toggleSelect = function(index) {//现有代码在这里$scope.currentTabSideNavItems = SideNavItems[index];}

您的标记可以简化为:

<div ng-repeat="currentTabSideNavItems 中的项目"><a ng-click="level=2">{{item}}</a>

这样提取要显示的项目的逻辑就很好地隐藏在您的控制器中,并且可以进行测试.

I have the the following two dimensional array:

 SideNavItems= [["Parent Section 1", "Parent Section 2"],["Parent Section 3", "Parent Section 4"]]

I want to use ng-repeat to display them the following way on a page that has tabbed navigation:

Desired output:

When first tab is selected:

Parent Section 1
Parent Section 2

When second tab is selected:

Parent Section 3
Parent Section 4

Current output:

When first tab is selected:

Parent Section 1
Parent Section 2
Parent Section 3
Parent Section 4

I have a $index variable that changes based on what tab is selected. Can I set it so that only the contents of the first array(element 0 of the 2D array) are repeated when the index is 0, the contents of the second array(element 1 of the 2D array) are repeated when the index is 1, and so on if the 2D array had more elements?

Here is the html I have for repeating the arrays:

            <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>

and I don't know if this will be very relevant but here is the HTML for the tabs. The getClass methods giving active classes that change the tabs appearance in CSS. The toggleSelect method changes the current index to the active tab.

<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>

I have similar arrays with the child sections for each of these parent sections and hope to do the same thing if there is a way to make this work. I guess I would need another variable to act as a second index though...

***EDIT 2/3/15 2:00PM

Using your advice, I was able to get my desired output. I am now trying to get similar functionality for the children of these parent sections and it seems a bit more complicated. I actually have a 3D array:

 [
   [[Parent1Child1, Parent1Child2],[Parent2Child1, Parent2Child2, Parent2Child3]],
   [[Parent3Child1, Parent3Child2],[Parent4Child1, Parent4Child2, Parent4Child3]]
 ]

Desired Output: When first tab is selected:

Parent Section 1
  Parent1Child1
  Parent1Child2

Parent Section 2
  Parent2Child1
  Parent2Child2
  Parent2Child3

When second tab is selected:

Parent Section 3
  Parent3Child1
  Parent3Child2

Parent Section 4
  Parent4Child1
  Parent4Child2
  Parent4Child3

Each section will eventually be an collapsable/expandable accordion and that's what I started coding toward but maybe I need to back up and work on it spitting out the desired input shown above, and then I can play around with ng-click showing/hiding the correct content for the expand/collapse functionality. He is what I have so far and it seems to be close. I describe the output after the code.

toggleSelect code:

 //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>     

With this code, I get the proper children appearing as I click on the corresponding Parent Section header, but the children appear under all the parent sections on the current page. For example:

When first tab is selected and I click on Parent Section 1:

Parent Section 1
  Parent1Child1
  Parent1Child2

Parent Section 2
  Parent1Child1
  Parent1Child2

When first tab is selected and I click on Parent Section 2:

Parent Section 1
  Parent2Child1
  Parent2Child2
  Parent2Child3

Parent Section 2
  Parent2Child1
  Parent2Child2
  Parent2Child3

Am I over complicating things again? How might I work toward getting the proper children with their parents? Thank you so much for your time it is very appreciated.

解决方案

In my opinion, you really want to keep this out of the UI and hide it away in the controller. You already have an action called toggleSelect in your controller by the looks of things, inside that you can set a new scope property to be only the side nav items of the currently selected tab, something like;

$scope.toggleSelect = function(index) {
  // existing code here

  $scope.currentTabSideNavItems = SideNavItems[index];
}

And your markup can be simplified to:

<div id="field">
    <div ng-repeat="item in currentTabSideNavItems">
        <a ng-click="level=2">{{item}}</a>
    </div>
</div>

That way the logic for extracting out the items to be displayed is hidden nicely in your controller, and can be tested.

这篇关于AngularJS ng-repeat 二维数组并根据索引仅显示某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆