Splitting ng-repeat every 3 items

前端 未结 1 1538
庸人自扰
庸人自扰 2021-02-06 06:19

Using AngularJS, I\'m iterating over a JSON object containing an array of event objects, containing an array of competition objects.

I wish to show each event

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 07:05

    You are using a table but the semantics of your data indicate you have a list of lists. You should consider outputting this with

    • instead of a grid.

      • {{listing.name}}

        • {{competition.id}} - {{competition.name}}

      You can achieve your desired layout with CSS quite easily using the above HTML. Checkout the "block grids" in Twitter Bootstrap or Foundation for examples. Tables should generally be used only for data that is actually tabular.

      However...

      You're question does still bear answering since there may be other reasons to alternate templates in the way you suggest. You could use a function for this to get things three at a time:

      {{listing.name}}
      {{competition.id}} - {{competition.name}}

      In your controller you can create a "list of lists" to bind to the nested repeaters

      // Adapted from Fresheyeball's solution
      
      $scope.competitions = []
      compSet = []
      for(var i; i < $scope.listings.competitions; i++){
         var competition = $scope.listings.competitions[i];
         if( i % 3 ){
            $scope.competitions.push(compSet);
            compSet = [];
         }
         compSet.push(competition);
      }
      

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