翻譯|使用教程|編輯:楊鵬連|2021-03-30 13:14:00.447|閱讀 518 次
概述:內(nèi)聯(lián)編輯使您可以直接在網(wǎng)格中進(jìn)行任何更改:創(chuàng)建和更新任務(wù),設(shè)置任務(wù)之間的連接,定義開始和結(jié)束日期或修改持續(xù)時間-所有這些都通過內(nèi)置編輯器進(jìn)行。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
dhtmlxGantt是用于跨瀏覽器和跨平臺應(yīng)用程序的功能齊全的Gantt圖表??蓾M足項目管理應(yīng)用程序的所有需求,是最完善的甘特圖圖表庫。它允許你創(chuàng)建動態(tài)甘特圖,并以一個方便的圖形化方式可視化項目進(jìn)度。有了dhtmlxGantt,你可以顯示活動之間的依賴關(guān)系,顯示具有完成百分比陰影的當(dāng)前任務(wù)狀態(tài)以及組織活動到樹結(jié)構(gòu)。
dhtmlxGantt提供了兩個用于編輯內(nèi)容的選項:
要啟用內(nèi)聯(lián)編輯,您需要:
var textEditor = {type: "text", map_to: "text"}; var dateEditor = {type: "date", map_to: "start_date", min: new Date(2018, 0, 1), max: new Date(2019, 0, 1)}; var durationEditor = {type: "number", map_to: "duration", min:0, max: 100};
gantt.config.columns = [ {name: "text", tree: true, width: '*', resize: true, editor: textEditor}, {name: "start_date", align: "center", resize: true, editor: dateEditor}, {name: "duration", align: "center", editor: durationEditor}, {name: "add", width: 44} ];您可以看一下視頻指南,該指南顯示了如何在網(wǎng)格中實現(xiàn)內(nèi)聯(lián)編輯。
編輯類型
內(nèi)聯(lián)編輯器存儲在editor_types配置對象中。
有幾種預(yù)定義的內(nèi)聯(lián)編輯器:
var editors = { text: {type: "text", map_to: "text"}, start_date: {type: "date", map_to: "start_date", min: new Date(2018, 0, 1), max: new Date(2019, 0, 1)}, end_date: {type: "date", map_to: "end_date", min: new Date(2018, 0, 1), max: new Date(2019, 0, 1)}, duration: {type: "number", map_to: "duration", min:0, max: 100}, priority: {type:"select", map_to:"priority", options:gantt.serverList("priority")}, predecessors: {type: "predecessor", map_to: "auto"} };日期編輯器中的日期限制
從v6.3開始,日期內(nèi)聯(lián)編輯器的最小和最大輸入值沒有默認(rèn)限制。
如果希望時間刻度上顯示的日期限制日期內(nèi)聯(lián)編輯器的最小值和最大值(除非提供了自定義的最小值/最大值),則可以指定動態(tài)最小值/最大值:
const dateEditor = {type: "date", map_to: "start_date", min: function(taskId){ return gantt.getState().min_date }, max: function( taskId ){ return gantt.getState().max_date } };結(jié)束日期的編輯器
如果將格式用于任務(wù)的包含結(jié)束日期,并希望通過網(wǎng)格中的內(nèi)聯(lián)編輯使其正確運(yùn)行,則必須創(chuàng)建一個特殊的編輯器來編輯任務(wù)的包含結(jié)束日期,如下所示:
// inclusive editor for end dates // use the default editor, but override the set_value/get_value methods var dateEditor = gantt.config.editor_types.date; gantt.config.editor_types.end_date = gantt.mixin({ set_value: function(value, id, column, node){ var correctedValue = gantt.date.add(value, -1, "day"); return dateEditor.set_value.apply(this, [correctedValue, id, column, node]); }, get_value: function(id, column, node) { var selectedValue = dateEditor.get_value.apply(this, [id, column, node]); return gantt.date.add(selectedValue, 1, "day"); }, }, dateEditor); var textEditor = {type: "text", map_to: "text"}; var startDateEditor = {type: "date", map_to: "start_date"}; var endDateEditor = {type: "end_date", map_to: "end_date"}; var durationEditor = {type: "number", map_to: "duration", min:0, max: 100}; gantt.config.columns = [ {name: "text", label: "Name", tree: true, width: 200, editor: textEditor, resize: true}, {name: "duration", label: "Duration", width:80, align: "center", editor: durationEditor, resize: true}, {name: "start_date", label: "Start", width:140, align: "center", editor: startDateEditor, resize: true}, {name: "end_date", label: "Finish", width:140, align: "center", editor: endDateEditor, resize: true} ]; // change lightbox and grid templates to display dates of tasks in an inclusive format gantt.templates.task_end_date = function(date){ return gantt.templates.task_date(new Date(date.valueOf() - 1)); }; var gridDateToStr = gantt.date.date_to_str("%Y-%m-%d"); gantt.templates.grid_date_format = function(date, column){ if(column === "end_date"){ return gridDateToStr(new Date(date.valueOf() - 1)); }else{ return gridDateToStr(date); } }格式化前置編輯器的值
從v6.3開始,Gantt允許直接從內(nèi)聯(lián)編輯器中指定鏈接的類型以及滯后/超前值。
為此,您需要使用鏈接格式化程序模塊,并將LinksFormatter的一個實例提供到先前的編輯器中:
var formatter = gantt.ext.formatters.durationFormatter({ enter: "day", store: "day", format: "auto" }); var linksFormatter = gantt.ext.formatters.linkFormatter({durationFormatter: formatter}); var editors = { text: {type: "text", map_to: "text"}, start_date: {type: "date", map_to: "start_date", min: new Date(2018, 0, 1), max: new Date(2019, 0, 1)}, end_date: {type: "date", map_to: "end_date", min: new Date(2018, 0, 1), max: new Date(2019, 0, 1)}, duration: {type: "duration", map_to: "duration", min:0, max: 100, formatter: formatter}, priority: {type: "select", map_to: "priority", options:gantt.serverList("priority")}, predecessors: {type: "predecessor", map_to: "auto", formatter: linksFormatter} }; gantt.config.columns = [ {name: "wbs", label: "#", width: 60, align: "center", template: gantt.getWBSCode}, {name: "text", label: "Name", tree: true, width: 200, editor: editors.text, resize: true}, {name: "start_date", label: "Start", width:80, align: "center", editor: editors.start_date, resize: true}, {name: "predecessors", label: "Predecessors",width:80, align: "left", editor: editors.predecessors, resize: true, template: function(task){ var links = task.$target; var labels = []; for(var i = 0; i < links.length; i++){ var link = gantt.getLink(links[i]); labels.push(linksFormatter.format(link)); } return labels.join(", ") }}, {name:"add"} ];自定義內(nèi)聯(lián)編輯器
您還可以指定自定義內(nèi)聯(lián)編輯器。為此,您需要通過以下方式創(chuàng)建一個新的編輯器對象:
gantt.config.editor_types.custom_editor = { show: function (id, column, config, placeholder) { // called when input is displayed, put html markup of the editor into placeholder // and initialize your editor if needed: var html = "<div><input type='text' name='" + column.name + "'></div>"; placeholder.innerHTML = html; }, hide: function () { // called when input is hidden // destroy any complex editors or detach event listeners from here }, set_value: function (value, id, column, node) { // set input value }, get_value: function (id, column, node) { // return input value }, is_changed: function (value, id, column, node) { //called before save/close. Return true if new value differs from the original one //returning true will trigger saving changes, returning false will skip saving }, is_valid: function (value, id, column, node) { // validate, changes will be discarded if the method returns false return true/false; }, save: function (id, column, node) { // only for inputs with map_to:auto. complex save behavior goes here }, focus: function (node) { } }為了實現(xiàn)可重用的編輯器,需要記住一些關(guān)鍵點:
var getInput = function(node){ return node.querySelector("input"); }; gantt.config.editor_types.simpleNumber = { show: function (id, column, config, placeholder) { var min = config.min || 0, max = config.max || 100; var html = "<div><input type='number' min='" + min + "' max='" + max + "' name='" + column.name + "'></div>"; placeholder.innerHTML = html; }, hide: function () { // can be empty since we don't have anything to clean up after the editor // is detached }, set_value: function (value, id, column, node) { getInput(node).value = value; }, get_value: function (id, column, node) { return getInput(node).value || 0; }, is_changed: function (value, id, column, node) { var currentValue = this.get_value(id, column, node); return Number(value) !== Number(currentValue); }, is_valid: function (value, id, column, node) { return !isNaN(parseInt(value, 10)); }, focus: function (node) { var input = getInput(node); if (!input) { return; } if (input.focus) { input.focus(); } if (input.select) { input.select(); } } };之后,您可以使用與內(nèi)置編輯器相同的方式來使用編輯器:
var numberEditor = {type: "simpleNumber", map_to: "quantity", min:0, max: 50}; gantt.config.columns = [ ... {name: "quantity", label: "Quantity", width: 80, editor: numberEditor, resize: true}, ... ];請注意,hide在這種情況下,我們不需要實現(xiàn)該方法,因為甘特圖會自動分離編輯器的DOM元素,并且在關(guān)閉編輯器后無需清理其他內(nèi)容。
編輯器
hide如果您在內(nèi)聯(lián)編輯器中使用復(fù)雜的小部件,則可能需要添加邏輯。
例如,讓我們考慮以下使用jQuery的DatePicker輸入的實現(xiàn)。在這種情況下,我們需要在將日期選擇器小部件與DOM分離后銷毀它。
先決條件:
<link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.12.4.js"></script> <script src="http://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>編輯:
gantt.config.editor_types.custom_datepicker_editor = { show: function (id, column, config, placeholder) { placeholder.innerHTML = "<div><input type='text' id='datepicker' name='" + column.name + "'></div>"; $("#datepicker").datepicker({ dateFormat: "yy-mm-dd", onSelect: function(dateStr){ gantt.ext.inlineEditors.save() } }); }, hide: function (node) { $("#datepicker").datepicker( "destroy" ); }, set_value: function (value, id, column, node) { $("#datepicker").datepicker("setDate", value); }, get_value: function (id, column, node) { return $("#datepicker").datepicker( "getDate" ); }, is_changed: function (value, id, column, node) { return (+$("#datepicker").datepicker( "getDate" ) !== +value); }, is_valid: function (value, id, column, node) { return !(isNaN(+$("#datepicker").datepicker( "getDate" ))) }, save: function (id, column, node) { }, focus: function (node) { } }; let dateEditor = { type: "custom_datepicker_editor", map_to: "start_date" }; gantt.config.columns = [ {name: "text", tree: true, width: '*', resize: true}, {name: "start_date", align: "center", resize: true, editor: dateEditor}, {name: "duration", align: "center"}, {name: "add", width: 44} ];editor.save
save僅當(dāng)您的編輯器需要一次修改任務(wù)的多個屬性,或者希望它修改不同于任務(wù)的對象時,才需要使用該功能。
在這種情況下,您可以保留適當(dāng)?shù)膶崿F(xiàn)以get_value進(jìn)行內(nèi)置驗證,但是gantt本身不會嘗試將編輯器的值應(yīng)用于任務(wù),save而是將調(diào)用該函數(shù)。
之后save被調(diào)用時,你需要解釋的輸入值,并應(yīng)用變?yōu)閹в凶远x代碼甘特。方法完成后,Gantt將調(diào)用onSave事件save,但不會為修改后的行調(diào)用gantt.updateTask。
筆記!save僅當(dāng)您map_to:"auto"在編輯器的配置中指定時,才會調(diào)用該方法:
var editors = { ... predecessors: {type: "predecessor", map_to: "auto"} };內(nèi)置的前身編輯器就是此類控件的一個很好的例子。您可以在相關(guān)示例中找到其簡化的實現(xiàn)。
關(guān)產(chǎn)品推薦:
VARCHART XGantt:支持ActiveX、.Net等平臺的C#甘特圖控件
AnyGantt:構(gòu)建復(fù)雜且內(nèi)容豐富的甘特圖的理想工具
jQuery Gantt Package:基于HTML5 / jQuery的跨平臺jQuery Gantt包
phGantt Time Package:對任務(wù)和時間的分配管理的甘特圖
APS幫助提升企業(yè)生產(chǎn)效率,真正實現(xiàn)生產(chǎn)排程可視化呈現(xiàn)與控制,快速有效響應(yīng)不同場景的生產(chǎn)計劃,提高準(zhǔn)時交貨能力,提高產(chǎn)能和資源利用率
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: