JSON对象{“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}],“模型”:“项目”,“方法”:“get_entities”}
我有一个表格来显示项目列表及其持续时间。在表格的左侧,我ng-repeat
用来列出项目的名称,即{{item.data.project}}
. 它工作正常。
在右侧,每个项目都有相应的持续时间(从开始日期到结束日期)。所以我用另一个ng-repeat
来获取每个实体并timeline(starDate,endDate)
用来绘制一个块。右边,每一行(每个项目)都有一个时间块,如果我有 n 个项目,它将显示 n 行,每一行都有一个时间块。但是,现在每行显示 n 个块;在一行中,它显示了所有项目的所有时间块。
<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>
在 JavaScript 中,我用$(".gantt-item ul.gantt-days").append
365 个方框来表示 365 天。时间线的方法是绘制蓝色块,即项目持续时间。
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>');
};