2017-06-19 250 views
0

我在我的页面中有一个dhtmlx甘特图,通常它会有完美的工作,但现在当我有一个嵌套数组的JSON文件时,我的输出将是unrecognized。我正试图填充official namedhtmlxgantt填充嵌套的JSON数据

任何人都可以帮助我吗?非常感谢你。

JSON

{ 
    "data": [ 
     { 
      "assign_to": { 
       "id": 3, 
       "employee_id": "28141", 
       "official_name": "Hal Jordan", 
      }, 
      "task": { 
       "id": 1, 
       "name": "Modeling", 
       "description": "3d modeling work" 
      }, 
      "start_date": "2017-06-15", 
      "end_date": "2017-06-19" 
     }, 
     { 
      "assign_to": { 
       "id": 3, 
       "employee_id": "28144", 
       "official_name": "Kyle Rayner", 
      }, 
      "task": { 
       "id": 8, 
       "name": "Composting", 
       "description": null 
      }, 
      "start_date": "2017-06-01", 
      "end_date": "2017-06-08" 
     } 
    ] 
} 

的Javascript

gantt.config.columns = [ 
     {name: "assign_to.official_name", label: "Assign To", align: "left", width: 70}, 
    ]; 

function initializeGantt() { 
    gantt.init("my_scheduler"); 
    gantt.load("/dashboard/ganttchart_list/5/?format=json"); 
} 

initializeGantt(); 

HTML

<div id="my_scheduler" style='width:1405px; height:245px;'></div> 

回答

2

DHTMLX甘特不支持嵌套的结构,所以你它们加载之前需要进行预处理您的项目甘特。 由于您知道哪种格式以及甘特图(https://docs.dhtmlx.com/gantt/desktop__supported_data_formats.html#json)预期使用哪种格式,因此它不应该成为问题。

最重要的事情

  1. 确保数据项有 '身份证' 属性
  2. 当你定义列名这样的:

    {名: “assign_to.official_name”,标签: “Assign To”,align:“left”,width:70},

    列名无法映射到嵌套对象的属性,所以您要么必须在加载期间将结构展平,它可以在f中完成ollowing方式: http://snippet.dhtmlx.com/129d2e043

或定义这将需要价值从嵌套对象中列的模板: http://snippet.dhtmlx.com/9e8e65e85

  • 请检查结果数据集中的控制台输出
+0

你好@Alex Klimenkov 非常感谢你,这是完美的!但对于我的数据,它实际上来自REST API,所以如果我想在'getData()'函数中调用,我该如何调用它?例如'http:// 127.0.0.1/test/ganttchart_data/5 /' 再次我可以感谢你足够Alex –

+0

嗨!你可以通过ajax get手动请求你的api,一旦你从服务器获取数据 - 你就可以将它转换成甘特图。 如果您使用上面示例中的jQuery和函数,它可能看起来如下: '$ .get(“/ test/ganttchart_data/5 /”,function(result){ gantt.parse(prepareData(result)); }); ' https://api.jquery.com/jquery.get/ –