使用extjs自动完成:带有REMOTE查询存储的ComboBox

时间:2013-08-10 19:05:28

标签: extjs4

我想将ComboBox与REMOTE查询一起使用,

我使用extjs 4,

我想用combobox做自动完成

意思是当我在组合框中输入文本时,请求将发送到数据库,以便根据在组合框中输入的文本显示一个emplyees列表(在我的情况下)

我找到了一些使用queryMode: 'remote',hideTrigger:true

的示例

这是我找到的一些链接

http://demo.mysamplecode.com/ExtJs/pages/comboBoxes.jsp

目前我有这个代码用传统方式填写组合

在我的 \ templyeesModel.js 我有

 Ext.define('GenericComboModel', {
            extend: 'Ext.data.Model', 
            fields: [
                 {name: 'label', type: 'string'},
                 {name: 'value',  type: 'string'}
            ]
        });
        var employeesStore= Ext.create('Ext.data.Store', {
         model: 'GenericComboModel'
    });

emplyeesView.js我有

function fillEmployeesCombo() {
        employeesService.getEmployeesList({
            callback:function(employeesList){   

                for(var i=0; i<employeesList.length; i++){
                    var employeesLabel = employeesList[i].libelleEmployees;
                    var employeesId = employeesList[i].idEmployees;
                    employeesStore.add({label: employeesLabel , value: employeesId });

                }

            }  
        });
    }
var employeesPanel = Ext.create('Ext.panel.Panel', {
        title: 'test',
        bodyPadding: 5,
        width: '100%',
        height: '100%',
        autoScroll: true,
        id: 'tab_Employees',
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
        defaultType: 'textfield',
        items: 
        [

            {
                xtype: 'container',
                layout: {
                    type: 'hbox'
                },
                padding: '5 5 5 5',
                items:
                [
{
                                        xtype: 'combobox',
                                        store: employeesStore,
                                        displayField: 'label',
                                        valueField: 'value',
                                        queryMode: 'local',
                                        fieldLabel: 'test',
                                        editable: false,
                                        id: 'employees_IdCombo'

                                    }
                ]
            },
        ] 
    }); 

employeesService.java我有

public List<employees> getEmployeesList() {
        // TODO Auto-generated method stub

        Query query = getSession().createQuery("FROM employees emp ");  
        List result = query.list();
        if(result.size()!=0 && result !=null)
            return result;
        else 
            return null;
    }

但实际上我会更改我的代码: \ templyeesView.js

我会有这样的代码:

xtype: 'combobox',
                                            store: employeesStore,
                                            displayField: 'label',
                                            valueField: 'value',
                                            queryMode: 'remote',
                                            fieldLabel: 'test',
                                            editable: false,
                                            id: 'employees_IdCombo',
                                                                               hideTrigger:true

我认为在 emplyeesModel.js 我应该添加这个概念:

 proxy: {
        type: 'ajax',.......

我认为为了完成我的例子,我应该使用servlet

意思是例子:

 proxy: {
        type: 'ajax',
        url: 'EmployeesServlet',...

有人可以帮我纠正我的代码

我尝试使用此代码:

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

 var employeesStore= Ext.create('Ext.data.Store', {
     model: 'GenericComboModel',
     proxy: {
         type: 'ajax',
         url: 'employeesService',
         reader: {
             type: 'json',
             root: 'users'
         }
     }
 });

//Finally your combo looks like this
{
  xtype: 'combobox',
  store: employeesStore,
  displayField: 'label',
  valueField: 'value',
  queryMode: 'remote',
  fieldLabel: 'test',
  editable: false,
  id: 'employees_IdCombo',
  hideTrigger:true
  queryParam: 'searchStr' //This will be the argument that is going to be passed to your service which you can use this to return your resulkt set

}


    function fillEmployeesComboByParam(String Libelle) {
            employeesService.getEmployeesList(Libelle){
                callback:function(employeesList){   

                    for(var i=0; i<employeesList.length; i++){
                        var employeesLabel = employeesList[i].libelleEmployees;
                        var employeesId = employeesList[i].idEmployees;
                        employeesStore.add({label: employeesLabel , value: employeesId });

                    }

                }  
            });
        }





in `employeesService.java` I have

    public List<employees> getEmployeesList(String libelle) {
            // TODO Auto-generated method stub

            Query query = getSession().createQuery("FROM employees emp where emp.libelle=:libelle ");  
query.setParameter("libelle", libelle);
            List result = query.list();
            if(result.size()!=0 && result !=null)
                return result;
            else 
                return null;
        }

juste我想知道如果这个网址是正确的还是没有

  url: 'employeesService,

1 个答案:

答案 0 :(得分:5)

以下是对extjs的更改,您必须更改服务以处理作为queryParam传递的searchStr

//Your model remains the same as you defined
 Ext.define('GenericComboModel', {
        extend: 'Ext.data.Model', 
        fields: [
             {name: 'label', type: 'string'},
             {name: 'value',  type: 'string'}
        ]
 });

 //Your store will look like

 var employeesStore= Ext.create('Ext.data.Store', {
     model: 'GenericComboModel',
     proxy: {
         type: 'ajax',
         url: 'Your service URL',
         reader: {
             type: 'json',
             root: 'users'
         }
     }
 });

//Finally your combo looks like this
{
  xtype: 'combobox',
  store: employeesStore,
  displayField: 'label',
  valueField: 'value',
  queryMode: 'remote',
  fieldLabel: 'test',
  editable: true,
  id: 'employees_IdCombo',
  hideTrigger:true
  queryParam: 'searchStr' //This will be the argument that is going to be passed to your service which you can use this to return your resulkt set

}