Extjs 如何为Combobox的选定值传递参数并从数据库中获取参数值

Extjs 如何为Combobox的选定值传递参数并从数据库中获取参数值,extjs,Extjs,我正在使用ExtJS。 我有一个组合框,我在其中选择一个值,并使用该值作为参数来获取其他值(两个值)。现在我想把它添加到一个变量中,这样我就可以在组合框以外的其他地方使用它了。我该怎么做 var txtEP=new Ext.form.ComboBox({ renderTo: "txtEP", fieldLabel: 'End Point', triggerAction: "all", forceSelecti

我正在使用ExtJS。 我有一个组合框,我在其中选择一个值,并使用该值作为参数来获取其他值(两个值)。现在我想把它添加到一个变量中,这样我就可以在组合框以外的其他地方使用它了。我该怎么做

var txtEP=new Ext.form.ComboBox({
           renderTo: "txtEP",
           fieldLabel: 'End Point',
           triggerAction: "all",
           forceSelection: true,
           mode:'local',
           autoScroll: true,
           allowBlank: false,
           autoShow: true,
           typeAhead:true,
           store: genres,
           valueField:'pincode',
           displayField:'pincode',
           emptyText:'Select a Start Point',
           selectOnFocus:true,
           listeners : {
               'select' : function(){
               var selVal = this.getValue();
            //endpt(Global Variable) is the variable where i am trying to get this value.
            endpt=store.load({url:'./genres1.php', params: {pincode: selVal}});
                   alert(endpt);
                   }
               }
           //valueField: 'X,Y'     
        });

您必须在回调中将其分配为
store.load
的配置,这是因为当您立即分配它时,存储不包含任何数据。大概是这样的:

var txtEP = new Ext.form.ComboBox({
    renderTo: "txtEP",
    fieldLabel: 'End Point',
    triggerAction: "all",
    forceSelection: true,
    mode:'local',
    autoScroll: true,
    allowBlank: false,
    autoShow: true,
    typeAhead:true,
    store: genres,
    valueField:'pincode',
    displayField:'pincode',
    emptyText:'Select a Start Point',
    selectOnFocus:true,
    listeners : {
        'select' : function(){
            var selVal = this.getValue();
            store.load({
                url:'./genres1.php', 
                params: {pincode: selVal},
                callback: function(records) {
                    endpt = records; // here is where it is assigned
                }
            });
        }
    }
});
firstValue = endpt[0].get('value');
secondValue = endpt[1].get('value');
还要认识到,“endpt”现在将包含一个Ext.data.Model对象数组,因此您可以使用给定的方法从中提取所需的任何值

回答您的评论:

Ext.data.Model有一个
get
方法。将要获取值的字段的名称传递给它。在您的例子中,您提到了
/genres.php
返回两个值,如果数据作为一个记录返回,并包含两个不同的列,如下所示:

var txtEP = new Ext.form.ComboBox({
    renderTo: "txtEP",
    fieldLabel: 'End Point',
    triggerAction: "all",
    forceSelection: true,
    mode:'local',
    autoScroll: true,
    allowBlank: false,
    autoShow: true,
    typeAhead:true,
    store: genres,
    valueField:'pincode',
    displayField:'pincode',
    emptyText:'Select a Start Point',
    selectOnFocus:true,
    listeners : {
        'select' : function(){
            var selVal = this.getValue();
            store.load({
                url:'./genres1.php', 
                params: {pincode: selVal},
                callback: function(records) {
                    endpt = records; // here is where it is assigned
                }
            });
        }
    }
});
firstValue = endpt[0].get('value');
secondValue = endpt[1].get('value');
列标题:|值1 |值2

第1行:|“数据1”|“数据2”

您可以像这样将两个返回的数据值分配给回调函数中的变量,比如您将变量命名为
firstValue
secondValue

firstValue = endpt[0].get('value1');
secondValue = endpt[0].get('value2');
如果相反,您的
/genres.php
将数据返回为两个不同的行,其中只有一个列标题,如下所示:

var txtEP = new Ext.form.ComboBox({
    renderTo: "txtEP",
    fieldLabel: 'End Point',
    triggerAction: "all",
    forceSelection: true,
    mode:'local',
    autoScroll: true,
    allowBlank: false,
    autoShow: true,
    typeAhead:true,
    store: genres,
    valueField:'pincode',
    displayField:'pincode',
    emptyText:'Select a Start Point',
    selectOnFocus:true,
    listeners : {
        'select' : function(){
            var selVal = this.getValue();
            store.load({
                url:'./genres1.php', 
                params: {pincode: selVal},
                callback: function(records) {
                    endpt = records; // here is where it is assigned
                }
            });
        }
    }
});
firstValue = endpt[0].get('value');
secondValue = endpt[1].get('value');
列标题:值

第1行:“数据1”

第2行:“数据2”

您可以将数据分配给如下变量:

var txtEP = new Ext.form.ComboBox({
    renderTo: "txtEP",
    fieldLabel: 'End Point',
    triggerAction: "all",
    forceSelection: true,
    mode:'local',
    autoScroll: true,
    allowBlank: false,
    autoShow: true,
    typeAhead:true,
    store: genres,
    valueField:'pincode',
    displayField:'pincode',
    emptyText:'Select a Start Point',
    selectOnFocus:true,
    listeners : {
        'select' : function(){
            var selVal = this.getValue();
            store.load({
                url:'./genres1.php', 
                params: {pincode: selVal},
                callback: function(records) {
                    endpt = records; // here is where it is assigned
                }
            });
        }
    }
});
firstValue = endpt[0].get('value');
secondValue = endpt[1].get('value');

首先谢谢。但是现在我该如何使用Ext.data.Model对象的数组呢?请作为新手提供帮助。@user1220259我添加了一些示例来说明如何操作this@user1220259如果这对你合适,别忘了接受答案(左边勾选)