Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS repeat with table and rowspan

Tags:

angularjs

Say I have the following data structure

* Key 1
    * Value 1
    * Value 2
* Key 2
    * Value 3
    * Value 4
    * Value 5

How, with AngularJS, can I render it in a table similar to the following:

|-------|---------|
| Key 1 | Value 1 |
|       |---------|
|       | Value 2 |
|-------|---------|
| Key 2 | Value 3 |
|       |---------|
|       | Value 4 |
|       |---------|
|       | Value 5 |
|-------|---------|

The keys are done via rowspan.

like image 707
Jacob Avatar asked Oct 25 '14 18:10

Jacob


1 Answers

Nice and tricky question!

One way to do it would be:

Given an object like this:

$scope.testData={
    key1:[1,2],
    key2:[3,4,5]
};

You could do this:

<table>
    <tr ng-repeat-start="(key, val) in testData">
        <td rowspan="{{val.length}}">{{key}}</td>
        <td>{{val[0]}}</td>
    </tr>
    <tr ng-repeat-end ng-repeat="value in val.slice(1)">
        <td>{{value}}</td>
    </tr>
</table>

Example

like image 58
Josep Avatar answered Nov 08 '22 09:11

Josep