Extjs中Form表單combobox重置後初始化值爲空問題解決

【問題描述】:

     有些時候Ext.form.FromPanel中的組件都寫好了,但是初始化的時候需要給組件填寫初始值,例如combobox,textarea等。

     可以使用Ext.getCmp('combobox元素的id號').setValue('value');來設置初始值。但是這樣設置初始值有個問題,就是如果form.reset();設置初始值的combobox就會被清空。

 

【原因分析】:

      因爲form最初被實例化的時候,combobox是沒有值的。

【解決方法】:

    此時在reset前,增加一句話,即可保證reset到正確的值:  Ext.getCmp('combobox元素的id號').originalValue = value;

 

      在本人的使用過程中還發現另外一個問題,採用上面的方法貌似是解決了初始化爲空值的問題,但是卻發現當點擊其他頁面再返回到該頁面時combobox表單元素標籤名顯示重複,但下拉選擇框不見了,每次彈出該表單窗口頁面combobox表單元素標籤名就多一個顯示出來。

      查看API,發現combobox存在多個ID:

 An itemId can be used as an alternative way to get a reference to a component
     * when no object reference is available.  Instead of using an
{@link #id} with
     * {@link Ext}.{@link Ext#getCmp getCmp}, use
itemId with
     * {@link Ext.Container}.{@link Ext.Container#getComponent getComponent} which will retrieve
     *
itemId's or {@link #id}'s. Since itemId's are an index to the
     * container's internal MixedCollection, the
itemId is scoped locally to the container --
     * avoiding potential conflicts with {@link Ext.ComponentMgr} which requires a
unique
     *
{@link #id}.

    抱着嘗試的態度:

  •     修改id爲itemid,
  •     Ext.getCmp('combobox元素的id號').setValue('value');修改爲Ext.Container.getComponent('combobox元素的itemid號').originalValue = value;

     最終上面重複問題是解決了,但是卻發現重置失效了。最後註釋掉Ext.Container.getComponent('combobox元素的itemid號').originalValue = value; 行代碼居然啥事都沒了,就一個itemid搞定撒了。

 

部分代碼如下:

 //formUI:表單窗口對象(同事自定義的具有表單和窗口特性的)

//formpanel:表單面板FormPanel對象

//點擊“新增”按鈕彈出表單窗口界面

function add() {

         formUI.show("新增");
       // Ext.Container.getComponent('combobox_type').originalValue = "fieldtype";
       // Ext.getCmp('combobox_type').originalValue = "fieldtype";

        formpanel.getForm().reset();

         }

 

//在Grid列表中一行記錄上單擊彈出表單窗口界面,對應記錄中的值設置到表單中

   gridUI.on("cellclick",function(di,record,grid, rowIndex, colIndex, e){

         formUI.show("編輯");

         formpanel.getForm().setValues(record.data);
            });

 

//表單中對應Combobox元素腳本:

{
     layout : 'form',
     labelWidth : 60,
     labelAlign : 'right',
                    colspan : 2,
     items : {
                        itemid:'combobox_type',
      xtype : 'combo',                                        //注意:Extjs版本是3.3
      fieldLabel : '擴展類型',
      width : 150,
      hiddenName : 'extensiontype',
      colspan : 2,
      store : new Ext.data.SimpleStore({
         fields : ['name', 'value'],
         data : [["字段類型", "fieldtype"], ["分詞器", "tokenizer"],["Filter過濾器","filter"],["文本處理","textprocess"],["文件解析","fileparser"],["積分策略","similarity"]]
        }),
      editable : false,
      displayField : 'name',
      value : 'fieldtype',
      valueField : 'value',
      typeAhead : true,
      mode : 'local',
      forceSelection : true,
      triggerAction : 'all',
      selectOnFocus : true
     }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章