1

I want to build a table from an array that has this scheme:

var items = [
    {
        name: 'xxx',
        configurations: [
            { 
                image: 'aaa.jpg'
            },
            { 
                image: 'bbb.jpg'
            },
            ...
        ]
    },
    {
        name: 'yyy',
        configurations: [
            { 
                image: 'ccc.jpg'
            },
            { 
                image: 'ddd.jpg'
            },
            ...
        ]
    },
    ...
]

I want to to achive one single row with all images from all elements (every image is nested in one <td>, like:

<tr>
    <td>
        <img src="aaa.jpg">
    </td>
    <td>
        <img src="bbb.jpg">
    </td>
    <td>
        <img src="ccc.jpg">
    </td>
    <td>
        <img src="ddd.jpg">
    </td>
</tr>

How can I do it with angular? My code so far looks like this:

<tr>
    <td ng-repeat-start="proposition in items">
        <img ng-src="{{::proposition.configurations[0].image}}" />
    </td>
    <td ng-repeat-end>
        <img ng-src="{{::proposition.configurations[1].image}}" />
    </td>
</tr>

But obviously a number of configurations in proposition is dynamic, so how can I iterate through it, keeping the same html scheme?

2
  • So, if a configuration contains multiple images, do you want these images in the same <td>? Commented May 3, 2016 at 8:58
  • @Cerbrus No, I would like every image to be in a separate <td> Commented May 3, 2016 at 9:02

1 Answer 1

1

You have to run two ng-repeat, one for items and one for configuration.

Edit

Simplest way is to flatten your array. So you will have a long array with tuples of <image, name>

You can do that with map:

That's code:

 $scope.items =  [
    {
        name: 'xxx',
        configurations: [
            { 
                image: 'yyy.jpg'
            },
            { 
                image: 'yyy2.jpg'
            }
        ]
    },
    {
        name: 'yyy',
        configurations: [
            { 
                image: 'zzz.jpg'
            },
            { 
                image: 'zzz2.jpg'
            }
        ]
    },
];

  $scope.items = $scope.items.map(function(item){
    return item.configurations.map(function(inner){
        return { name: item.name, image: inner.image};
    })
  })

  $scope.items = $scope.items.concat.apply([], $scope.items);

You have a full working example here.

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

2 Comments

But how exactly should I do it to keep the same structure?
Well, I was hoping to deal with it just in a view, but I guess that I really need to modify my data in controller which is not perfect, but probably the only way to do it with angular. Apart from some mistakes like you create multiple tr elements (we can move ng-repeat to td) and the fact that it would be better to make a separate function in the controller to flatten the date (so we don't modify the original data), that gives the solution to my problem, thanks!

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.