2017-04-13 98 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(); 

}); 

谢谢你的帮助