gantt javascript循环问题

时间:2017-04-13 14:38:36

标签: javascript gantt-chart

我想做一个循环,以便将所有数据放入我的图表中。但是,我不知道该怎么做。我尝试了一些东西,但它不起作用。我在循环线上得到Uncaught SyntaxError

$(".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"
      }]
    }
  }]
});

2 个答案:

答案 0 :(得分: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"
            }
        });
    ]
 }

答案 1 :(得分: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();

});

感谢您的帮助