extjs - center a formPanel on a normal panel

二次信任 提交于 2020-01-24 11:53:48

问题


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({
    title:'test',
    width:300,
    bodyStyle:'margin:5px auto 0 auto',
    items: [edIst, edAdjust, edSumme]
});   


var testPanel = new Ext.Panel({
    width: '100%',
    frame:true,
    items: [curStateForm]
});  

回答1:


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.




回答2:


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
    }]



回答3:


    style: {
        marginLeft: 'auto',
        marginRight: 'auto'
    },



回答4:


I just did this to a panel

listeners: {
    afterlayout : function(container, layout, eOpts) {
    panel.center();
    }
}

Worked for me.




回答5:


In panel:

bodyStyle: "padding: 15px;"  // may also be only padding-left/right



回答6:


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',
...



回答7:


Try the following in the panel:

style: {
    "margin-left": "auto",
            "margin-right": "auto"
       }, 


来源:https://stackoverflow.com/questions/2880920/extjs-center-a-formpanel-on-a-normal-panel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!