0

I have an Angular variable which has values in the form of an array as [ 170,1,130,3,134,4.... and so on]. I want to display it in Tabular form such that the values appear in the table in three columns for eg. first three values in first three columns of the first row, next three values in columns of the second row and so on.

col1   col2   col3
 170   1     130
 3     134    4
 ................
 ................

My Code

<span  ng-repeat="obj in items">
<div ng-switch on="{{obj%3}}"> 
   <div ng-switch-when="0">{{obj}}</div>
   <div ng-switch-when="1">{{obj}}</div>
   <div ng-switch-when="2">{{obj}}</div>
</div>

But this code is not working. Any suggestion would be useful.

Thanks in advance.

1
  • This won't work at all the way you intend. the <span> is repeated for each obj. What you will end up with is a series of <span> elements, each containing a <div> holding another <div> inside, but it won't have any effect on their layout on the page. Commented Apr 25, 2016 at 11:24

2 Answers 2

1

Firstly, you can use ng-switch over the $index property of the isolated scope created by ng-repeat (https://docs.angularjs.org/api/ng/directive/ngRepeat).

Secondly, ng-switch is working with expressions, do not use brackets {{}} (https://docs.angularjs.org/api/ng/directive/ngSwitch).

Example:

<span  ng-repeat="obj in items">
    <div ng-switch on="$index % 3"> 
       <div ng-switch-when="0">{{obj}}</div>
       <div ng-switch-when="1">{{obj}}</div>
       <div ng-switch-when="2">{{obj}}</div>
    </div>
</span>
Sign up to request clarification or add additional context in comments.

Comments

0

Change your code into

<div class="container"> 
  <div flex="30">Col-1</div>
  <div flex="30">Col-2</div>
  <div flex="30">Col-3</div>
</div>
<div class="container"> 
  <div flex="30" ng-repeat="obj in items">{{obj}}</div>
</div>

add style for .container

.container{
  display: flex;
  flex-wrap: wrap;
}

1 Comment

This approach is not working. Columns are appearing in one below other . Any other styling required here ?

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.