1

I want to display the angular array string values in row of four columns in a table. The array has values like:

["str1", "str2", "str3","str4","str5","str6","str7","str8" and so on]

I want to display it in table of four columns such that the table should look like :

col1    col2   col3   col4
--------------------------
str1    str2   str3   str4
str5    str6   str7   str8

My controller code:

$scope.output=["str1", "str2", "str3","str4","str5","str6","str7","str8" ....]   
var one=[];
var two=[];
var three=[];
var four=[];

for( var i=1; i<=$scope.output.length ;i++)
{
 if((i)%4==0){
 one.push($scope.output[i]);
 }
 else if((i)%4==1)
{ two.push($scope.output[i]);}

}
else if((i)%4==2)
{ three.push($scope.output[i]);}

 }
  else if((i)%4==3)
{ four.push($scope.output[i]);}

 }

 $scope.output = {one: one , two: two , three : three , four :four};

My html code :

<table>
  <tr ng-repeat="one in output.one track by $index" >

          <td><div>{{one}}</div></td>
          <td><div>{{output.two[$index]}}</div></td> 
          td><div>{{output.three[$index]}}</div></td>
          <td><div>{{output.four[$index]}}</div></td> 

     </tr>
 </table>

But above code not solving the problem.How to solve this?

2

1 Answer 1

1

try like this.

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
  
  $scope.index = 0;
   $scope.items =["str1", "str2", "str3","str4","str5","str6","str7","str8","str9","str10"];
   $scope.array = [];
  for(var i=0; i<$scope.items.length/4;i++)
    $scope.array.push(i);
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   
  <table>
    <tr>
      <td>Col1</td>
      <td>Col2</td>
      <td>Col3</td>
      <td>Col4</td>
      </tr>
     <tr ng-repeat="i in array"  ng-init="index = i*4">
       
         <td ng-repeat="one in items.slice(index,index+4)">{{one}}</td>
     </tr>
 </table>
  
</div>

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

3 Comments

nice ans i think this is perfect
did you see my answer?
ya i see nice logic, that's way i up-word you

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.