0

I want to create a dynamic height & width table, but I'm having trouble with the table header.

The <td> elements display just fine with this ng-repeater

<tr ng-repeat="set in currFormData.volume track by $index">
  <td ng-repeat="repSpace in set track by $index">
    {{repSpace}}
  </td>
</tr>

but I want a <th> row containing 1, 2, 3, 4... and it must adjust as the table adjusts. I'm looking for something like this which looks through only the second level of my model 1 time:

<tr>
  <th ng-repeat="repSpace in set in currFormData.volume track by $index">{{$index + 1}}</th>
</tr>

Here is my $scope model:

$scope.currFormData = {"date" : "", "volume": [
    [[1,135],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
    [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
    [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
    [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
] };

How can I make this work? I'm open to changing my model.

1 Answer 1

1

Assuming that every 'set' in your 'volume' will be of equal length:

HTML:

<th ng-repeat="i in getArray(currFormData.volume[0].length) track by $index">{{$index + 1}}</th>

Controller:

$scope.getArray = function (length) {
    return new Array(length);
}

Otherwise:

HTML:

<th ng-repeat="i in getLongestArray(currFormData.volume) track by $index">{{$index + 1}}</th>

Controller:

$scope.getLongestArray = function (arrayOfArrays) {
    var longest: number = 0;
    for (var i: number = 0; i < arrayOfArrays.length; i++) {
        if (arrayOfArrays[i].length > longest) {
            longest = arrayOfArrays[i].length;
        }
    }
    return (new Array(longest));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.