ng-repeat问题,重复n * n次

时间:2013-03-02 17:59:37

标签: angularjs

  

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}],“model”:“project”,“method”:“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>');
      }; 

0 个答案:

没有答案