如何在Ext Js 4中渲染面板

时间:2012-08-20 09:36:43

标签: javascript extjs

enter image description here

面板的数量取决于来自商店的数据的数量,并且应该以上面显示的形式呈现 即两列

我的代码是

Ext.define('ConnectionsFeed.view.Communities',{
     extend: 'Ext.container',
     requires: 'ConnectionsFeed.store.Communities',
     store: 'Communities',
     alias: 'widget.communities',
     layout: 'table',

 initComponent: function(){
  var x = this;
  this.store.each(function(item){
   x.add(new Ext.panel.Panel());
  });
 }
});

Ext.create('ConnectionsFeed.view.Communities',{
   renderTo: Ext.getBody()
});

但收到以下错误

> Uncaught TypeError: Cannot read property 'isInstance' of undefined
> ext-all.js:21 (anonymous function) ext-all.js:21 Ext.apply.doProcess
> ext-all.js:21 (anonymous function) ext-all.js:21 Ext.apply.require
> ext-all.js:21 (anonymous function) ext-all.js:21 Ext.apply.doProcess
> ext-all.js:21 Ext.apply.doProcess ext-all.js:21 Ext.apply.process
> ext-all.js:21 Ext.Class.c ext-all.js:21 Ext.ClassManager.create
> ext-all.js:21 Ext.apply.define ext-all.js:21 (anonymous function)

3 个答案:

答案 0 :(得分:1)

Ext.Store.each()让你为数据存储中的每个元素调用一个函数。有问题的函数应该将面板的实例添加到table布局的容器中。

YourStore.each(function(item) {
  //container declared elsewhere
  container.add(new YourPanel(item));
});

答案 1 :(得分:1)

你在initComponent的末尾错过了对this.callParent()的调用。

答案 2 :(得分:1)

容器也没有商店。您需要在initComponent中创建一个实例并加载它。

Ext.define('ConnectionsFeed.view.Communities', {
    extend: 'Ext.container',
    requires: 'ConnectionsFeed.store.Communities',
    store: 'Communities',
    alias: 'widget.communities',
    layout: 'table',

    initComponent: function() {
        var x = this;

        x.store = Ext.create('ConnectionsFeed.store.Communities');

        //If you override initComponent you need to call its parent
        x.callParent();
    },
    afterRender: function() {
        var x = this;

        x.store.load({
            scope: x,
            //Callback function after the store has loaded
            callback: function(records, operation, success) {
                Ext.each(records, function(record) {
                    //Your logic of add a panel here
                    x.add(Ext.create('....'));
                });
            }
        });

        x.callParent();
    }
});

Ext.create('ConnectionsFeed.view.Communities', {
    renderTo: Ext.getBody()
});​