JSON object{"count": 4, "entities": [{"model": "project", "data": {"project": "test1", "startDate": 2013/03/15, "endDate": "2013/03/31"}, "id": 1}, {"model": "project", "data": {"project": "test2", "startDate": 2013/01/15, "endDate": "2013/01/31"}, "id": 2}, {"model": "project", "data": {"project": "test3", "startDate": 2013/02/15, "endDate": "2013/02/31"}, "id": 2}], "model": "project", "method": "get_entities"}
I have a table to show a list of projects with their duration. At left hand side of table I use ng-repeat to list down the name of projects, which is {{item.data.project}}. It works fine.
At right hand side, each project has corresponding duration (from start date to end date). So I use another ng-repeat to get each entity and use timeline(starDate,endDate) to draw a block. By right, each row (each project) has one time block, if I have n projects it will show n rows with one time block on each row. However, now it is showing n blocks on each row; on one row it shows all the time blocks of all projects.
<div class="content">
<figure class="gantt">
<figcaption>All Projects</figcaption>
<aside>
<ul class="gantt-labels" style="margin-top: 71px">
<li class="gantt-label" ng-repeat="item in items.entities"><strong style="line-height: 35px; height: 35px">{{item.data.project}}</strong></li>
</ul>
</aside>
<section class="gantt-data">
<header>
<ul class="gantt-months" style="width: 9150px">
</ul>
<ul class="gantt-days" style="width: 9150px">
</ul>
</header>
<ul>
<li ng-repeat="item in items.entities">
<ul class="gantt-items" style="width: 9150px; height: 35px" >
<li class="gantt-item" >
<ul class="gantt-days">
</ul>
</li>
</ul>
{{timeline(item.data.sDate,item.data.eDate)}}
</li>
</ul>
</section>
</figure>
In JavaScript, I use $(".gantt-item ul.gantt-days").append to draw 365 pieces of boxes representing 365 days. And timeline method is to draw blue block which is project duration.
angular.module('myApp', ['ngResource']);
function MainCtrl($scope,$resource){
$(document).ready(function(){
...
$scope.$watch('items.entities', function(){
for(var i = 1; i < 366; i++){
$("header ul.gantt-days").append('<li class="gantt-day" style="width: 25px"><strong style="line-height: 35px; height: 35px">' + d.getDate() + '</strong></li>');
d = new Date(d.getTime() + (24 * 60 * 60 * 1000));
}
for(var i = 1; i < 366; i++){
$(".gantt-item ul.gantt-days").append('<li class="gantt-day" style="width: 25px"><span style="line-height: 35px; height: 35px">' + year + "-" + d.getMonth() + "-" + d.getDate() + '</span></li>');
d = new Date(d.getTime() + (24 * 60 * 60 * 1000));
}
});
...});
$scope.timeline = function GetLength(startDate, endDate)
{
var firstDay = new Date(new Date().getFullYear(), 0, 1);
var st1 = startDate.split("/");
var dt1 = new Date(st1[2], st1[1] - 1, st1[0]);
var st2 = endDate.split("/");
var dt2 = new Date(st2[2], st2[1] - 1, st2[0]);
var startPoint = ((dt1.getTime() - firstDay.getTime())/1000/60/60/24)*25;
var length = (1+(dt2.getTime() - dt1.getTime())/1000/60/60/24)*25;
$(".gantt-item ul.gantt-days").append('<span class="gantt-block" style="left:' + startPoint + 'px; width: ' + length + 'px; height: 27px"><strong class="gantt-block-label">Duration</strong></span>');
};