ITeye博客

Ext.form.ComboBox调用store.filterBy失效问题

pingfan · 2009-11-11 10:16 · extjs · Markdown

问题:
在ComboBox初始化后,调用combobox.getStore().filterBy()来过滤下拉框的数据,失效,

但当点击一次下拉框后,再调用过滤,一切正常..

解决办法:
设置combobox的lastQuery属性为''

评论(4)

  1. #4 楼 · zhangbaocheng · 2014-03-03 16:09

    非常感谢,解决了我的大问题了

  2. #3 楼 · pingfan · 2010-01-15 11:42

    我用的ext版本是ext.3.0.0

  3. #2 楼 · pingfan · 2010-01-15 11:40

    以下有一段测试代码,你看看效果.

        **//用代码初始化,选择"国家3"**
        this.countryCmb.setValue("3");
        this.filterBy("3");
        //1.先打开,点击stateCmb,可以看到已经正常过滤了.
        //2.把this.stateCmb的lastQuery=''注释掉,你再看看效果.发现this.stateCmb没有被过滤.
        //我说的就是这个问题.至于你的应用的问题,我不太清楚..
    
    
    Ext.ns("com.quizzpot.tutorial");
    
    com.quizzpot.tutorial.LinkedComboBoxTutorial = {
        init: function(){
            //code here
            Ext.data.Store.prototype.updateSnapshot = function() {
                this.snapshot = this.data
            }
            this.countryStore = new Ext.data.Store({
                reader: new Ext.data.JsonReader({
                    fields: ['value','label']
                }),
                data:[{label:'国家1',value:'1'},{label:'国家2',value:'2'},{label:'国家3',value:'3'},{label:'国家4',value:'4'}]
            });     
            this.stateStore = new Ext.data.Store({
                reader: new Ext.data.JsonReader({
                    fields: ['value','label','country']
                }),
                data:[
                	{label:'地区1a',value:'11',country:'1'},
                	{label:'地区1b',value:'12',country:'1'},
                	{label:'地区1c',value:'13',country:'1'},
                	{label:'地区2',value:'2',country:'2'},
                	{label:'地区3a',value:'31',country:'3'},
                	{label:'地区3b',value:'32',country:'3'},
                    {label:'地区4',value:'4',country:'4'}
                ]
            });    
            this.countryCmb = new Ext.form.ComboBox({
                store: this.countryStore,
                editable: false,
                forceSelection: true,  
                selectOnFocus:true,
                blankText:'请选择',
                id: 'country',
                valueField: 'value',
                displayField: 'label',
                triggerAction: 'all',
                mode: 'local',          
                emptyText: '选择一个筛选项',
                fieldLabel: 'Country',
                lastQuery:''
              //applyTo: 'local-states'
            });
    
            this.stateCmb = new Ext.form.ComboBox({
                store: this.stateStore,
                forceSelection: true,  
                selectOnFocus:true,  
                id: 'state',
                valueField: 'value',
                displayField: 'label',
                triggerAction: 'all',
                mode: 'local',
                emptyText: 'Select a Contry first',
                fieldLabel: 'State'
                ,
                lastQuery:''
                //renderTo: document.body
                
            });
            
            this.window = new Ext.Window({
                title: 'Linked ComboBox',
                layout:'form',
                width:300,
                height:200,
                bodyStyle: 'padding:5px;background-color:#fff',
                items: [this.countrycmb,this.statecmb]
            });
            this.window.show();
            this.filterBy = function(value){
            	this.stateCmb.getStore().filterBy(function(record,id){
                    var countryId = record.get('country')+"";
                    return value == countryId;
                },this);
            }
            
            this.countryCmb.on('select',function(cmb,record,index){
                var value = cmb.getValue()+"";
                this.filterBy(value);            
            },this);
            
            
            
            //用代码初始化,选择"国家3"
            this.countryCmb.setValue("3");
            this.filterBy("3");
            //1.先打开,点击stateCmb,可以看到已经正常过滤了.
            //2.把this.stateCmb的lastQuery=''注释掉,你再看看效果.发现this.stateCmb没有被过滤.
            //我说的就是这个问题.至于你的应用的问题,我不太清楚..
            
        }
    }
    Ext.onReady(com.quizzpot.tutorial.LinkedComboBoxTutorial.init,com.quizzpot.tutorial.LinkedComboBoxTutorial);
    
    
  4. #1 楼 · falloutxxx2 · 2010-01-15 00:59

    last Query 在ext3下面没用?

    Ext.ns("com.quizzpot.tutorial");
    
    com.quizzpot.tutorial.LinkedComboBoxTutorial = {
    	init: function(){
    		//code here
    Ext.data.Store.prototype.updateSnapshot = function() {
    	    this.snapshot = this.data
    	}
    		this.countryStore = this.getStore();
    		this.stateStore = this.getStore();
    		this.countryCmb = new Ext.form.ComboBox({
    			store: this.countryStore,
    			editable: false,
    			forceSelection: true,  
    			selectOnFocus:true,
    			blankText:'请选择',
    			id: 'country',
    			valueField: 'value',
    			displayField: 'label',
    			triggerAction: 'all',
    			mode: 'local',			
    			emptyText: '选择一个筛选项',
    			fieldLabel: 'Country',
    			lastQuery:'',
    		  //applyTo: 'local-states'
    		});
    
    		this.stateCmb = new Ext.form.ComboBox({
    			store: this.stateStore,
    			disabled: true,
    			forceSelection: true,  
    			selectOnFocus:true,  
    			id: 'state',
    			valueField: 'value',
    			displayField: 'label',
    			triggerAction: 'all',
    			mode: 'local',
    			emptyText: 'Select a Contry first',
    			fieldLabel: 'State',
    			lastQuery:'',
    			renderTo: document.body
    			
    		});
    		
    
    		this.window = new Ext.Window({
    			title: 'Linked ComboBox',
    			layout:'form',
    			width:300,
    			height:200,
    			bodyStyle: 'padding:5px;background-color:#fff',
    			items: [this.countrycmb,this.statecmb]
    		});
    		this.window.show();
    
    		this.stateCmb.on('beforequery',function(e){  
    	         var combo = e.combo;  
    	         if(!e.forceAll){  
    	             var value = e.query;  
    	             combo.store.filter(function(record,id){  
    	             var text = record.get(combo.displayField);  
    	                         //用自己的过滤规则,如写正则式  
    	             return (text.indexOf(value)!=-1);  
    	             });  
    	             combo.expand();  
    	             return false;  
    	         }  
    	     });
    	    
    		this.countryCmb.on('select',function(cmb,record,index){
    			this.stateCmb.enable();
    			this.stateCmb.clearValue();
    			this.stateStore.load({params:{id:record.get('value')}});
    		},this);
    	},
    	
    	getStore: function(){
    //		var store = new Ext.data.JsonStore({
    //			url:'linked-cmb.php',
    //			root:'data',
    //			fields: ['value','label']
    //		});
    //
    		
            var store = new Ext.data.Store({
                reader: new Ext.data.JsonReader({
                	fields: ['value','label'],
                	root:'data'
                }),
                proxy: new Ext.data.HttpProxy({
                    url: 'linked-cmb.php'
                }),
                autoLoad: true
            });		
    		return store;
    	}
    }
    
    Ext.onReady(com.quizzpot.tutorial.LinkedComboBoxTutorial.init,com.quizzpot.tutorial.LinkedComboBoxTutorial);
    /*
    var combo = new Ext.form.ComboBox({
    id:'somecombo',
    store: combostore,
    displayField:'state',
    typeAhead: true,
    width: 180,
    mode: 'local',
    //enableKeyEvents: true,
    forceSelection: true,
    triggerAction: 'all',
    emptyText:'Select a state...',
    selectOnFocus:true,
    renderTo: document.body
    });
    
    combo.on('beforequery',function(e){
    var combo = e.combo;
    if(!e.forceAll){
    	var value = e.query;
    	combo.store.filterBy(function(record,id){
    		var text = record.get(combo.displayField);
                    //用自己的过滤规则,如写正则式
    		return (text.indexOf(value)!=-1);
    	});
    	combo.expand();
    	return false;
    }
    });
    */
    

评论功能维护中,当前仅展示历史评论。