26

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.

1

1 Answer 1

44

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

Sign up to request clarification or add additional context in comments.

9 Comments

Is it easy to add an extra level? :P
@user2195592 I don't see why not, what do you mean exactly?
So each value in the example is in fact an array. I thought if I simplified it, it would be easier to explain, but I really can't work out how to add it from that example!
@user2195592 sorry, I really don't understand what you mean, if you provide me with an example of the concrete data structure that you are working with I will help you to adapt it for this example.
@user2195592 wow! But that's not the data structure that you described in your question... That's another story, let me have a look at it, please stay connected for the next few minutes.
|

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.