2017-04-13 139 views
2

我想做一個循環,以便將我所有的數據放入我的圖表。但是,我不知道該怎麼做。我嘗試了一些東西,但不起作用。我得到一個在環路的線路:甘特javascript循環問題

$(".gantt").gantt({ 
    source: [{ 
    name: "Tasks", 
    desc: "", 

    for (var i = 0, z = tab1.length; i < z; i++) { 

     values: [{ 
     from: "/Date(" + tab2[i].getTime() + ")/", 
     to: "/Date(" + tab3[i].getTime() + ")/", 
     label: tab1[i], 
     customClass: "ganttRed" 
     }] 
    } 
    }] 
}); 

回答

1

不能運行在一個對象的聲明一樣,中間的for循環。像這樣的東西應該工作壽:

$(".gantt").gantt({ 
    source: [{ 
     name: "Tasks", 
     desc: "", 
     values: tab1.map(function(tab, index) { 
      return { 
       from: "/Date("+tab2[index].getTime()+")/", 
       to: "/Date("+tab3[index].getTime()+")/", 
       label: tab, 
       customClass: "ganttRed" 
      } 
     }); 
    ] 
} 
0

首先感謝您快速的答案,我都試過,你說什麼,它的工作,但不正是我想要的:enter image description here

我想什麼是兩個工作(任務1,任務2)在兩條不同的線上,而不是在一條線上。 這裏是代碼:

var tab1 = []; 

tab1[0] = "Task 1"; 
tab1[1] = "Task 2"; 

var tab2 = []; 

tab2[0] = new Date("2017-04-04T00:00:00"); 
tab2[1] = new Date("2017-04-04T12:52:00"); 

var tab3 = []; 

tab3[0] = new Date("2017-04-04T15:00:00"); 
tab3[1] = new Date("2017-04-06T06:00:00"); 


$(function() { 

    "use strict"; 

    $(".gantt").gantt({ 
     source: [{ 
      name: "Sprint 0", 
      desc: "Analysis", 
      values: [{ 
       from: "/Date("+c.getTime()+")/", 
       to: "/Date("+d.getTime()+")/", 
       label: "Requirement Gathering", 
       customClass: "ganttRed" 
      },{ 
       from: "/Date("+a.getTime()+")/", 
       to: "/Date("+e.getTime()+")/", 
       label: "Scoping", 
       customClass: "ganttRed" 
      }] 
     },{ 
      desc : "Tasks", 
      values: tab1.map(function(tab, index) { 
       return { 
        from: "/Date("+tab2[index].getTime()+")/", 
        to: "/Date("+tab3[index].getTime()+")/", 
        label: tab1, 
        customClass: "ganttRed" 
       } 
      }) 
     }], 
     navigate: "scroll", 
     scale: "weeks", 
     maxScale: "months", 
     minScale: "hours", 
     itemsPerPage: 30, 
     useCookie: true, 
     onItemClick: function(data) { 
      alert("Item clicked - show some details"); 
     }, 
     onAddClick: function(dt, rowId) { 
      alert("Empty space clicked - add an item!"); 
     }, 
     onRender: function() { 
      if (window.console && typeof console.log === "function") { 
       console.log("chart rendered"); 
      } 
     } 
    }); 

    $(".gantt").popover({ 
     selector: ".bar", 
     title: "I'm a popover", 
     content: "And I'm the content of said popover.", 
     trigger: "hover", 
     placement: "auto right" 
    }); 

    prettyPrint(); 

}); 

謝謝你的幫助