2011-05-11 81 views
24

我將我的應用程序從ExtJs 3遷移到4版本。 我有幾個組合框在我的formPanel,以前我用隱藏名稱 和所有stuff提交valueField而不是displayField。Extjs 4 combobox默認值

我的所有適應都可以正常工作(值域是提交),但我無法設置組合框的默認值,它們在頁面加載後顯示爲空。 以前,我只是在config中指定了'value'參數。 有沒有什麼想法如何解決這個問題?

我的代碼 - 型號和商店:

Ext.define('idNamePair', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'id', type: 'string'}, 
     {name: 'name', type: 'string'} 
    ] 
}); 

var dirValuesStore = new Ext.data.Store({ 
    model: 'idNamePair', 
    proxy: { 
     type: 'ajax', 
     url: '../filtervalues.json', 
     reader: { 
      type: 'json', 
      root: 'dir' 
     } 
    }, 
    autoLoad: true 
}); 

組合配置:

{ 
    triggerAction: 'all', 
    id: 'dir_id', 
    fieldLabel: 'Direction', 
    queryMode: 'local', 
    editable: false, 
    xtype: 'combo', 
    store : dirValuesStore, 
    displayField:'name', 
    valueField:'id', 
    value: 'all', 
    width: 250, 
    forceSelection:true 
} 
+0

請張貼一些示例代碼,讓我們來看看這個問題,一個可能的解決方案。 –

+0

問題恰恰在於。沒有代碼需要,即使我不知道答案,因爲我仍然卡在3.x – sra

+0

我想這又是一個異步加載存儲和組合的問題,因爲如果商店是在組合內定義的 - 它工作正常。 – BlackLine

回答

4

我注意到你的組合配置具有queryMode: 'local'。該值適用於將數據本地存儲在數組中的情況。但是你的模型正在使用AJAX代理。難道這會讓Ext混淆,所以它找不到你想要設置的默認值?嘗試刪除queryMode,所以它默認爲'遠程'的值(或明確設置)。

更新:發佈上述內容後,我正在將自己的應用從Ext3遷移到4,並且遇到了完全相同的問題。我確信queryMode是其中的一部分,但主要問題是組合框在渲染時沒有所需的數據。設置value確實給它一個值,但數據存儲中沒有任何內容與它匹配,所以該字段顯示爲空白。我發現autoLoad屬性也可以指定數據加載時要使用的回調函數。這裏是你可以做什麼:

store: new Ext.data.Store({ 
    model: 'MyModel', 
    autoLoad: { 
     scope: this, 
     callback: function() { 
      var comboBox = Ext.getCmp("MyComboBoxId"); 
      var store = comboBox.store; 

      // set the value of the comboBox here 
      comboBox.setValue(blahBlahBlah); 
     } 
    } 
    ... 
}) 
17

我有同樣的問題,據我所知有選擇列表的渲染做的項目被添加到商店之前。我嘗試了上面提到的回調方法,但沒有任何運氣(猜測它必須是選擇列表上的回調,而不是商店)。

我加入這行添加項目到店裏後,它工作正常:

Ext.getCmp('selectList').setValue(store.getAt('0').get('id')); 
+0

謝謝,你完全正確 –

0

我敢打賭,這是與時間,你(異步)加載組合框做的,到時候你設置的值的組合框。要解決此問題,請執行以下操作:

Ext.define('idNamePair', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'id', type: 'string'}, 
     {name: 'name', type: 'string'} 
    ] 
}); 

var dirValuesStore = new Ext.data.Store({ 
    model: 'idNamePair', 
    proxy: { 
     type: 'ajax', 
     url: '../filtervalues.json', 
     reader: { 
      type: 'json', 
      root: 'dir' 
     } 
    }, 
    autoLoad: false // set autoloading to false 
}); 

存儲的自動加載已關閉。現在,您已將您的ComboBox放置在某個地方 - 使用您的起始帖子中的代碼 - 您只需手動加載商店:dirValuesStore.load();

這可能是在某些組件的initComponent()中配置Ext.apply(this, {items: [..., {xtype: 'combo', ...}, ...]})之後。

0

試試這個代碼:

var combo = new Ext.form.field.ComboBox({ 
    initialValue : something, 
    listeners: { 
     afterrender: function(t,o) { 
      t.value = t.initialValue;  
     } 
    } 
}) 
3

您可以直接把邏輯步入回調,或建立一個函數來處理所有的商店。

var store1 = Ext.create('Ext.data.Store', { 
    ... 
    autoLoad: { 
     callback: initData 
    } 
}); 

var store2 = Ext.create('Ext.data.Store', { 
    ... 
    autoLoad: { 
     callback: initData 
    } 
}); 

var myComboStores = ['store1', 'store2'] 

function initData() { 
    var loaded = true; 
    Ext.each(myComboStores, function(storeId) { 
     var store = Ext.StoreManager.lookup(storeId); 
     if (store.isLoading()) { 
      loaded = false; 
     } 
    } 
    if(loaded) { 
     // do stuff with the data 
    } 
} 

=====================

對於那些閱讀,你的 '二合一' 的配置/性能對象應設置爲某個值,以便組合框獲得初始值。你已經做到了。值「all」也需要在您的商店中,然後纔會將其設置爲默認值。

value: 'all' 

此外,這是很好的做法,設置了valueField配置,您已經做了值。如果你不這樣做,那麼在調用combo.getValue()時,select監聽器將不會得到正確的值。

4

執行此操作的最佳方法是,監聽afterrender事件,然後在回調函數中設置默認值。

看到這個代碼:

new Ext.form.field.ComboBox({ 
    //This is our default value in the combobox config 
    defaultValue: 0, 
    listeners: { 
     //This event will fire when combobox rendered completely 
     afterrender: function() { 
      //So now we are going to set the combobox value here. 
      //I just simply used my default value in the combobox definition but it's possible to query from combobox store also. 
      //For example: store.getAt('0').get('id'); according to Brik's answer. 
      this.setValue(this.defaultValue);  
     } 
    } 
}); 
10

添加loading: true您的存儲配置將修復它。 autoLoad: trueforceSelection: true似乎存在問題。 這個小黑客會讓你的組合框相信商店正在加載,即使加載函數還沒有被觸發。

+0

@BlackLine很好!我會投兩次!該解決方案簡單而有效。 – leaf

+1

對於那些爲什麼會這樣工作的好奇者,這是因爲[Store構造函數](http://docs.sencha.com/extjs/4.2.1/source/Store.html#Ext-data-Store-method-構造函數),將'autoLoad'的加載調用延遲1ms(參見構造函數代碼的末尾)。因此,僅當調用組合框'setValue'方法後,商店的'loading'屬性才設置爲'true',所以組合商店認爲商店被加載並且該值無效(因爲商店處於事實是空的)。 – rixo

+1

我建議通過重寫'Ext.data.Store#constructor'來設置系統範圍,如果'autoLoad'也是'true',則將'loading'設置爲'true'。例如。 '分機。定義(NULL,{ \t控制裝置: 'Ext.data.Store', \t構造:功能(){ \t \t this.callParent(參數); \t \t如果(this.autoLoad){ \t \t \t this.loading = true; \t \t} \t} });'' – rixo

0

在配置中指定'value'參數是爲組合框設置默認值的正確方法。

在你的例子中,只需設置forceSelection:false,它會正常工作。

如果您想設置forceSelection:true,您應該確保從您的商店返回的數據包含的值等於您的默認值(在本例中爲'all')。否則,它將是默認情況下爲空文本。 更清楚,請通過此更換您的dirValuesStore定義:

var dirValuesStore = Ext.create('Ext.data.Store', { 
     fields: ['id', 'name'], 
     data: [ 
      {id: 'all', name: 'All'}, 
      {id: '1', name: 'Name 1'}, 
      {id: '2', name: 'Name 2'}, 
      {id: '3', name: 'Name 3'}, 
      {id: '4', name: 'Name 4'} 
     ] 
    }) 

你會看到它的作品!

0

在ExtJS的5.0.1這應該工作,在配置組合:

... 
editable: false, 
forceSelection: true, 
emptyText: '',