2014-01-26 42 views
1

我在ext js中定義了自定義表單字段。
自定義字段只包含一個按鈕(此按鈕可以是事件的一些文本或圖像,無論)。
我想強制我的價值。所以我設置getValue返回一些字符串。
該方法未被調用。
我想要發生2件事。
1. getValue將是在提交中發送的值。
2. setValue會給我form.load(...)已加載的數據,我會處理我的路......Extjs使用getValue和setValue定義自定義字段

這裏有一個例子,點擊提交,你會看到getValue沒有蜜蜂叫。 http://jsfiddle.net/RB9Hp/

或這更現實生活中的用例:http://jsfiddle.net/FTV3R/ < - 這並不會的getValue

Ext.define('Ext.form.CustomField', { 
    extend: 'Ext.form.FieldContainer', 
    alias: 'widget.customfield', 

    width: 300, 
    constructor: function(config) { 
     this.callParent([config]); 
    }, 
    getValue : function(){ 
     console.log("getValue"); 
     return "test"; // I want to force new value. 
    }, 
    setValue : function(val){}, 
    initComponent: function() { 
     this.items = ([{'xtype' : 'button',text : 'some button'}]); 
     this.callParent(); 
    } 
}); 
+0

可能重複[?如何創建自定義的ExtJS表單字段部件(http://stackoverflow.com/questions/6153362/how-to-create-定製ExtJS的外形場分量) –

回答

4

Ext.form.FieldContainer發送的值就是表單域組件容器。如果你需要你的組件行爲像表單域一樣,它應該使用Ext.form.field.Field mixin。在文檔我們可以看到,這混入提供表單域的邏輯行爲和狀態的通用接口,包括:

  • Getter和現場setter方法值
  • 跟蹤價值活動和方法,有效改變
  • 方法,因爲當它從表單字段您致電後收集數據用此方法由表單觸發驗證

在您的測試情況下,你也應該改變getValuegetSubmitValue米的submit()方法。

Ext.define('Ext.form.CustomField', { 
    extend: 'Ext.form.FieldContainer', 
    alias: 'widget.customfield', 
    mixins: { 
     field: 'Ext.form.field.Field' 
    },  

    width: 300, 
    constructor: function(config) { 
     this.callParent([config]); 
    }, 
    getValue : function(){ 

     return "test"; // I want to force new value. 
    }, 
    getSubmitValue: function() { 
     return "test"; 
    },  
    setValue : function(val){}, 
    initComponent: function() { 
     this.items = ([{'xtype' : 'button',text : 'some button'}]); 
     this.callParent(); 
    } 
}); 

撥弄比如:https://fiddle.sencha.com/#fiddle/2vp