0

I have an object like

    {
      "2015": ["path",0, 0],
      "2016": ["path",0, 0],
      "2017": ["path",0, 0],
      "2018": ["path",0, 0],
    }

Using ng-repeat I am trying to display the info as a grid like below.

    <div class = "row">
      <div class = "col-sm-4"></div>
      <div class = "col-sm-4"></div>
      <div class = "col-sm-4"></div>
    </div> 

I have tried the following code

    <div ng-repeat="(exam, data) in imageJSON">
      <div class="row grid-divider" ng-if="$index%3 == 0">
        <div class="col-sm-3">{{exam}} {{index}}
        <div class="col-sm-3">{{exam}} {{index + 1}}
        <div class="col-sm-3">{{exam}} {{index + 2}}
      </div>
    </div>

I want it to display like below

table,
th,
td {
  border: 1px solid black;
}
<table>
  <tr>
    <td>
      2014
      <br>"path"
    </td>
    <td>
      2015
      <br>"path"
    </td>
    <td>
      2016
      <br>"path"
    </td>
  </tr>
  <tr>
    <td>
      2017
      <br>"path"
    </td>
    <td>
      2018
      <br>"path"
    </td>
    <td>
      2019
      <br>"path"
    </td>
  </tr>
</table>
As like table, I want to display them as bootstrap grid. In order to do that, I want to iterate over the object using their index. How to implement such grid view?

2
  • show what you have tried Commented Apr 12, 2016 at 5:44
  • Thanks, updated the question Commented Apr 12, 2016 at 5:50

3 Answers 3

3

$index is what you're looking for. (add one and bounds check for the next index) unless you're looking for the first or last item, then there's $first or $last.

https://docs.angularjs.org/api/ng/directive/ngRepeat

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

Comments

0

Updated for Object - Index can be obtained using $index service of angularjs as below:

<div>
  <div ng-repeat="(key, prop) in names track by key">
    <span>{{ $index + 1 }}</span>
    <span>{{ key }}</span>
    <span>{{names[key][0]}}</span>
  </div>
</div>

Here is the updated plunk :

Hope this helps!

4 Comments

This works in case of array. But my problem contains object for which $index doesnot work.
Updated the solution above for objects. Hope it works fine for you now.
Thank you :) But I need to iterate through the (key,prop) not the names[key]. Updated the question with more details. Hope you can get to know my problem now
Answer and plunk modified as per your requirement. I hope its the thing you wanted.
0

If I understand correctly, this should work:

<div class="table">
  <div class="row" ng-repeat="(year, array) in object">
    <div class="col-sm-4 row-header">{{ year }}</div>
    <div class="col-sm-4 row-items" ng-repeat="item in array">{{ item }}</div>
  </div>
</div> 

OR

<div class="table">
  <div class="row" ng-repeat="(year, array) in object">
    <div class="col-sm-4 row-items" ng-repeat="item in array">{{ year }} {{ item }}</div>
  </div>
</div> 

depending on what you mean.


Edit

Markup:

<table>
  <tr ng-repeat="row in imageJSON">
    <td ng-repeat="item in row">
      {{ item.exam }}
      <br>{{ item.data }}
    </td>
  </tr>
</table>

Angular:

var CELLS_IN_ROW = 3;
var imageJSON = {
  "2015": ["path",0, 0],
  "2016": ["path",0, 0],
  "2017": ["path",0, 0],
  "2018": ["path",0, 0]
};

var array = [];
var count = 0;

angular.forEach(imageJSON, function (value, key) {
  array[Math.floor(count / CELLS_IN_ROW)] = array[Math.floor(count / 3)] || [];
  array[Math.floor(count / CELLS_IN_ROW)].push({
    exam: key,
    data: value[0]
  });
  count++;
});

$scope.imageJSON = array;

1 Comment

Thank you. But I need to iterate through the (year, array) as col-sm-4 value rather than "item in array". I have updated the question with more details. Hope you can get a clear picture of my problem now

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.