1

Json data:-

[{
    location: x,
    id: 1,
    name: A
  },
  {
    location: x,
    id: 2,
    name: B
  },
  {
    location: y,
    id: 3,
    name: C
  },
  {
    location: y,
    id: 4,
    name: D
  }
]

I want the output in this format:

+----------+------+------+
| location |  id  | name |
+----------+------+------+
|          |   1  |  A   |
|    x     +------+------+
|          |   2  |  B   |
+----------+------+------+
|          |   3  |  C   |
|    y     +------+------+
|          |   4  |  D   |
+----------+------+------+

In Angular JS how can i get this type of output.

4
  • Create a html table to display that json Commented Nov 3, 2015 at 9:14
  • i used rowspan but not able to get the solution Commented Nov 3, 2015 at 9:15
  • Can you please add html which you have try Commented Nov 3, 2015 at 9:21
  • the table should be displayed using Angular jS Commented Nov 3, 2015 at 9:28

1 Answer 1

2

You should count the number of same locations before displaying the data.

var localData = [

    { location:'x', id:1, name:'A' },
    { location:'x', id:2, name:'B' }, 
    { location:'y', id:3, name:'C' }, 
    { location:'y', id:4, name:'D' },
    { location:'y', id:5, name:'E' },
    { location:'z', id:6, name:'F' },
  ],
  i, j;

  for(i = 0; i < localData.length; i++){
      var l1 = localData[i].location;

      localData[i].rowspan = 1;

      for(j = i+1; j < localData.length; j++){
        var l2 = localData[j].location;
        if(l1 == l2){
          localData[i].rowspan += 1;
        }
        else{
          break;
        }
      }
      i = j-1;
  }
$scope.data = localData;

Table:

 <table border="1" cellpadding="15">
     <tr ng-repeat="row in data">
      <td ng-if="row.rowspan" rowspan={{row.rowspan}}>{{row['location']}} </td>
      <td>{{row['id']}}</td>
      <td>{{row['name']}}</td>
     </tr>
 </table>

Here is a working plunker:

http://plnkr.co/edit/YRDtrwJoA7266CzWMAJ0?p=preview

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

6 Comments

if the data is like this $scope.data = [{ location:'x', id:1, name:'A' }, { location:'x', id:2, name:'B' }, { location:'y', id:3, name:'C' }, { location:'y', id:4, name:'D' },{ location:'y', id:5, name:'E' }]; });
In this case can we use rowspan?.. because y should align to 4 and x should display between 1 and 2.
Then you will have to count the number of locations with the same value before displaying the data, and then add rowspan with that value.
Ya i can count the no of location and send to html..but not able to put the different count .Can you do it for hard coded rowspan 2,3,4....... json data: $scope.data = [ { location:'x', id:1, name:'A' }, { location:'x', id:2, name:'B' }, { location:'y', id:3, name:'C' }, { location:'y', id:4, name:'D' }, { location:'y', id:5, name:'E' }, { location:'z', id:6, name:'E' }, { location:'z', id:7, name:'E' }, { location:'z', id:8, name:'E' }, { location:'z', id:9, name:'E' }, ];
See my update, made several fixes and optimizations, now it should work properly with any number of same locations.
|

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.