EXTJS组合框在展开后不按valueField选择

时间:2009-01-27 14:28:21

标签: javascript combobox extjs

我写了一些效果很好的代码,但我有一个奇怪的错误 这是一个例子......


PLEASE WATCH MY COMBOBOX BUG VIDEO


就像我说的那样,每次datachanged触发时都会运行良好 - 选择正确的索引并显示displayField但是,每次在组合框中输入一些文本后,当“datachanged”触发时,它都不会显示displayField。相反,它显示我启动的setValue方法的值。

奇怪的是,如果我没有输入文字并用鼠标改变选择,那就没有错误。最后,只有在组合框中输入文本时才会出现这种情况。

有没有人听说过这个错误,有解决方案或一些明智的建议?

守则!

两个数据存储

ficheDataStore = new Ext.data.Store({
    id: 'ficheDataStore',
    autoLoad: true,
    proxy: new Ext.data.HttpProxy({
        url: 'ficheDetail.aspx',      // File to connect to
        method: 'GET'
    }),
    baseParams: { clientId: clientId, Type: 'Fiche' }, // this parameter asks for listing
    reader: new Ext.data.JsonReader({   // we tell the datastore where to get his data from
        root: 'results'
    }, [
        { name: 'GUID', type: 'string', mapping: 'GUID' },
        { name: 'TagClient', type: 'string', mapping: 'TagClient' },
        { name: 'Nom', type: 'string', mapping: 'Nom' },
        { name: 'Compteur', type: 'string', mapping: 'CompteurCommunes' },
        { name: 'CompteurCommunesFacturation', type: 'string', mapping: 'CompteurCommunesFacturation' },
        { name: 'AdresseFacturation', type: 'string', mapping: 'AdresseFacturation' },
        { name: 'Codes', type: 'string', mapping: 'Codes' },
        { name: 'Observations', type: 'string', mapping: 'Observations' },
        { name: 'Adresse', type: 'string', mapping: 'Adresse' }

      ])
});

 communesDataStore = new Ext.data.Store({
    autoLoad: true,
    proxy: new Ext.data.HttpProxy({ url: 'ficheDetail.aspx?Type=Communes' }),
    reader: new Ext.data.JsonReader({ root: 'results' }, [{ name: 'Compteur' }, { name: 'Localisation'}])
});

  

谁会为此返回这样的内容   第一:

  {results:[{"Nom":"cercle interieur"},{"Observations":""},{"Codes":" "},{"Adresse":"dd"},{"CompteurCommunes"
    :"1"},{"TagClient":"3-56"},{"GUID":"443609c6-d064-4676-a492-7baa7b4288d1"},{"AdresseFacturation":""}
    ,{"CompteurCommunesFacturation":"1"}]}
  

对于后者:

{"results":[{ "Compteur" : "1","Localisation" : "6200  ST ISIDORE"},{ "Compteur" : "2","Localisation"
 : "21340 CHANGE"},{ "Compteur" : "3","Localisation" : "1200  ELOISE"},{ "Compteur" : "4","Localisation"
 : "1200  ST GERMAIN SUR RHONE"},{ "Compteur" : "5","Localisation" : "75000 PARIS"},{ "Compteur" : "6"
,"Localisation" : "75001 PARIS 1ER ARRONDISSEMENT"}]}

a Combobox

 var comb = new Ext.form.ComboBox(
             {
               store: communesDataStore,
               fieldLabel: 'Code postal',
               // hiddenName: 'Compteur',
               name: 'CompteurCommune',
               id: 'CompteurCommunes',
               width: 300,
               typeAhead: true,
               mode: 'local',
               minChars: 0,
               selecOnFocus: true,
               forceSelection: true,
               valueField: 'Compteur',
               displayField: 'Localisation',
               autocomplete: true,
               emptyText: 'Selectionnez un code postal',
               triggerAction: 'all',
               value: ''
              });

datachanged 事件中,我设置 Combobox “CompteurCommunes”的新值:

   ficheDataStore.addListener('datachanged', handleDatachangedEvent);

     function handleDatachangedEvent() 
       {
        try {
              comb.setValue(ficheDataStore.getAt(4).data.Compteur);                                                                                 
            }
        catch (err) { }
        }

5 个答案:

答案 0 :(得分:3)

这可能是因为当您将随机数据输入到组合中时,它可能无法每次都找到正确的fieldValue。然后它卡在最后一个不存在的值。

在datachanged事件处理程序中执行新的setValue()之前,尝试将ComboBox设置为任何现有值(在combo的数据存储区中)。或者您可以尝试使用clearValue()方法重置上一个(未定义的)valueField。

还存在initList()方法,用于将组合重置为初始状态。

编辑:经过一些测试,我发现: combo.store.clearFilter(); 必须在外部事件处理程序中的setValue之前使用。

答案 1 :(得分:1)

function formatComboBox(value, metaData, record, rowIndex, colIndex, store) {
        myStore = Ext.getCmp('myComboBox');
        myStore.clearFilter();
        var idx = myStore.find('value',value);
        return (idx != '-1') ? myStore.getAt(idx).data.label : value;
}

答案 2 :(得分:0)

首先,Ext JS组合框应自动应用该值并在选择项目时显示,除非您已分配商店并告知Ext该字段需要值。

您看起来要求的值(CompteurCommunes)不会出现在您的阅读器定义中,因此它将成为数据存储中记录的一部分。

为什么要尝试为ComboBox设置此值的根本原因是什么?

答案 3 :(得分:0)

您可以查看Ext.form.ComboBox的hiddenName和hiddenId参数。如果设置与组合框链接的隐藏表单字段的值,则组合框将呈现该值的标签而不是值本身。

当您需要在服务器端设置值并将用户定向到页面时,这非常有用。

另一个有用的方法是selectByValue。此方法将选择值等于传递参数的元素。

在dataChangedListener中,您应该设置与ComboBox关联的值隐藏表单字段,而不是设置组合框的值。在设置隐藏字段的值之后,您可能必须触发selectByValue方法。

您可以查看ExtJS API文档以获取进一步的参考。

答案 4 :(得分:0)

如果有人 - 像我一样 - 通过谷歌到这里,因为它与他们的ComboBox ft.setValue()问题最相似:

经过一个小时踩到Ext的内部结构后,我发现我需要为组合框设置 lazyInit:false 。如果您不知道这一点,它默认为true并且为true可能会导致非逻辑行为。你为什么这样?其他任何事情似乎都不是懒惰的。