Extjs3 Combo實現百度搜尋查詢

來源:互聯網
上載者:User

標籤:span   extend   base   length   one   rem   trigger   onload   資料來源   

在Extjs中實現Combo手輸模糊篩選出下拉框資料。之前一直利用的Combo的keyup來即時的請求資料庫進行查詢。最近發現了一個更好的方式:只需要引用一個ComboBoxQuery

Ext.ns(‘Ext.plugins.ComboBoxQuery‘);Ext.plugins.ComboBoxQuery = function (config) {    Ext.apply(this, config);};Ext.extend(Ext.plugins.ComboBoxQuery, Ext.util.Observable, {    minChars: 1,    init: function (combo) {        this.combo = combo;        this.combo.on(‘beforequery‘,function(qe){            var cmb = qe.combo;            var q = qe.query;            var forceAll = qe.forceAll;            if (forceAll === true || (forceAll == undefined && cmb.mode == ‘remote‘) || (q.length >= this.minChars)) {                if (cmb.lastQuery !== q) {                    cmb.lastQuery = q;                    if (cmb.mode == ‘local‘) {                        cmb.selectedIndex = -1;                        if (forceAll||q==="") {                            cmb.store.clearFilter();                        } else {                            // 檢索的正則                            var regExp = new RegExp(".*" + q + ".*", "i");                            // 執行檢索                            cmb.store.filterBy(function(record, id) {                                // 得到每個record的項目名稱值                                var text = record.get(combo.displayField);                                return regExp.test(text);                            });                        }                        cmb.onLoad();                    } else if (cmb.forceQueryInLocal){                        if(cmb.store.getCount()>0){                            this.isRemoteStoreLoaded = true;                        } else if(!this.isRemoteStoreLoaded){                            cmb.store.load();                            this.isRemoteStoreLoaded = true;                        }                        cmb.selectedIndex = -1;                        if(q==="")                            cmb.store.clearFilter();                        else{                            var regExp = new RegExp(".*" + q + ".*", "i");                            // 執行檢索                            cmb.store.filterBy(function(record, id) {                                // 得到每個record的項目名稱值                                var text = record.get(combo.displayField);                                return regExp.test(text);                            });                        }                        cmb.expand();                        cmb.restrictHeight();                    } else {                        cmb.store.baseParams[this.queryParam] = q;                        cmb.store.load({                            params: cmb.getParams(q)                        });                        cmb.expand();                        cmb.restrictHeight();                    }                } else {                    cmb.selectedIndex = -1;                    cmb.onLoad();                }            }            return false;        });        //解決當combobox的store提前載入後,再點擊輸入框不能自動彈出下拉框的問題        this.combo.on(‘focus‘, function(cmb){            if(!cmb.list){                cmb.initList();            }            if(!cmb.isExpanded()) {                cmb.expand();                cmb.restrictHeight();            }        });    }});
View Code

然後在Combo中加入

var Store = new Ext.data.JsonStore({    url: ‘xxxx‘,    method: ‘Post‘,    root: ‘Table‘,    autoLoad: true,    fields: [‘Id‘, ‘Name‘]});xtype: ‘combo‘,editable: false,mode: ‘local‘,displayField: ‘Name‘,valueField: ‘Id‘,triggerAction: ‘all‘,store: Store,editable: true,plugins: [new Ext.plugins.ComboBoxQuery()],forceSelection: true,width: 250,fieldLabel: ‘測試‘

主要是  plugins: [new Ext.plugins.ComboBoxQuery()],

forceSelection: true,  這兩句代碼。

plugins引用外掛程式,

forceSelection:true輸入只能是combo的資料來源裡存在的資料。

以上版本只在Extjs3的測試使用。

歡迎Extjs使用者加入QQ群:460607949一起交流學習。

Extjs3 Combo實現百度搜尋查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.