1

I'm very new to angular js. My task is to create an html table from the JSON data from a web service.

JSON data will be like:

[{"StartTime":"07:00","Name":"xxxxxxxx","Slots":["07:00","07:15"]},{"StartTime":"07:30","Name":"xxxxxxxx","Slots":["07:30"]}]   

Slots length will tell us how many rows to be merge.

Table should be like:

*-------------------------------*
| Time  |  Name                 |
*-------*-----------------------*
| 07:00 | xxxxxxxxxxx           |
|-------|                       |
| 07:15 |                       |
|-------|-----------------------|
| 07:30 | xxxxxxxxxxxxxx        |
|-------|-----------------------|
5
  • This is not an valid object or json Commented Dec 25, 2014 at 12:03
  • 1
    Show us what you have tried and what problems you have encountered Commented Dec 25, 2014 at 12:04
  • A.B Json Edited . Please have a look at that. Commented Dec 25, 2014 at 12:15
  • @SindhuVinodhini yes i edited too, see answer :) Commented Dec 25, 2014 at 12:17
  • @SindhuVinodhini StackOverflow is meant for solving your doubts, please post what you have tried and where are you stuck. Commented Dec 25, 2014 at 12:56

2 Answers 2

2

I have edited your json, which was not valid. you can see in js or json object valiadator

Working Demo:

angular.module('testApp',[]).controller('testCtrl', function($scope) {
  $scope.items=[
{"StartTime":"07:00","Name":"xxxxxxxx","Slots":["07:00","07:15"]},
{"StartTime":"07:30","Name":"xxxxxxxx","Slots":["07:30"]}];
  });
div.col {
  background:pink; 
  border-bottom:1px dotted black;
  padding:2px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">
   <table border="2">
      <tr>
         <td>Time</td>
         <td>Name</td>
      </tr>
      <tr ng-repeat="item in items">
         <td >
             <div class="col" ng-repeat="i in item.Slots"> {{i}}</div>
         </td>
          <td >{{item.Name}}</td>
       </tr>
 </table>

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

Comments

2

It was quite tricky to do it with just a table alone with rowspan. You have to use ng-repeat-start/end. I also used $first to skip the first row of the nested repeat.

<table border="1">
  <thead>
    <tr>
      <th>Time</th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat-start="item in array">
      <td>{{item.Slots[0]}}</td>
      <td rowspan="{{item.Slots.length}}">{{item.Name}}</td>
    </tr>
    <tr ng-repeat="slot in item.Slots" ng-if="!$first" ng-repeat-end>
      <td>{{slot}}</td>
    </tr>
  </tbody>
</table>
td {
    vertical-align: top;
}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.