1

In HTML, I want to display multiple rows of items(tasks timeline). In each row, gantt-items,gantt-days is used to plot 365 small boxes which are presented 365 days in a year. And each row need to display a blue block, which means the assigned period of this task. {{timeline(item.data.sDate,item.data.eDate)}}, each row only contains one blue block.

HTML

<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>

JS

angular.module('myApp', ['ngResource']);
   function MainCtrl($scope,$resource){
      $(document).ready(function(){
         ...
         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>');
};

When I am using ng-repeat, $(".gantt-item ul.gantt-days").append doesn't work. And timeline(item.data.sDate,item.data.eDate) is repeating n times in each row, which supposes to show only one item per raw of n rows.

1 Answer 1

2

It happens because the elements in the ng-repeat directive appear after the DOM compiling.
Try using $watch:

$scope.$watch('items.entities', function(){
    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));
     }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you of your solution. However, my ng-repeat repeating n times in every rows, which should display only 1 item per row, and repeating n rows.
To be more specific of my ng-repeat problem. [link]stackoverflow.com/questions/15177632/…
@elephant I have the same problem with ng-repeat. I have an array with 5 items and it repeats each item 5 times with total of 25 rows. If I try with 3 items it repeats 3 times for each item. I was not able to reproduce this in JS fiddle or anywhere outside of my project. Can you tell me what was the solution to this issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.