How would I use AngularJS ng-repeat with Twitter Bootstrap's scaffolding?

前端 未结 5 1990
既然无缘
既然无缘 2020-12-24 12:39

How would I use AngularJS ng-repeat to display the following HTML (Twitter Bootstrap Scaffolding)? Essentially, every third record I need to close the <

相关标签:
5条回答
  • 2020-12-24 12:46

    Here's a way:

    <div ng-controller="MyCtrl">
      <div ng-repeat="project in projects">
        <span ng-if="$index % 3 == 0">
          <hr />
          <div class="row">
            <h3 class="span4" ng-if="projects[$index+0]">{{projects[$index+0]}}</h3>
            <h3 class="span4" ng-if="projects[$index+1]">{{projects[$index+1]}}</h3>
            <h3 class="span4" ng-if="projects[$index+2]">{{projects[$index+2]}}</h3>
          </div>
        </span>
      </div>
    </div>
    

    This way will also work if you have for example 7 data items: on the last 3 data, it will only show item 7 and not try to show the nonexistant item 8 and 9.

    http://jsfiddle.net/4LhN9/68/

    EDIT: Updated to use ng-if & angular 1.2.12

    0 讨论(0)
  • 2020-12-24 12:57

    A more elegant way is to use the view model to provide a chunked collection and then let the view handle it like

    <div ng-controller="Controller">
        Projects <input ng-model="projects"></input>
        <hr/>
        <div ng-repeat="row in rows">
            <div ng-repeat="project in row">
                Projects {{project}}
            </div>
            <hr/>
        </div>
    </div>​
    

    and the coffeescript is pretty simple

    # Break up an array into even sized chunks.
    chunk = (a,s)->
        if a.length == 0
            []
        else               
            ( a[i..i+s-1] for i in [0..a.length - 1 ] by s)
    
    @Controller = ($scope)->
        $scope.projects = "ABCDEF"
    
        $scope.$watch "projects", ->      
           $scope.rows = chunk $scope.projects.split(""), 3
    
    angular.bootstrap(document, []);
    

    http://jsfiddle.net/ADukg/370/

    0 讨论(0)
  • 2020-12-24 13:02

    ng-repeat-start and ng-repeat-end end points seems a convenient way to achieve this :

    Which leads to simple code

    <div ng-controller="MyCtrl">
        <div ng-repeat-start="project in projects">
            <span>
                <div class="row">
                    <h3 class="span4">{{project}}</h3>
                </div
            </span>
        </div>
        <hr ng-repeat-end ng-if="($index + 1) % 3 == 0" />
    </div>
    

    See this jsfiddle

    0 讨论(0)
  • 2020-12-24 13:03

    To keep view logic out of the controller and have a more re-usable solution, you can create a custom filter that splits the array into row groups:

    angular.module('app').filter('group', function() {
        /**
         * splits an array into groups of the given size
         * e.g. ([1, 2, 3, 4, 5], 2) -> [[1, 2], [3, 4], [5]]
         */
        return function(array, groupSize) {
            return _.groupBy(array, function(val, index) {
                return Math.floor(index / groupSize);
            });
        };
    });
    

    And in the view:

    <div class="row" ng-repeat="group in projects | group:3">
       <div class="span4" ng-repeat="project in group">
          {{project}}
       </div>
    </div>
    

    To include the hr you can use the ng-repeat start and end points:

    <div class="row" ng-repeat-start="group in projects | group:3">
       <div class="span4" ng-repeat="project in group">
          {{project}}
       </div>
    </div>
    <hr ng-repeat-end />
    
    0 讨论(0)
  • 2020-12-24 13:11

    Moving on from the more easy answer. I dislike the original solution because of the empty dom elements it produces. This is for 3 divs per row.

    <div ng-controller="ModulesCtrl">
        <div class="row" ng-repeat="i in fields.range()">
            <div class="span4" ng-repeat="field in fields.slice(i,i+3)">
                <h2>{{field}}</h2>
            </div>
        </div>
    </div>
    

    In function FieldsCtrl($scope):

    $scope.fields.range = function() {
        var range = [];
        for( var i = 0; i < $scope.fields.length; i = i + 3 )
            range.push(i);
        return range;
    }
    

    If you know fields.length you can in place of fields.range() use [0,3,6,9] etc

    0 讨论(0)
提交回复
热议问题