如何在Ext JS 4中将按钮置于其容器中心?

问题描述 投票:0回答:2

我正在使用Ext JS 4构建一个网站,我需要将一个按钮集中到它的容器,但我似乎无法实现它。

我在互联网上搜索了如何做到这一点,我发现一些答案说使用layout但它在我的情况下不起作用:

const submit = Ext.create('Ext.Button', {
    text     : 'search',
    scale: 'large',
    renderTo : Ext.getBody(),
    listeners: {
    click: function() {

        }
    },
    layout: {
        align: 'middle',
        pack: 'center',
        type: 'hbox'
    },
});

我的所有代码:

function init(results, checkInDate, checkOutDate, array) {
    if(results!="") {
        results = JSON.parse(results);
    }
    const todaysDate = new Date();
    var visitDay = todaysDate.getDate();
    var visitMonth = todaysDate.getMonth()+1;
    var visitYear = todaysDate.getFullYear();
    var leavingDay = todaysDate.getDate();
    var leavingMonth = todaysDate.getMonth()+1;
    var leavingYear = todaysDate.getFullYear();
    var array1 = new Array();
    var i2 = 0;
    var i3 = 1;
    for(var i = 0; i<array.length; i++) {
        array1.push({ 'name': array[i],  'price1': results[i2],  'price2': results[i3]})
        i2 = i2 + 2;
        i3 = i3 + 2;
    }

    Ext.application({
        name: 'MyApp',
        launch: function() {
            const datePickers = Ext.create('Ext.form.Panel', {
                renderTo: Ext.getBody(),
                width: 300,
                bodyPadding: 10,
                items: [{
                    xtype: 'datefield',
                    format: 'd/m/Y',
                    anchor: '100%',
                    fieldLabel: 'Check In:',
                    name: 'from_date',
                    value: checkInDate,
                    listeners: {
                        'change': function(me) {
                            const visitDate = me.getSubmitValue();
                            visitMonth = '';
                            visitYear = '';
                            visitDay = '';
                            for(var i=0; i<2; i++) {
                                visitDay = visitDay + visitDate.charAt(i);
                            }
                            for(var i=3; i<5; i++) {
                                visitMonth = visitMonth + visitDate.charAt(i)
                            }
                            for(var i=6; i<10; i++) {
                                visitYear = visitYear + visitDate.charAt(i);
                            }
                        }
                    }
                }, {
                    xtype: 'datefield',
                    format: 'd/m/Y',
                    anchor: '100%',
                    fieldLabel: 'Check Out:',
                    name: 'to_date',
                    value: checkOutDate,
                    listeners: {
                        'change': function(me) {
                            const leavingDate = me.getSubmitValue();
                            leavingMonth = '';
                            leavingYear = '';
                            leavingDay = '';
                            for(var i=0; i<2; i++) {
                                leavingDay = leavingDay + leavingDate.charAt(i);
                            }
                            for(var i=3; i<5; i++) {
                                leavingMonth = leavingMonth + leavingDate.charAt(i)
                            }
                            for(var i=6; i<10; i++) {
                                leavingYear = leavingYear + leavingDate.charAt(i);
                            }
                        }
                    }
                }]
            });

            //This is the item I want to center:
            const submit = Ext.create('Ext.Button', {
                text     : 'Search',
                scale: 'large',
                renderTo : Ext.getBody(),
                listeners: {
                    click: function() {

                    }
                }
            });

            var store = Ext.create('Ext.data.Store', {
                storeId:'prices',
                fields:['name', 'price1', 'price2'],
                proxy: {
                    type: 'memory',
                    reader: {
                        type: 'json',
                        root: 'items'
                    }
                }
            });

            const grid = Ext.create('Ext.grid.Panel', {
                store: Ext.data.StoreManager.lookup('prices'),
                columns: [
                    { text: 'Name',  dataIndex: 'name', width: 200},
                    { text: 'Price1', dataIndex: 'price1', width: 135},
                    { text: 'Price2', dataIndex: 'price2', width: 135},
                ],
                renderTo: Ext.getBody()
            });

            store.getProxy().data = array1;
            store.load();

            Ext.create('Ext.container.Viewport', {
                layout: 'border',
                items: [{
                    title: 'Filter',
                    region: 'west',
                    collapsible: true,
                    split: true,
                    titleCollapse: true,
                    items: [
                        {  
                            items: datePickers
                        },
                        {
                            //I want to center this button horizontally:
                            items: submit
                        }

                    ],          
                }, {
                    title: 'prices',
                    region: 'center',
                    collapsible: false,
                    items: {
                        items: grid
                    }
                }]
            });
        }
    });
}

所以此刻,按钮出现在这个位置:

但我希望它在那里:

关于如何实现这一点的任何想法?

提前致谢!

编辑:代码pvlt的答案

Ext.create('Ext.container.Viewport', {
    layout: 'border',
    items: [{
       title: 'Filter',
       region: 'west',
       collapsible: true,
       split: true,
       titleCollapse: true,
       width: 300,
       layout: {
           type: 'hbox',
           align: 'middle',
           pack: 'center',
       },
           items: [
              {
                   items:datePickers
              },
              {
                   items: submit
              }
           ],           
       }, {
            title: 'prices',
            region: 'center',
            collapsible: false,
            items: {
               items: grid
            }
       }]
});
extjs extjs4
2个回答
1
投票

您必须包装提交按钮才能使其正常工作。

{
  width: 300,
  layout: {
    type: 'hbox',
    align: 'middle',
    pack: 'center',
  },
  items: [{
    items: submit
  }]
}

布局必须是包装容器的属性


1
投票

renderTo: Ext.getBody()是无意义的,对于datePickerssubmit ......

这可以更简单,->xtype: 'tbfill'的简写。

tbar: ['->', submit, '->']
© www.soinside.com 2019 - 2024. All rights reserved.