2011-04-25 39 views
2

我有一个Ext JS面板容器。每次点击一个按钮时,面板容器内都会创建一个子面板。我可以拖放并调整容器中任何位置的子面板的大小。获取Ext JS子面板的尺寸和位置

我的问题是获取所有子面板的宽度,高度和位置,并在点击“保存”按钮时将它们传递给服务器端。如何做到这一点?任何帮助,将不胜感激。

这是我到目前为止创建:

Ext.override(Ext.Panel, { 

    // private 
    initEvents: function() { 
    if (this.draggable) { 
     this.initDraggable(); 
    } 
    this.resizer = new Ext.Resizable(this.el, { 
     animate: true, 
     duration: '.6', 
     easing: 'backIn', 
     handles: 'all', // handles:'s' gives vertical resizing only 
     // minHeight: this.minHeight || 100, 
     // minWidth:this.minWidth || 100, 
     pinned: false, 
     transparent:true 
    }); 
    this.resizer.on('resize', this.onResizer, this); 
    }, 

    onResizer: function (oResizable, iWidth, iHeight, e) { 
    this.setHeight(iHeight); 
    this.setWidth(iWidth); 
    //alert(iHeight); 
    } 

}); 

var p = new Ext.Panel({ 
    border: false, 
    layout: 'absolute', 
    autoScroll: true, 
    margins: '0 0 5 0', 
    ref: 'containerPanel', 
    resizable: true, 
    title: 'Container Panel', 

    buttons: [{ 
    text: 'Add Panel', 
    handler: function() { 
     var childPanelCount = w.containerPanel.items.length; 
     var startYPosition = 10; 
     startYPosition = startYPosition * childPanelCount; 
     var childPanel = new Ext.Panel({ 
     draggable: dragMeToHeckAndBack, 
     layout: 'fit', 
     height: 100, 
     title: 'New Panel ' + (childPanelCount + 1), 
     resizable: true, 
     width: 200, 
     x: 10, 
     y: startYPosition, 
     tools: tools 
     }); 
     w.containerPanel.add(childPanel); 
     w.containerPanel.doLayout(); 
    } 
    }, { 
    text: 'save', 
    handler: function() {} 
    }] 
}); 

var dragMeToHeckAndBack = { 
    insertProxy: false, 
    onDrag: function (e) { 
    var pel = this.proxy.getEl(); 
    this.x = pel.getLeft(true); 
    this.y = pel.getTop(true); 
    var s = this.panel.getEl().shadow; 
    if (s) { 
     s.realign(this.x, this.y, pel.getWidth(), pel.getHeight()); 
    } 
    }, 
    endDrag: function (e) { 
    this.panel.el.setX(this.x); 
    this.panel.el.setY(this.y); 
    } 
}; 

w = new Ext.Window({ 
    height: 600, 
    autoScroll:true, 
    layout: 'fit', 
    resizable: false, 
    width: 800, 
    items: [p] 
}); 

w.show(); 

回答

4

Ext.Panel收益大小和位置的getBox方法:

var box = my_panel.getBox(); 

alert(
    'Box dimensions: ' 
    +' width='+ box.width 
    +' height='+ box.height 
    +' x='+ box.x 
    +' y='+ box.y 
); 
+0

如何能在我的代码被应用。我将拖动/调整一些子面板的大小,之后当我单击保存按钮时,我需要获取当前所有子面板的大小和位置。 – prajeesh 2011-04-26 05:08:44

+0

您可以通过迭代父面板的“项目”属性来查看任何面板的子项。如果您将父项存储在“w.containerPanel”中,则可以将函数传递给w.containerPanel.items.each,并在传递给它的每个子面板上调用getBox。 – owlness 2011-04-26 13:11:55

+0

现在当我点击按钮子面板添加一个在容器面板下面的一个。每次我点击按钮时,我都希望将子面板显示在预定位置,例如x:20,y:20。如何做到这一点? – prajeesh 2011-04-27 06:46:26