Integrate ZK with dhtmlxGantt"
Line 104: | Line 104: | ||
== Wrap dhtmlxgantt library with ZK Client Widget == | == Wrap dhtmlxgantt library with ZK Client Widget == | ||
+ | Before we load the real data, we will create a new widget that extends div widget and use bind_ life-cycle to register Gantt Event. And we add extra API such as "_init", "_attachGanttEvent" and "_detachGanttEvent" that are only used for Gantt Chart. | ||
+ | <source lang="javascript" high="2, 6, 12, 16, 19"> | ||
+ | zk.afterLoad('zul.wgt', function() { | ||
+ | zul.wgt.GanttDiv = zk.$extends(zul.wgt.Div, { | ||
+ | bind_: function() { | ||
+ | this.$supers('bind_', arguments); | ||
+ | // attach gantt event | ||
+ | this._attachGanttEvent(); | ||
+ | }, | ||
+ | unbind_: function() { | ||
+ | this._detachGanttEvent(); | ||
+ | this.$supers('unbind_', arguments); | ||
+ | }, | ||
+ | _init: function(task) { | ||
+ | gantt.init(this.uuid); //let dhmltxGantt library know where to render Gantt Chart | ||
+ | gantt.parse(task); //parse task and render Gantt Chart | ||
+ | } | ||
+ | _attachGanttEvent: function() { | ||
+ | // attach Gantt Event here | ||
+ | }, | ||
+ | _detachGanttEvent: function() { | ||
+ | // detach Gantt Event here | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | </source> | ||
+ | Line 2: Create GanttDiv widget that extends Div widget. | ||
+ | Line 6: Attach Gantt Event. | ||
+ | Line 12: "_init" API that will be used for initializing Gantt chart from server side. | ||
+ | Line 16, 19: Attach and detach Gantt Event for communication between client and server. | ||
== Loading data from server to dhtmlxGantt library == | == Loading data from server to dhtmlxGantt library == |
Revision as of 04:09, 3 October 2014
Vincent Jian, Engineer, Potix Corporation
October 3, 2014
ZK 7.0.3, dhtmlxGantt v.3.0.0 Standard
Introduction
dhtmlxGantt is an open source JavaScript Gantt chart that helps you visualize a project schedule in a nice-looking chart. It can show the dependencies between tasks as lines and allows you to set up different relationships between tasks (finish-to-start, start-to-start, end-to-end). This smalltalk will introduce how to integrate dhtmlxGantt into ZK web application.
Prerequisites
Since dhtmlxGantt is a javascript library, here I will use the technical described in the following articles to integrated with ZK:
- Integrate 3rd Party Javascript Libraries In ZK
- Integrate 3rd Party Javascript Libraries In ZK Using Clientside Controller
It is suggested to read the above two articles to understand the life cycle of ZK client widget.
Step to integrate dhtmlxGantt
Create ZK Project and Prepare Necessary Libraries
1. Create a ZK project by maven, refer to our installation guide.
2. Download dhtmlxGantt library from its official site, here I download the version 3.0 standard edition (GNU GPL v2 license).
- Extract the zip file and put dhtmlxgantt.js and dhtmlxgantt.css files under the project's "webapp/gantt" folder.
Load the library source and setup basic component
To load the dhtmlxgantt library, we can use script xml processing instruction like <?link?> and <?script?> on the zul page. From dhtmlxgantt online document, a simple div element is enough to initialize Gantt chart. Therefore, we add a div component on the zul page.
<?link rel="stylesheet" type="text/css" href="/gantt/dhtmlxgantt.css"?>
<?script type="text/javascript" src="/gantt/dhtmlxgantt.js"?>
<zk>
<window title="Dhtml Gantt Integration" border="normal" vflex="1">
<div id="gantt" />
</window>
</zk>
- Line 1,2: Load dhtmlxgantt library
- Line 5: Add div component to generate Gantt Chart.
Prepare data for Gantt Chart
Normally, a Gantt Chart need gantt tasks and gantt dependency links, and from dhtmlxGantt's online document, we can see all the properties for gantt task and gantt link. Threrfore, we need to create two Java Objects as follows:
public class GanttTask implements JSONAware {
private long id;
private Date startDate;
private int duration;
private String description;
private double progress;
private int sortOrder;
private GanttTask parent;
private boolean open;
public String toJSONString() {
DateFormat dft = new SimpleDateFormat("dd-MM-yyyy");
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("start_date", dft.format(startDate));
map.put("duration", duration);
map.put("text", description);
map.put("progress", progress);
map.put("sortorder", sortOrder);
map.put("parent", parent == null ? 0 : parent.getId());
map.put("open", open);
return JSONObject.toJSONString(map);
}
/* getter and setter omitted */
}
public class GanttLink implements JSONAware {
private long id;
private GanttTask source;
private GanttTask target;
private int type;
public String toJSONString() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("source", source.getId());
map.put("target", target.getId());
map.put("type", type);
return JSONObject.toJSONString(map);
}
/* getter and setter omitted */
}
As dhtmlxGantt support load JSON format objects, here we implement JSONAware.
Wrap dhtmlxgantt library with ZK Client Widget
Before we load the real data, we will create a new widget that extends div widget and use bind_ life-cycle to register Gantt Event. And we add extra API such as "_init", "_attachGanttEvent" and "_detachGanttEvent" that are only used for Gantt Chart.
zk.afterLoad('zul.wgt', function() {
zul.wgt.GanttDiv = zk.$extends(zul.wgt.Div, {
bind_: function() {
this.$supers('bind_', arguments);
// attach gantt event
this._attachGanttEvent();
},
unbind_: function() {
this._detachGanttEvent();
this.$supers('unbind_', arguments);
},
_init: function(task) {
gantt.init(this.uuid); //let dhmltxGantt library know where to render Gantt Chart
gantt.parse(task); //parse task and render Gantt Chart
}
_attachGanttEvent: function() {
// attach Gantt Event here
},
_detachGanttEvent: function() {
// detach Gantt Event here
}
}
});
Line 2: Create GanttDiv widget that extends Div widget. Line 6: Attach Gantt Event. Line 12: "_init" API that will be used for initializing Gantt chart from server side. Line 16, 19: Attach and detach Gantt Event for communication between client and server.
Loading data from server to dhtmlxGantt library
Send dhtmlxGantt event back to server
Conclusion
Download
Comments
Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License. |