使用每个以逗号分隔符显示数组数据

时间:2017-11-28 13:49:08

标签: jquery arrays ajax

我有这个代码并且可以正确运行静态数据:

$('#jqChart').jqChart({
    title: { text: 'Gantt Chart' },
    animation: { duration: 1 },
    legend: {
        visible: false
    },

    series: [
        {
            type: 'gantt',
            fillStyles: ["#418CF0", "#FCB441", "#E0400A", "#056492", "#BFBFBF"],
            data: [
               ['Phase 1', new Date(1394, 06, 01), new Date(1394, 09, 20), 'Task 1'],
               ['Phase 1', new Date(1394, 06, 01), new Date(1394, 09, 20), 'Task 1']
               //$(wfs).each(function (ix, wf) {
               //    ['Phase 2', new Date(1396, 06, 01), new Date(1396, 06, 01), 'Task 2'] +','
               //})
            ],
            labels: {
                fillStyle: 'white'
            }
        }
    ]
});

我希望每次传递我的数据,当我使用不带Comma的代码时,所需的输出将不会显示。

这是我的最终代码无法正常运行:

$('#jqChart').jqChart({
    title: { text: 'Gantt Chart' },
    animation: { duration: 1 },
    legend: {
        visible: false
    },

    series: [
        {
            type: 'gantt',
            fillStyles: ["#418CF0", "#FCB441", "#E0400A", "#056492", "#BFBFBF"],
            data: [
               //['Phase 1', new Date(1394, 06, 01), new Date(1394, 09, 20), 'Task 1'],
               //['Phase 1', new Date(1394, 06, 01), new Date(1394, 09, 20), 'Task 1']
               $(wfs).each(function (ix, wf) {
                   ['Phase 2', new Date(1396, 06, 01), new Date(1396, 06, 01), 'Task 2'] + ','
                   ['Phase 2', new Date(1396, 06, 01), new Date(1396, 06, 01), 'Task 2'] 
               })
            ],
            labels: {
                fillStyle: 'white'
            }
        }
    ]
});

2 个答案:

答案 0 :(得分:1)

您无法在数组构造函数中运行函数。在初始化插件之前创建数组并将数组引用传递给config

var chartData = [];

$.each(wfs,function(ix, wf) {
  chartData.push(['Phase from wf', date1FromWf, date2FromWf, 'Task from wf']);
});

$('#jqChart').jqChart({
  title: {
    text: 'Gantt Chart'
  },
  animation: {
    duration: 1
  },
  legend: {
    visible: false
  },

  series: [{
    type: 'gantt',
    fillStyles: ["#418CF0", "#FCB441", "#E0400A", "#056492", "#BFBFBF"],
    data: chartData,// array from above
    labels: {
      fillStyle: 'white'
    }
  }]
});

答案 1 :(得分:0)

鉴于" wfs是我们从response.d读取的数组"然后你可以按原样使用wfs,假设它包含适当的数据。

假设wfs看起来像

[
    ['Phase 2', new Date(1396, 06, 01), new Date(1396, 06, 01), 'Task 2'],
    ['Phase 2', new Date(1396, 06, 01), new Date(1396, 06, 01), 'Task 2']
]

然后你可以直接使用它:

$('#jqChart').jqChart({
    title: { text: 'Gantt Chart' },
    animation: { duration: 1 },
    legend: {
        visible: false
    },

    series: [
        {
            type: 'gantt',
            fillStyles: ["#418CF0", "#FCB441", "#E0400A", "#056492", "#BFBFBF"],
            data: wfs, // use wfs inline
            labels: {
                fillStyle: 'white'
            }
        }
    ]
});

我不认为jQuery.each()做你认为的事情。