创建自定义元素
要向 lightbox 添加自定义控件,你可以如下定义一个新对象:
gantt.form_blocks["my_editor"]={
render:function(sns){ //sns - 区块的配置对象
return "html code of the editor here";
},
set_value:function(node,value,task,section){
//node - 上述定义的 html 元素
//value - 由 map_to 属性定义的值
//task - 任务对象
//section- 区块的配置对象
... code to set value to the element ...
},
get_value:function(node,task,section){
//node - 上述定义的 html 元素
//task - 任务对象
//section - 区块的配置对象
return "current value from editor";
},
focus:function(node){
//node - 上述定义的 html 元素
...code to set focus to the element...
}
}
请注意,在 "render" 函数返回的 HTML 代码中不要使用自闭合标签,否则可能会在部分浏览器中导致解析问题:
//这是错误的写法
render:function(){
return "<div id='box'/>";
}
//正确做法,请使用成对的开始和结束标签:
render:function(){
return "<div id='box'></div>"; // 推荐
}
Custom control in the lightbox
lightbox 控件包含以下方法:
- render (sns): string - 返回包含该区块 HTML 元素的字符串
- sns - (LightboxSection) - 区块的配置对象
- set_value (node, value, task, section): any - 从 Task 对象获取值并应用到区块
- node - (HTMLElement) - 与区块 HTML 相关的元素
- value - (any) - 由 map_to 属性定义的值
- task - (Task) - 任务对象
- section - (LightboxSection) - 区块的配置对象
- get_value (node, task, section): any - 从区块获取值并保存回 Task 对象
- node - (HTMLElement) - 与区块 HTML 相关的元素
- task - (Task) - 任务对象
- section - (LightboxSection) - 区块的配置对象
- focus (node): void - 设置区块的焦点
- node - (HTMLElement) - 与区块 HTML 相关的元素
自定义含有两个输入框的编辑器
以下是创建一个含有两个输入框的自定义编辑器的示例:

gantt.form_blocks["my_editor"] = {
render: function (sns) {
return "<div className='dhx_cal_ltext' style='height:60px;'>"+
"Text <input className='editor_description' type='text'/>"+
"
Holders <input className='editor_holders' type='text'/>"+
"</div>";
},
set_value: function (node, value, task) {
node.querySelector(".editor_description").value = value || "";
node.querySelector(".editor_holders").value = task.users || "";
},
get_value: function (node, task) {
task.users = node.querySelector(".editor_holders").value;
return node.querySelector(".editor_description").value;
},
focus: function (node) {
var a = node.querySelector(".editor_description");
a.select();
a.focus();
}
};
gantt.config.lightbox.sections = [
{ name:"description", height:200, map_to:"text", type:"my_editor", focus:true},
{ name:"time", height:72, type:"duration", map_to:"auto"}
];
Custom control in the lightbox
自定义第三方编辑器
你也可以创建一个自定义多选控件,用于选择多个值。
例如,可以基于 jQuery Chosen 插件 的控件,用于为任务分配多个资源。与默认的 Gantt resource control 不同,这个控件仅用于分配资源,不涉及数量。当你只需简单功能时,这是一个直接的选择。

要在 Gantt 图中集成基于 jQuery Chosen 的控件:
- 在页面中引入所需的源文件
<script
src="https://code.jquery.com/jquery-3.3.1.min.js?v="5.2.4""
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossOrigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.js?v="5.2.4""></script>
<link rel="stylesheet" type="text/css"
href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css?v="5.2.4"">
- 定义控件逻辑
gantt.form_blocks["multiselect"] = {
render: function (sns) {
var height = (sns.height || "23") + "px";
var html = "<div className='gantt_cal_ltext gantt_cal_chosen gantt_cal_multiselect'"+
"style='height:"+ height + ";'><select data-placeholder='...'"+
"class='chosen-select' multiple>";
if (sns.options) {
for (var i = 0; i < sns.options.length; i++) {
if(sns.unassigned_value !== undefined && sns.options[i].key==sns.unassigned_value){
continue;
}
html+="<option value='" +sns.options[i].key+ "'>"+sns.options[i].label+"</option>";
}
}
html += "</select></div>";
return html;
},
set_value: function (node, value, ev, sns) {
node.style.overflow = "visible";
node.parentNode.style.overflow = "visible";
node.style.display = "inline-block";
var select = $(node.firstChild);
if (value) {
value = (value + "").split(",");
select.val(value);
}
else {
select.val([]);
}
select.chosen();
if(sns.onchange){
select.change(function(){
sns.onchange.call(this);
})
}
select.trigger('chosen:updated');
select.trigger("change");
},
get_value: function (node, ev) {
var value = $(node.firstChild).val();
//value = value ? value.join(",") : null
return value;
},
focus: function (node) {
$(node.firstChild).focus();
}
};