如何在ng中使用不同的内容类型根据某些条件重复?

时间:2014-12-17 14:52:54

标签: css angularjs twitter-bootstrap

对于我的问题,我正在尝试从服务器接收的数据中创建一个列表。在此列表中,默认情况下只显示5个顶级项目,如果列表items["instance"]包含的项目超过5个,则应显示more按钮&点击此按钮,它应显示该手风琴内容的其余项目。

以下是我的代码

<div class="container-fluid" ng-controller="MainCtrl" id="bpNaminConvention">

                <accordion>
                    <accordion-group is-open="item.open" ng-repeat="item in namingConventions">
                        <accordion-heading style="margin-left: 0px">
                            <span class="pull-left glyphicon" ng-class="{'down-accordion': item.open, 'up-accordion': !item.open}" style="margin-right: 10px"></span>
                            <label class="filter-headItem">
                                <!-- <input type="checkbox" class="pull-left glyphicon" ng-click="filterHeaderClick(item.title, $event)" ng-model="master" value="{{item.title}}" style="margin-top:1px; margin-right:5px" /> -->
                                {{ item.title }}
                            </label>
                            <span style="margin-left: 15px;padding-left:10px; padding-right:10px; background-color: #FFCC4E">{{item.itemCount}}</span>
                        </accordion-heading>
                        <div>
                            <table style="margin-left: 12px">
                                <tbody>
                                    <tr>
                                        <td>
                                            <label class="filter-headItem">{{item.title}} should be named as </label><b> {{ item.contentText }}</b>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <label class="filter-subItem">
                                                Example.
                                                <p>
                                                    <span ng-repeat="data in item.exampleText">
                                                    {{data}}<br/> 
                                                </span>
                                                </p>
                                                <br/> {{item.itemCount}} instance located for improper naming.
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <label id="namingConventionInstances" class="filter-headItem">
                                                <ul ng-repeat="instance in item.filterItems" ng-show="item.filterEnabled" ng-switch="$last">
                                                    <li>
                                                        <span ng-switch-default>{{instance}}<br/></span>
                                                        <a class="namingConventionMoreATag" ng-click="showAllNamingConventionItems(item.title)" ng-switch-when="true">{{instance}}</a>
                                                    </li>
                                                </ul>
                                                <ul ng-repeat="instance in item.instance" ng-hide="item.filterEnabled">
                                                    <li><span>{{instance}}</span></li>
                                                </ul>
                                            </label>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </accordion-group>
                </accordion>

</div>

$scope.init = function() {
    var result = [{"title":"Agents","contentText":"<Agents>","exampleText":["For Windows: Marketing","For Linux: Automation"],"itemCount":6,"open":true,"filterItems":["FIB","JAQULIN","NOMNOM","JAGERY","MINC","more"],"filterEnabled":true,"instance":["FIB","JAQULIN","NOMNOM","JAGERY","MINC","WIN2008-64","more"]},{"title":"Task","contentText":"<Connection>","exampleText":["DC_Oracle_Contact_INS","DC_Oracle_Contact_UPD","DC_Oracle_Contact_DEL","DC_Oracle_Contact_UPSERT"],"itemCount":10,"open":false,"filterItems":["C_FIB","C_TEST","C_TEST_2","DB1","DB1_2","more"],"filterEnabled":true,"instance":["C_FIB","C_TEST","C_TEST_2","DB1","DB1_2","DC1","DC2 FIB","DB to DB SM","DB to DB FIB","DR1_som","more"]},{"title":"Task Flow","contentText":"<Source Type>_<Target Type>_<Operation Type>","exampleText":["DC_Oracle _INS","DC_Oracle _UPD","DC_Oracle _DEL","DC_Oracle _UPSERT"],"itemCount":1,"open":false,"filterItems":["Task flow1"],"filterEnabled":false,"instance":["Task flow1"]},{"title":"Scheduler","contentText":"Schedule_<Frequency>","exampleText":["Schedule_Five_Minutes","Schedule_Hourly","Schedule_Daily"],"itemCount":5,"open":false,"filterItems":["daily","every 45 min","s2","test","testing"],"filterEnabled":false,"instance":["daily","every 45 min","s2","test","testing"]}];
    $scope.renderNamingConventionAccord(result);

  }
$scope.showAllNamingConventionItems = function(item) {
    console.log(item);
  }

  $scope.renderNamingConventionAccord = function(result) {
    $scope.namingConventions = result;
  }

  $scope.init();

下面是我手风琴的截图

![在此处输入图片说明] [1]

以下是已更新 unfinished plunker link

总结一下,我需要针对以下两个问题的解决方案

  1. 如果列表项超过5,则应显示more,可点击以显示其余项目。
  2. more按钮不应包含li项目符号。
  3. 更新

    添加有关我使用getBestPracticesNamingConventionData功能的原因的详细说明(请查看plunker link "helper.js"

    我正在准备手风琴使用的result函数中的服务器端数据getBestPracticesNamingConventionData。基本上需要以下项目

    items["title"] //title of accordion
    items["contentText"] //some text
    items["exampleText"] //some more text
    items["itemCount"] //*this count should be used to check >5 and truncate extra items and display only when more button is clicked
    items["open"] //to trigger accordion open 
    items["instance"] //*this is the item list which needs to be truncated and shown accordingly as stated above.
    

    所以基本上我需要帮助*以上项目,如何在手风琴中使用它。

    更新

    根据NewDev的要求,我准备了一个最简单的东西。

2 个答案:

答案 0 :(得分:1)

感谢您简化问题中的代码。

要解决此问题,您需要以下内容:

首先,您需要定义一个本地(对accordion的{​​{1}})变量的当前迭代来更改要显示的项目数。一种方法是在控制器中定义它,但在这种情况下,最好使用ng-repeat。这需要在ng-init的{​​{1}}的任何祖先元素上完成,例如在item.instance上:

ng-repeat

然后,要实际限制<td>结果的数量,您可以使用<td ng-init="limit = 5"> 过滤器。对筛选后的列表进行别名会很有帮助 - 这只适用于Angular 1.3。所以,这将是:

ng-repeat

最后,您需要一个“显示更多”按钮。这是limitTo别名有用的地方,因为如果显示的实体数小于总数,您只想显示按钮:

<td  ng-init="limit = 5">
  <ul ng-repeat="instance in item.instance | limitTo: limit as shownInstances">
    <li>{{instance}</li>
  </ul>
</td>

这是您更新的plunker

答案 1 :(得分:0)

我已经解决了我的问题,plunker link。得到here的帮助。现在使用limitTo directive限制列表项谢谢大家。

<label class="filter-headItem" >
    <ul>
        <li ng-repeat="instance in item.instance | limitTo:item.limit">
            <span>{{instance}}</span>
        </li>
    </ul>
    <a href ng-click="item.limit = item.instance.length; item.filterEnabled = false" ng-show="item.filterEnabled">more</a>
</label>

我正在改变我准备数据的方式。添加限制并在需要时更改它并隐藏更多按钮

for(i=0; i<bp_titles.length; i++) {
    var items = {}, maxFilterItems = 5, tempContentArray = [], tempActArray = [], more = "more";        
    items["title"] = bp_titles[i];
    items["contentText"] = bp_contentText[i];
    items["exampleText"] = bp_exampleText[i];
    items["itemCount"] = contentArray[i].length;
    items["limit"] = maxFilterItems;

    if (i==0) {
        items["open"] = true;
    }
    else
        items["open"] = false;

    if (contentArray[i].length > maxFilterItems) {
        items["filterEnabled"] = true;
    }
    else {
        items["filterEnabled"] = false;
    }
    items["instance"] = contentArray[i];
    if (contentArray[i].length != 0) {
        mainArray.push(items);
    }        
}