Pars data from apex class to apex page

Hi everyone, I have a question about using dhtmlxGantt. Is there a way that we can parse data as a variable to apex page?

For now, what I have here is:
<apex:stylesheet value="{!$Resource.dhtmlxGanttCSS}"/>
<apex:includeScript value="{!$Resource.dhtmlxGanttJS}"/>

gantt.init("gantt_here");

I know I can do sth like:
var tasks =
{data:[{id:1, text:“Project”,start_date:“01-04-2015”, duration:28},…}]}
then use gantt.parse(task) to display the chat

But I don’t want to do it by hardcode.
Say in my controller class: I have a query ‘Projects[]’ which store all the informations need.
How can I pas this to the apex page?

I already tried for loop{var task = {data=project[i]}} something like that, but didn’t work…

Please Give me some ideas ~~~~~
btw, the only way to get my data is have a query to salesforce, so can’t load it from other place
Project[] = select * from IT_Project__c where balabla

Thanks :smiley:

Hi,
the data is usually loaded via ajax. So you can create a handler which will get your data from the datastore and write them to response as an JSON string

docs.dhtmlx.com/gantt/desktop__s … .html#json
docs.dhtmlx.com/gantt/api__gantt_load.html

So you’ll have gantt.load(url) on the page, and the url should return json like following
docs.dhtmlx.com/gantt/samples/co … nector.php

so there is no way to pass a query directly ?!~

Hi,
you can execute a query on the page and then render a javascript code that assigns the retrieved dataset to the variable that is later passed to gantt.parse

Probably you can serialize the selected data to JSON developer.salesforce.com/page/G … _Apex_JSON
which can be rendered to the page as a javascript object declaration. However, I’m not familiar with a framework and can’t give you a ready example

Got a solution: using JSON

JSONGenerator generator = JSON.createGenerator(true);
generator.writeStartObject();
generator.writeFieldName(‘data’);
generator.writeStartArray(); //Write the starting marker of a JSON object ‘[’

	for(Task__c t : projectList)
	{   
		generator.writeStartObject(); //Write the starting marker of a JSON object '{'
		generator.writeStringField('id', string.valueOf(counter));
		generator.writeStringField('text', t.Name);
		generator.writeDateField('start_date',t._Start_Date__c);
		generator.writeNumberField('duration',10);
		generator.writeEndObject(); //Write the end marker of a JSON object '}'
	}
	generator.writeEndArray(); //Write the end marker of a JSON object ']'
	generator.writeEndObject();
	jsonString = generator.getAsString();

Then, in the apex page, call {!jsonString}
var task={!jsonString};
function displayChart()
{
alert(task);
gantt.init(“gantt_here”);
gantt.parse(task,“json”);

}

Thanks for the help !~~^_^

Can you please provide the full working code?
I’m trying to implement the code you generously provided, but having trouble adding the missing sections.

thanks

Hi vicki_dlam, can you please send me your complete solution? I have now the some problem!
thank you