0

我在 Extjs 中创建了 3 个面板、2 个按钮(上一个、下一个)并添加到视口。一次只能看到一个面板(默认情况下是第一个面板)。单击下一个按钮时,它应该显示下一个面板,然后如果我单击“上一个”按钮,它应该显示上一个面板。

现在我已经为面板编写了一个代码,它的工作原理是,面板不能正确地左右导航。

这是我的代码:

Ext.application({
name: 'HelloExt',
requires: [
'Ext.util.Point' 

 ],
launch: function() {

 var button =Ext.create('Ext.Button', {
    text: 'Toggle Containers',
    handler: function () {      

    if (this.clickCount==1) {            
        containerPanel1.getEl().scrollRight; 
        containerPanel2.getEl().slideIn('t', 'toggle');
        this.clickCount=2;

    } else {

        this.clickCount = 1;            
        containerPanel1.getEl().slideIn('t', 'toggle');
        containerPanel2.getEl().scrollLeft;

        }

    }, 
    renderTo: Ext.getBody()
}); 


var containerPanel1 = Ext.create('Ext.panel.Panel', {
renderTo: Ext.getBody(),
draggable: {
      insertProxy: false,

      startDrag: function(e) {
        var el = Ext.get(this.getEl());
        el.dd = new Ext.dd.DDProxy(el.dom.id, 'group');
        this.x = el.getLeft(true);
        this.y = el.getTop(true);           
      },          

      afterDrag: function(e) {
          this.x = el.getLeft(true);
          this.y = el.getTop(true);
          this.fireEvent('itemdrag', this);

      }
    },
width:400,
height:550,    
layout: 'column',
bodyStyle:{'background-color':'blue'},
margin:'30 0 0 20', 
suspendLayout: true ,   
defaults :{
xtype: 'panel',
margin:'30 0 0 0',
height: 450,
columnWidth: 0.2
},

 items: [
    {
        html: 'Child Panel 1',            
    },
    {           
        html: 'Child Panel 2',          
    },
    {          
        html: 'Child Panel 3',          
    },
    {            
        html: 'Child Panel 4',          
    },
    {           
        html: 'Child Panel 5',          
    }
]
      });

containerPanel1.draggable;

var containerPanel2 = Ext.create('Ext.panel.Panel', {
renderTo: Ext.getBody(),
draggable: {
      insertProxy: false,

      startDrag: function(e) {
        var el = Ext.get(this.getEl());
        el.dd = new Ext.dd.DDProxy(el.dom.id, 'group');
        this.x = el.getLeft(true);
        this.y = el.getTop(true);           
      },          
          afterDrag: function(e) {
          this.x = el.getLeft(true);
          this.y = el.getTop(true);
          this.fireEvent('itemdrag', this);

      }
    },
width:400,
height:550,  
layout: 'column',
bodyStyle:{'background-color':'green'},
margin:'30 0 0 20', 
suspendLayout: true ,   
defaults :{
xtype: 'panel',
margin:'30 0 0 0',
height: 300,
columnWidth: 0.2
},  
 items: [           
    {      
        html: 'Child Panel 1',            
    },
    {            
        html: 'Child Panel 2',          
    },
    {           
        html: 'Child Panel 3',          
    },
    {            
        html: 'Child Panel 4',          
    },
    {           
        html: 'Child Panel 5',

    }
]
   });
     containerPanel2.draggable;
    containerPanel2.getEl().hide();

       Ext.create('Ext.container.Viewport', {
   layout: 'column',
    items: [containerPanel1,containerPanel2,button]

    }); 


    }
     });          

请帮助我..谢谢

4

2 回答 2

0

我只是在构建一个类似的项目,所以这里有一个您可以使用的示例片段。请注意,您可以更改面板的数量和顺序,代码仍然有效。单击处理程序只是首先查找可见面板,然后根据方向尝试前进或后退。

Ext.define('MyApp.view.MyViewport1', {
    extend: 'Ext.container.Viewport',

    id: 'switchPanels',

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'panel',
                    height: 100,
                    html: 'this is panel 1',
                    title: 'My Panel A'
                },
                {
                    xtype: 'panel',
                    height: 100,
                    hidden: true,
                    html: 'this is panel 2',
                    title: 'My Panel B'
                },
                {
                    xtype: 'panel',
                    height: 100,
                    hidden: true,
                    html: 'this is panel 3',
                    title: 'My Panel C'
                },
                {
                    xtype: 'container',
                    switchPanel: function(forward) {
                        var p = Ext.ComponentQuery.query('panel(true){isVisible()}')[0]; // visible panel
                        var n = forward ? p.nextSibling('panel(true)') : p.previousSibling('panel(true)'); // closest sibling
                        if (n) { // don't let go past the last one
                            p.hide();
                            n.show();
                        }
                        else {
                            console.log("can't go past the " + (forward ? 'end' : 'beginning'));
                        }
                    },
                    id: 'buttons',
                    items: [
                        {
                            xtype: 'button',
                            handler: function(button, event) {
                                button.up().switchPanel(false);
                            },
                            text: 'Prev'
                        },
                        {
                            xtype: 'button',
                            handler: function(button, event) {
                                button.up().switchPanel(true);
                            },
                            text: 'Next'
                        }
                    ]
                }
            ]
        });

        me.callParent(arguments);
    }

});
于 2013-06-05T17:01:50.983 回答
0

您需要为此使用卡片(向导)布局。请参考simple sample example下文。

我相信这将帮助您解决问题。

var active = 0;
var main = Ext.create('Ext.panel.Panel', {
    renderTo: Ext.getBody(),
    width: 200,
    height: 200,
    layout: 'card',
    tbar: [{
        text: 'Next',
        handler: function(){
            var layout = main.getLayout();
            ++active;
            layout.setActiveItem(active);
            active = main.items.indexOf(layout.getActiveItem());
        }
    }],
    items: [{
        title: 'P1'
    }, {
        title: 'P2'
    }, {
        title: 'P3',
        listeners: {
            beforeactivate: function(){
                return false;
            }
        }
    }]
});

请参考以下链接中的卡片(向导)布局

http://docs.sencha.com/extjs/4.2.0/#!/example/layout-browser/layout-browser.html

谢谢

于 2013-06-06T05:32:21.707 回答