Louis345
Louis345

Reputation: 740

creating a table using ng-repeat from JSON

I have just started using Angular for my project. The task at hand, is I need to generate a table from JSON. I was able to do this by hardcoding everything, but the project changed in a way that now I am required to use JSON. I would like to use ng-repeat. My data structure is an array of objects with nested objects that represent the business and the hours. I am getting weird behavior and I'm wondering if it is my data structure causing it.

I created a fiddle. Any advice would be greatly appreciated.

My data structure looks like this:

var dept = {
sales : { startTime : "", endTime : "" },
service : { startTime : "", endTime : "" },
accounting : { startTime : "", endTime : "" },
parts : { startTime : "", endTime : "" },
bodyShop : { startTime : "", endTime : "" },
other : { startTime : "", endTime : "" },

}; The objects are nested inside an array and each index represents a day of the week. For example, index 1 would be Monday.

   <tr ng-repeat="hours in businessHours">
    <td>Monday</td>
    <td>{{hours[0].startTime}}</td>
    <td>{{hours[0].endTime}}</td>
    <td>{{hours[0].startTime}}</td>
    <td>{{hours[0].endTime}}</td>
    <td>{{hours[0].startTime}}</td>
    <td>{{hours[0].endTime}}</td>    
</tr>

I created a fiddle to give a better picture

`http://jsfiddle.net/yu216x5w/4/`

Upvotes: 0

Views: 484

Answers (1)

panzhuli
panzhuli

Reputation: 2940

You can try something like this:

var hours = [
        {
        "name" : "Monday",
      "hours": {
        "sales" : { startTime : "5", endTime : "6" },
        "service" : { startTime : "2", endTime : "3" },
        "accounting" : { startTime : "4", endTime : "6" },
        "parts" : { startTime : "10", endTime : "11" },
        "bodyShop" : { startTime : "3", endTime : "8" },
        "other" : { startTime : "a", endTime : "b" }
        }
    },
    {
        "name" : "Tuesday",
      "hours": {
        "sales" : { startTime : "5", endTime : "6" },
        "service" : { startTime : "2", endTime : "3" },
        "accounting" : { startTime : "4", endTime : "6" },
        "parts" : { startTime : "10", endTime : "11"},
        "bodyShop" : { startTime : "3", endTime : "8" },
        "other" : { startTime : "a", endTime : "b" }
        }
    }
];


var mockDataForThisTest = "json=" + encodeURI(JSON.stringify(hours));


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

function businessHours($scope, $http) {

    $scope.schedule = [];

    $scope.loadHours = function() {
        var httpRequest = $http({
            method: 'POST',
            url: '/echo/json/',
            data: mockDataForThisTest

        }).success(function(data, status) {
        console.log(data);
            $scope.schedule = data;
        });

    };

}

with:

    <div ng-app="myApp">
<div ng-controller="businessHours">
    <p>    Click <a ng-click="loadHours()">here</a> to load data.</p>
<table>
    <tr>
      <th></th>
    <th style="vertical-align:top" scope="col" colspan="2">Sales</th>
   <th style="vertical-align:top" scope="col" colspan="2" >Service</th>
  <th style="vertical-align:top" scope="col" colspan="2">Parts</th>
   <th style="vertical-align:top" scope="col" colspan="2">Accounting</th>
    <th style="vertical-align:top" scope="col" colspan="2">Body Shop</th>
    <th style="vertical-align:top" scope="col" colspan="2" >Other</th>
    </tr>
    <tr ng-repeat="day in schedule">
        <td>{{day.name}}</td>
        <td>{{day.hours.sales.startTime}} - {{day.hours.sales.endTime}}</td>
        <td>{{day.hours.service.startTime}} - {{day.hours.service.endTime}}</td>
        <td>{{day.hours.accounting.startTime}} - {{day.hours.accounting.endTime}}</td>
        <td>{{day.hours.parts.startTime}} - {{day.hours.parts.endTime}}</td>
        <td>{{day.hours.bodyShop.startTime}} - {{day.hours.bodyShop.endTime}}</td>
        <td>{{day.hours.other.startTime}} - {{day.hours.other.endTime}}</td>    
    </tr>
</table>
</div>

http://jsfiddle.net/yu216x5w/7/

Upvotes: 2

Related Questions