2010-06-25 93 views
0

我想创建我自己的日期/时间字段。我知道其他人已经做了一些,我正在做我自己的。ExtJS面板继承/基类

我的问题如下。我想创建一个新的对象DateTime,它扩展了Ext.Panel。我为宽度,高度等指定了一些属性,但我还指定了包含日期字段和时间字段的items属性的值。当我尝试实际实例化创建的对象时,出现错误,提示“对象或属性不受支持”。当我进入错误时,看起来物品收集会引发错误代码如下:

var dateField = new AppealDate({ 
    id: 'dateField', 
    tabIndex: 0, 
    fieldLabel: '', 
    msgTarget: 'under' 
}); 
var timeField = new Ext.form.TimeField({ 
    id: 'timeField', 
    tabIndex: 0, 
    fieldLabel: '', 
    msgTarget: 'under' 
}); 
var DateTime = Ext.extend(Ext.Panel, { 
    id: '', 
    xtype: 'panel', 
    fieldLabel: '', 
    layout: 'table', 
    layoutConfig: { 
     columns: 2 
    }, 
    items: [dateField, timeField] 
}); 

var dateTimeField = new DateTime(); //this throws an error 

回答

1

您的类缺少initComponent。您还需要将面板渲染到某个位置。

DateTime = Ext.extend(Ext.Panel, { 
    initComponent: function() { 
     // define dateField, timeField here. 
     this.dateField = new AppealDate({ 
      id: 'dateField', 
      msgTarget: 'under' 
     }); 
      this.timeField = new Ext.form.TimeField({ 
      id: 'timeField', 
      msgTarget: 'under' 
     }); 
     Ext.apply(this, { 
      items: [this.dateField, this.timeField] 
     }); 
     DateTime.superclass.initComponent.call(this); 
    } 
}); 

var dateTimeField = new DateTime(); 
dateTimeField.render(Ext.get('someDiv')); 
+0

谢谢!这很棒,我从StackOverflow获得了更快的响应,然后从我们支付的实际ExtJS支持中获得了更快的响应。 – extnoob 2010-06-25 18:54:47

+0

只是好奇,为什么我不得不调用initComponent并在其中使用Ext.apply来完成此操作?为什么我原来的帖子不能运作?想知道,所以我可以记住未来的基础班。 – extnoob 2010-06-25 20:57:08

0

作为您的直接问题之外的注释,“DateTime”是面板子类的可怕名称。你希望稍后有人知道他们正在处理什么样的类 - 根据你使用它的方式,“DateTimeField”会更好(尽管这意味着Field子类如下所述...)。

但是,请注意,另一个潜在的问题,因为你打算使用这个面板作为一个字段是一个FormPanel会希望其表单字段支持Ext.form.Field接口,你的“字段”不会(即你将无法将您的DateTime对象添加到表单的items配置中)。因此,如果您的目标是创建一个真正可重用的组件,并将其视为字段,那么您需要添加getValuesetValuemarkInvalid等与您的组成字段进行内部交互的方法。让这一切顺利进行并不是一项微不足道的任务。

(不知道这是你的目标,但是我想我会提到它,因为我自己走了这条路)。

+0

感谢您的反馈。你是对的,不像我想象的那么简单。我不得不为这个面板实现一个领域的所有常见的预期方法。 – extnoob 2010-06-28 21:46:39