简体   繁体   English

ExtJS如何使用renderTo config将组件呈现给另一个特定容器

[英]ExtJS how to use renderTo config to render a component to another specific container

I am learning ExtJS MVC 我正在学习ExtJS MVC

I want to click button then create a panel in specific container. 我想单击按钮,然后在特定容器中创建一个面板。 But I am confuse about the renderTo config. 但我对renderTo配置感到困惑。 I don't know how to render to right side container. 我不知道如何渲染到右侧容器。

My codes as below. 我的代码如下。 First I define two container in Viewport 首先,我在Viewport中定义了两个容器

Ext.define('MyTest.view.Viewport', {
  extend: 'Ext.container.Viewport',
  layout: 'border',

  requires: [
      'MyTest.view.button.TestButton',
  ],

  items: {
  xtype: 'container',
  itemId: 'main',
  region: 'center',
  border: false,
  layout: {
      type: 'hbox',
      pack: 'start',
      align: 'stretch'
  },
  items: [{
      xtype: 'container',
      itemId: 'left',
      style: 'background-color: black;',
      flex: 1,
      items: [{
          xtype: 'testbtn'
      }]
  }, {
      xtype: 'container',
      itemId: 'right',
      flex: 1
  }]
 }

});

Button in view 按钮在视图中

  Ext.define('MyTest.view.button.TestButton', {
  extend: 'Ext.button.Button',
  alias: 'widget.testbtn',

  initComponent: function() {
    var me = this;
    Ext.apply(this, {
      height: 100,
      width: 100
    });
    me.callParent(arguments);
  }
});

And click event in controller 然后单击控制器中的事件

Ext.define('MyTest.controller.Button', {
    extend: 'Ext.app.Controller',
    views: ['MyTest.view.button.TestButton'],

  refs: [{
    ref: 'testbtn',
    selector: 'testbtn'
  }],

  init: function() {
    this.control({
      'testbtn': {
          click: this.test
      }
    });
  },

  test: function() {
    Ext.create('Ext.panel.Panel', {
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });
  }
});

Now it just can render to body. 现在它可以渲染到身体。 How to render it in right side container? 如何在右侧容器中呈现它? Thanks a lot 非常感谢

You can use the add method to add to the items array of a panel or container . 您可以使用add方法添加到panelcontaineritems数组中。

For example: 例如:

Ext.ComponentQuery.query('#westPanel')[0].add({ html:'Some New Component Here.' });

and here is the full code for a more in-depth example: 以下是更深入的示例的完整代码:

Ext.application({
    name : 'Fiddle',

    launch : function() {
        Ext.create('Ext.panel.Panel',{
            renderTo:Ext.getBody(),
            height:800,
            layout:'border',
            tbar:[{
                text:'Add Html To East Region',
                handler: function(btn){
                    Ext.ComponentQuery.query('#westPanel')[0].add({
                        html:'Oh, Hello.'
                    });
                }
            }],
            defaults:{
                xtype:'panel'
            },
            items:[{
                region:'center',
                html:'Center Panel'
            },{
                region:'west',
                width:400,
                itemId:'westPanel',
                items:[]
            }]
        });
    }
});

And a fiddle for demonstration of the working code 并且是演示工作代码的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM