2012-08-02 76 views
0

我有一个combobox,我需要从服务器填充数据。在服务器端,我有以下数据要显示。从远程服务器填充Combobox

PersonID 
PersonFName 
PersonLName 

在ComboBox,我需要显示的文字为PersonFName + PersonLName(像James Smith - 这是它会在下拉显示向下),并且当用户选择一个记录,我需要显示相应的PersonID (如PersonFNamePersonLName的人拥有该用户的PersonID 1)。

我无法想出解决办法,这是我的代码

查看:

{ 
        xtype: 'combobox', 
        id: 'personcombo', 
        readOnly: false, 
        selectOnFocus: true, 
        forceSelection: true, 
        store: 'Person' 
      } 

商店:

Ext.define('MyApp.store.PersonStore', { 
    extend: 'Ext.data.Store', 
    requires: [ 
     'MyApp.model.Person' 
    ], 

    constructor: function(cfg) { 
     var me = this; 
     cfg = cfg || {}; 
     me.callParent([Ext.apply({ 
      model: 'MyApp.model.Person', 
      proxy: { 
       type: 'ajax', 
       api: { 
        read: 'person.php', 
        create: 'person.php' 
       }, 
       reader: { 
        type: 'array' 
       } 
      } 
     }, cfg)]); 
    } 
}); 

型号:

Ext.define('MyApp.model.Person', { 
    extend: 'Ext.data.Model', 

    fields: [ 
     { 
      name: 'PersonID' 
     }, 
     { 
      name: 'PersonFName' 
     }, 
     { 
      name: 'PersonLName' 
     } 
    ] 
}); 
+1

你现在看到什么?主要问题是“如何显示组合字段”? – sha 2012-08-02 12:37:15

+0

是的,你是对的。我需要知道如何显示组合字段。 – Illep 2012-08-03 17:06:32

+0

看看@Geronimo如何创建动态字段的答案。 – sha 2012-08-03 17:08:54

回答

3

我觉得你的问题是:如何显示PersonFName + PersonLName在下拉列表,但保持PersonID字段的值。

您应该添加一个converted字段,该字段将数据模型中的名字和姓氏连接起来,然后将该名称与您的组合框displayField配置在一起。

尽管其他答案的确提出了一个很好的观点,即您的组合中定义的商店是Person,但您显示的商店的代码为PersonStore

这将是这个样子:

型号:

Ext.define('MyApp.model.Person', { 
    extend: 'Ext.data.Model', 

    fields: [ 
     { 
      name: 'PersonID' 
     }, 
     { 
      name: 'PersonFName' 
     }, 
     { 
      name: 'PersonLName' 
     }, 
     { 
      name: 'PersonName', 
      convert: function(value, record) { 
       return record.data.PersonFName + ' ' + 
        record.data.PersonLName; 
      } 
     } 
    ] 
}); 

商店:

// changed to "Person" instead of "PersonStore" 
Ext.define('MyApp.store.Person', { 
    extend: 'Ext.data.Store', 
    requires: [ 
     'MyApp.model.Person' 
    ], 

    model: 'MyApp.model.Person', 
    proxy: { 
     type: 'ajax', 
     api: { 
      read: 'person.php', 
      create: 'person.php' 
     }, 
     reader: 'array' 
    } 
}); 

查看:

{ 
    xtype: 'combobox', 
    id: 'personcombo', 
    readOnly: false, 
    selectOnFocus: true, 
    forceSelection: true, 
    store: 'Person', 
    valueField: 'PersonID', 
    displayField: 'PersonName' // the converted field 

} 
0

你的组合框有“人'作为商店,但我没有看到你在任何地方创建一个叫Person的商店。尝试store: Ext.create('MyApp.store.PersonStore', {autoLoad: true})

您还可以简化您的商店:

Ext.define('MyApp.store.PersonStore', { 
    extend: 'Ext.data.Store', 
    requires: [ 
     'MyApp.model.Person' 
    ], 

    model: 'MyApp.model.Person', 
    proxy: { 
     type: 'ajax', 
     api: { 
      read: 'person.php', 
      create: 'person.php' 
     }, 
     reader: 'array' 
    } 
}); 
+0

我认为商店会自动创建 – sha 2012-08-02 12:36:22

+0

我不明白。他给商店属性一个字符串,它将使用Ext.data.StoreManager来查找具有该ID的商店,但他从未创建过这样的商店。 – 2012-08-03 12:19:28

+0

我从来没有在我的项目中手动创建商店。只需定义它们并在使用它们的控制器中指定'stores:[]'数组。当您调用'Ext.getStore('storename')'' – sha 2012-08-03 12:28:23