I just started with AngularJS for about 2 weeks and I was not able to get a solid answer to this question. So I decided to take care of myself, that's what I came up with.
Two filters are listed below: nRange returns an array containing the sequence increased by n, which is limited by numItems - numItems% n. Where the range returns an array of a linear sequence starting at 0. Filters can be thought of as helper functions for the following AngularJS code.
website.filter('nRange', function() { return function(input, numItems, n) { if(numItems == 0) { return null; } range = numItems - numItems%n; for(i = 0; i <= range-n; i += n) { input.push(i); } if(numItems%n != 0) { input.push(range); } return input; }; }); website.filter('range', function() { return function(input, numItems) { for (var i=0; i<=numItems; i++) { input.push(i); } return input; }; });
$ Scope includes projects and the number of projects in the projectCols line. By defining projectCols, we can quickly and easily change the layout.
<div class="container"> <div class="row" ng-repeat="i in [] | nRange:projects.length:projectCols"> <div class="col-md-{{12/projectCols}} col-sm-12 col-xs-12" ng-repeat="j in [] | range:projectCols-1"> <project-item ng-if="projects[i+j]" info="projects[i+j]"></project-item> </div> </div> </div>
The following HTML uses filters to populate project elements. What the code does, it uses nRange to count for a certain amount, in other words, it's like a for loop with i + = n for incrementing. Then the range provides a subrange, in total it can be considered as the following:
for(i = 0; i < nRange; i+=n) { for(j = 0; i < range; i++) { if($scope.projects[i+j]) {
Together, this allows you to have 2, 3, 4, or 6 projects per line.
I also created some css classes to display the project correctly.
.project { border: 1px solid gray; border-radius: 5px; background: whitesmoke; } .col-md-6 .project { margin: 30px; padding: 30px; height: 450px; } .col-md-4 .project { margin: 20px; padding: 20px; height: 400px; } .col-md-3 .project { margin: 10px; padding: 10px; height: 400px; } .col-md-2 .project { margin: 5px; padding: 5px; height: 400px; }
Here is my repository for my site that contains my full implementation.
https:
Here is a live demo! http://www.jraraujo.com/