Question

I'm having some difficulties generating the desired table structure with a nest ng-repeat. I have a table header for each day of the week and I want to display three shifts in the <td> beneath that. Currently I solve this with three <span>'s in one <td>, but I would like to generate three <td>'s for each shift in on day. My html code with the repeats is:

<div>
    <table>
    <caption>Week: {{ week }}</caption>
        <thead>
            <tr>
                <th ng-repeat="day in days">
                <span>{{day.Date}}/{{ month }}/{{ year }}</span>    
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td ng-repeat="day in days">
                    <span ng-repeat="shift in shifts">{{shift.Name}}</span>
                </td>
            </tr>
        </tbody>
    </table>
</div>

I have tried <th colspan="3"> with different placement of "day in days" repeats, but I can't seem to get the wanted structure for the table body:

<tr>
    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>

    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>

    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>

    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>

    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>

    <td>shift1</td>
    <td>shift2</td>
    <td>shift3</td>
</tr>

My question is: how can I rewrite my current code with the <span>'s so that it generates the wanted structure above.

Was it helpful?

Solution 2

You can create new plain list in your model to iterate over and use it instead of using nested loops. If data is to be changed then you can watch changes of dependent properties and reassemble plain list for each change. Something like this:

JavaScript

angular.module('app',[]).
  controller('appController', function($scope) {
    var i, j;

    $scope.week = 1;
    $scope.days = [{
      day: 1,
      month: 2,
      year: 2014
    }, {
      day: 2,
      month: 2,
      year: 2014
    }, {
      day: 3,
      month: 2,
      year: 2014
    }];

    $scope.shifts = ['Shift1', 'Shift2', 'Shift3'];

    // Create new plain list each time shifts is changed
    $scope.$watchCollection('shifts', function() {
      $scope.daysShifts = [];
      for(i=0; i<$scope.days.length; i++) {
        for(j=0; j<$scope.shifts.length; j++) {
          $scope.daysShifts.push({
            name: $scope.shifts[j]  
          });
        }
      }
    });

    $scope.addShift = function() {
      $scope.shifts.push('Shift'+($scope.shifts.length+1));
    }
  });

HTML

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@*" data-semver="1.2.11" src="http://code.angularjs.org/1.2.11/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="appController">
    <div>
      <table>
        <caption>Week: {{week}}</caption>
        <thead>
          <tr>
            <th ng-repeat="day in days" colspan="{{shifts.length}}">
              <span>{{day.day}}/{{day.month}}/{{day.year}}</span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td ng-repeat="dayShift in daysShifts">
              <span>{{dayShift.name}}</span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <button ng-click="addShift()">Add Shift</button>
  </body>

</html>

Plunker: http://plnkr.co/edit/zZYrurhp3HQA9Jp9QHDo?p=preview

OTHER TIPS

If I understood your problem correctly then I think you are looking for ng-repeat-start & ng-repeat-stop.

HTML:

<div ng-app>
    <div ng-controller="ExampleCtrl">
       <table border="1">
           <tr>
               <td ng-repeat-start="day in days" ng-show="false"></td>
               <td ng-repeat="shift in day.shifts" ng-repeat-end>shift{{shift}}</td>
           </tr>
        </table>
    </div>
</div>

JS Controller:

function ExampleCtrl($scope) {
   $scope.days = [
       {
           shifts: ["1","2","3"]
       },
       {
           shifts: ["1","2","3"]
       },
       {
           shifts: ["1","2","3"]
       },
       {
           shifts: ["1","2","3"]
       },
       {
           shifts: ["1","2","3"]
       }
   ];
}

and the Fiddle

shifts outside days object: Fiddle update

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top