extjs - center a formPanel on a normal panel
I'm using extjs and just trying to solve a simple problem: I have a form with a fixed width. I want to Center this form within a normal Ext.Panel. I'm trying to achieve this the 'css-way' - setting the left and right margin to 'auto'.
But this doesn' work, it seems the margins are just ignored.
The Code:
var curStateForm = new Ext.FormPanel({
tit开发者_C百科le:'test',
width:300,
bodyStyle:'margin:5px auto 0 auto',
items: [edIst, edAdjust, edSumme]
});
var testPanel = new Ext.Panel({
width: '100%',
frame:true,
items: [curStateForm]
});
This works perfect for me in Ext 4
Use it on any container
layout: {
type: 'vbox',
align: 'center',
pack: 'center'
},
items: [{
title: 'Centered Panel',
width: 200,
height: 200
}]
style: {
marginLeft: 'auto',
marginRight: 'auto'
},
I just did this to a panel
listeners: {
afterlayout : function(container, layout, eOpts) {
panel.center();
}
}
Worked for me.
Have you seen this sample? Look at the example under "custom layouts / center".
EDIT: In Ext 3, that sample was the way to go. Now in Ext 4, it would be best to use a vbox layout on the outer container with center alignment as shown in A1rPun's answer.
In panel:
bodyStyle: "padding: 15px;" // may also be only padding-left/right
For instance, I use this layout:
Ext.layout.AbsoluteCenterLayout = Ext.extend(Ext.layout.ContainerLayout, {
monitorResize:true,
onLayout : function(ct, target){
Ext.layout.AbsoluteCenterLayout.superclass.onLayout.call(this, ct, target);
if(!this.container.collapsed){
var sz = (Ext.isIE6 && Ext.isStrict && target.dom == document.body) ? target.getViewSize() : target.getStyleSize();
this.setItemSize(this.activeItem || ct.items.itemAt(0), sz);
}
},
setItemSize : function(item, size){
if(item && size.height > 0){ // display none?
formEl = item.getEl();
var widthCenter = (size.width/2)-(formEl.getWidth()/2);
var heightCenter = (size.height/2)-(formEl.getHeight()/2);
formEl.setStyle({
left: widthCenter+'px',
top: heightCenter+'px',
position: 'absolute',
});
}
}
});
Ext.Container.LAYOUTS['absolutecenter'] = Ext.layout.AbsoluteCenterLayout;
My form:
Viewport = new Ext.Viewport({
renderTo: Ext.getBody(),
layout: 'border',
items: [{
region: 'center',
xtype: 'panel',
layout: 'absolutecenter',
baseCls: 'x-plain',
items:[{
id: 'form',
formId: 'admin-loginform',
xtype: 'form',
title: 'Authentication',
iconCls: 'admin-wnd-authentication',
frame: true,
autoHeight: true,
width: 270,
defaultType: 'textfield',
labelAlign: 'right',
...
Try the following in the panel:
style: {
"margin-left": "auto",
"margin-right": "auto"
},
精彩评论