原創|使用教程|編輯:秦林|2022-10-18 09:38:18.653|閱讀 329 次
概述:這篇文章給大家帶來dhtmlxGantt如何自定義內聯編輯器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
這篇文章給大家帶來dhtmlxGantt如何自定義內聯編輯器。
您可以指定自定義內聯編輯器。為此,您需要通過以下方式創建一個新的編輯器對象:
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) { } }
為了實現可重用的編輯器,需要記住一些關鍵點:
如果您正在實現一個編輯器,它比將值寫入任務的屬性更復雜 - 一個很好的例子是內置的前置編輯器-您需要在save函數中實現所需的邏輯并指定map_to輸入“自動”。在這種情況下,甘特圖不會修改任務對象,而是會save在需要將所做的更改應用到編輯器時調用該函數。
下面是一個簡單數字輸入的實現示例。請注意,該hide方法可以是一個空函數,并且該save方法可以完全跳過。
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(); } } };
之后,您可以像使用內置編輯器一樣使用編輯器:
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方法,因為甘特圖會自動分離編輯器的 DOM 元素,并且在編輯器關閉后我們不需要清理任何其他內容。
編輯器.hide
hide如果您在內聯編輯器中使用復雜的小部件,您可能需要添加邏輯。
例如,讓我們考慮以下使用 jQuery 實現的 DatePicker 輸入。在這種情況下,我們需要在它與 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} ];
編輯器.保存
save只有當您的編輯器需要一次修改任務的多個屬性,或者您希望它修改與任務不同的對象時,您才需要使用該功能。
在這種情況下,您可以get_value為內置驗證保留正確的實現,但甘特圖本身不會嘗試將編輯器的值應用于任務,save而是調用該函數。
調用后save,您需要解釋輸入值并使用自定義代碼將更改應用于甘特圖。Gantt 將在方法完成后調用onSave事件save,但不會為修改的行調用gantt.updateTask。
筆記!僅當您在編輯器的配置中save指定時才會調用該方法:map_to:"auto"
var editors = { ... predecessors: {type: "predecessor", map_to: "auto"} };
dhtmlxGantt是用于跨瀏覽器和跨平臺應用程序的功能齊全的Gantt圖表,可滿足項目管理控件應用程序的所有需求,是最完善的甘特圖圖表庫。了解更多DhtmlxGantt相關內容和資訊,歡迎在線咨詢或者私信我獲取正版試用版及報價。
甘特圖控件交流群:764148812 歡迎進群交流討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn