| 類 | 用途 | 關鍵配置/方法 |
| DragSource | 可拖動元素 | group、afterDragDrop、onDragEnter、onDragOut |
| DDTarget | 放置目標區域 | group、notifyDrop、notifyOver、notifyOut |
| DDProxy | 代理模式拖動(顯示代理元素而非原元素) | proxyEl、autoOffset |
| DragZone/DropZone | 容器級拖放(Grid/Tree 內部節點拖放) | getDragData/onNodeDrop 等模板方法 |
進階:DragSource 完整事件鉤子
var dragSource = new Ext.dd.DragSource('element-id', {group: 'myGroup'});
dragSource.onDragStart = function() { console.log('開始拖動'); };
dragSource.onDragEnter = function(target, e) { target.el.addClass('drop-ok'); };
dragSource.onDragOut = function(target, e) { target.el.removeClass('drop-ok'); };
dragSource.onDragDrop = function(target, e) { console.log('放置到目標'); };
dragSource.onInvalidDrop = function(e) { console.log('無效放置'); };
dragSource.afterDragDrop = function(target, e, id) { console.log('放置完成'); };
DDTarget 完整事件鉤子
var dropTarget = new Ext.dd.DDTarget('target-id', 'myGroup');
dropTarget.notifyEnter = function(source, e, data) { this.el.highlight(); return true; };
dropTarget.notifyOver = function(source, e, data) { return this.dropAllowed; }; // 返回 CSS 類
dropTarget.notifyOut = function(source, e, data) { this.el.unhighlight(); };
dropTarget.notifyDrop = function(source, e, data) {
// 處理放置業務邏輯
return true; // 返回 true 表示放置成功
};
最佳實踐
- group 隔離:不同功能區域使用不同 group,避免誤觸發
- 視覺反饋:
notifyOver 返回 this.dropAllowed/this.dropNotAllowed 顯示綠/紅邊框
- 代理模式:複雜 DOM 結構拖動用
DDProxy,性能更好
- 容器級拖放:Tree/Grid 節點拖放用
DragZone/DropZone,實現模板方法
總結
ExtJS 拖放核心在於 DragDrop 子類、group 匹配、事件鉤子鏈。DragSource + DDTarget 實現基礎元素拖放;DragZone + DropZone 支撐複雜容器節點拖放。掌握事件生命週期,即可構建豐富交互體驗。
- 上一篇文章: Session 劫持攻擊原理與防禦深度解析
- 下一篇文章: Java 遞歸刪除目錄完整實戰代碼
Copyright ©
程式碼之家 All Rights Reserved