2015-10-07 53 views
1

試圖讓dhtmlx甘特圖加載json數據加載頁面。dhtmlx甘特使用jquery動態加載數據

data_file.php(SNIP)

$colors = array(1=>"red", 2=>"green", 3=>"blue", 4=>"yellow", 5=>"orange", 6=>"grey"); 
$aryData = array(); 
if($project_tasks) { 
    foreach($project_tasks as $aryTask) { 
     $aryData[] = array(
      "id" => $aryTask["pt_id"], 
      "text" => $aryTask["pt_name"], 
      "start_date" => date("d-m-Y", strtotime($aryTask["pt_start_date"])), 
      "duration" => round((strtotime($aryTask["pt_end_date"]) - strtotime($aryTask["pt_start_date"]))/(60*60)), 
      "open" => true, 
      "color" => $colors[rand(1,6)] 
     ); 
    } 
} 

$strData = json_encode($aryData, JSON_UNESCAPED_SLASHES); 
header("Content-type:application/json;"); 
echo "\"data\" : ".$strData; 

提供了這樣的事情(從鉻控制檯):

"data" : [{"id":"152","text":"test3","start_date":"01-09-2015","duration":600,"open":true,"color":"yellow"},{"id":"153","text":"test1","start_date":"23-09-2015","duration":72,"open":true,"color":"grey"},{"id":"154","text":"test2","start_date":"15-09-2015","duration":264,"open":true,"color":"red"}] 

JS-file.js(中略)

var data_url = basePath + "system/data_file.php?project_id=" + projectID; 
$(".gantt").dhx_gantt({ 
scale_unit:"week", 
step:1, 
date_scale:"%W" 
}); 
// var tasks = $(".gantt").dhx_gantt().load(data_url); 
$(".gantt").dhx_gantt().load(data_url); 
gantt.parse(tasks); 

我嘗試「提醒(任務)」;「結果爲「未定義」或爲空。

我已經試過數據syncronous AJAX負荷:

var tasks = null; 

$.ajax({ 
    url: data_url, 
    async: false, 
    dataType: 'json', 
    success: function(data) { 
     tasks = data; 
    } 
}); 

,這給了相同的結果 - >空。

所以我明白了爲什麼甘特不會加載數據 - 任務變量中沒有數據。

有沒有人有一個運行的例子?

親切的問候

拉爾斯

回答

0

洙......我的代碼發揮各地..

而這就是我想出了這麼遠。看來這是必須是在一個特定的方式JSON ..

JS:

/* Gantt Start */ 

     var data_url = basePath + "system/print_gantt_project_data.php?project_id=" + projectID; 

     gantt.config.readonly = true; 
     gantt.config.scale_unit = "month"; 
     gantt.config.step = 1; 
     gantt.config.date_scale = "%M"; 

     gantt.init("gantt_div"); 
     gantt.load(data_url, "json"); 
     gantt.render(); 
/* Gantt End */ 

唯一的問題是這種方法 - 再次到目前爲止 - 是該dhtmlx_gantt起初不渲染任務。似乎需要一種更新。 (我試過什麼是可用的API(http://docs.dhtmlx.com/gantt/api__gantt_render.html

仍試圖獲得一個固定..

哦中,和PHP文件創建JSON數據:

/開始/

/* Create JSON array and print it for /js/system_project.js */ 
$colors = array(1=>"red", 2=>"green", 3=>"blue", 4=>"yellow", 5=>"orange", 6=>"grey"); 
$aryData = array(); 
if($project_tasks) { 
    foreach($project_tasks as $aryTask) { 
     $aryData[] = array(
      "id" => $aryTask["pt_id"], 
      "text" => $aryTask["pt_name"], 
      "start_date" => date("d-m-Y", strtotime($aryTask["pt_start_date"])), 
      "duration" => round((strtotime($aryTask["pt_end_date"]) - strtotime($aryTask["pt_start_date"]))/(60*60)), 
      "open" => "true", 
      "color" => $colors[rand(1,6)], 
      "parent" => $aryTask["pt_parent"] 
     ); 
    } 
} 

$strData = json_encode($aryData, JSON_UNESCAPED_SLASHES); 
header("Content-type:application/json;"); 
echo "{\"data\":".$strData."}"; 

上做的東西工作正確的任何信息,請不要猶豫,進入;-)

/Lars