ExtJs_Combo的即時檢索

來源:互聯網
上載者:User

標籤:

  項目裡有個功能,需要提供一些商鋪的名稱.一開始的思路是按照地區分類,挺好的.但是測試資料沒有那麼多,所以就沒有發現這個問題.combo的下拉是有限制的,而且一次性所能接受的資料來源大小也有限制.啟動頁面就卡死,報錯~這可怎麼辦? 無奈之下加了個分頁,但是多了之後忽然發現每個頁20個結果,隨便一列也都1K多了啊~ 這要是真心找起來多浪費功夫啊.於是就想到能不能根據輸入的去模糊查詢.毫無疑問,這是可行的~ 但是不知道是我搜尋不得法還是怎麼的,發現找的好多的模糊比對的就是載入到local,然後去匹配...這尼瑪,不是一樣的效果麼?依然卡死.後來看extjs的API發現combo有幾個叼叼的屬性啊.分分鐘出結果.還是前人說的好,人最大的努力就是去做那些自己認為不會不能成功的事情,因為做完回頭看會忽然發現,啊哈~不過如此嘛.

  閑話不多說,先貼代碼咯.下面這就是  xtype:‘combo‘ 的正常屬性

 1            xtype : ‘combo‘, 2                 fieldLabel : ‘店鋪名稱‘, 3                 valueField : ‘id‘, 4                 displayField : ‘text‘, 5                 hiddenName : ‘shopid‘, 6                 id : ‘x_shopid‘, 7                 name : ‘shopid‘, 8                 store : shop_store, 9                 10                 editable : true,//允許編輯11                 minChars : 1,//開始查詢最小字元數12                 mode : ‘remote‘, //查詢模式  local為本機資料源  remote為遠程服務端13                 typeAhead : false,//允許自動選擇匹配項  經過延時typeAheadDelay後14 //              typeAheadDelay:200,//經過指定毫秒後 自動選擇匹配選項 預設250毫秒15                 triggerAction : ‘all‘, //觸發器被啟用時執行的動作。使用‘all‘來運行由allQuery屬性指定的查詢(預設為‘query‘)  param{ Query : XXX}16                 pageSize : 10, //分頁大小17 //              readOnly:true,  18                 listWidth : 250, //下拉框寬度19 20                 displayField : ‘text‘,21                 anchor : ‘90%‘

  注意到 mode : ‘remote‘  這個屬性.  必須定義成remote 從伺服器擷取資料來源 如果改成local本地的話...哈哈~那就啥都看不到唄.然後是typeAhead 這個是允許自動匹配的,一般就是符合要求的條件自動匹配第一個. 延時預設250毫秒,但是可以自己通過typeAheadDelay指定哈.    pageSize也加著吧,不然容易出事兒.  truggerAction是一個指定觸發器被啟用時候執行的動作,預設查詢參數是query...  等同於param{querty:xxx}...當然了,最重要的是:editable要為true~  別問我為啥...

  資料來源就沒啥好看的了.

 1     shop_store=new Ext.data.Store({ 2         // autoLoad : true, 3         reader : new Ext.data.JsonReader({ 4                     totalProperty : "totalCount", 5                     root : ‘result‘ 6                 }, Ext.data.Record.create([{ 7                             name : ‘id‘ 8                         }, { 9                             name : ‘text‘10                         }])),11         proxy : new Ext.data.HttpProxy({12                 url : Ext.getDom(‘root‘).value+ ‘/helper/helper!shopCombox.do?bizloc=‘+Ext.getCmp(‘c_adbizloc‘).getValue()13                         })14     });

  定義一個var shop_store=new Ext.data.Store();  然後後就完事大吉了~然後就是後面的查詢分頁

 1     public void shopCombox() throws Exception { 2          3          4         if (!"".equals(this.getRequest().getParameter("query"))&&null!=this.getRequest().getParameter("query")) { 5         List<ShopInfo> list = dataCacheUtil.getShopList(this.getRequest().getParameter("query")); 6          7         if (list.size()==0) {//如果暫無商家防止空值異常 8             ShopInfo f=new ShopInfo(); 9             f.setId("");10             f.setShopname("查無相關結果");11             list.add(f);12 //            printJson("{\"result\":[],\"totalCount\":0}" );13 //            return;14         }15         16         int start = Integer.parseInt(this.getRequest().getParameter("start")); //開始數 17         int limit = Integer.parseInt(this.getRequest().getParameter("limit")); //間隔數18         19         PrintWriter out = this.getResponse().getWriter();20         int totalcount = list.size();21 22         int end=start+limit;//防止尾頁越界23         if (list.size()<end) {24             end=list.size();25         }26         27         List<ShopStore> li=new ArrayList<ShopStore>();28         ShopStore s=null;29         30         for (int i = start; i < end; i++) {31             s=new ShopStore();32             s.setId(list.get(i).getId());33             s.setText(list.get(i).getShopname());34             li.add(s);35         }36         37         JSONArray jsonObject = JSONArray.fromObject(li);38         JSONObject reqObject = new JSONObject();39         reqObject.element("result", jsonObject);40         int totalPageCount = (int) Math.ceil((double) totalcount / 10);41         reqObject.element("totalPageCount", totalPageCount);42         reqObject.element("countPerPage", this.getRequest().getParameter(43                 "limit"));44         reqObject.element("totalCount", totalcount);// pages為總頁數。45         out.print(reqObject.toString());46         47     }else{48         printJson("{\"result\":[],\"totalCount\":0}" );49         return;50     }51         52     }    

  之後就是sql了  後面查詢出的結果放到list裡面,傳回來  List<ShopInfo> list = dataCacheUtil.getShopList(this.getRequest().getParameter("query")); 分分鐘搞定啊!啟動頁面再一實驗,哇哈哈哈.搞定了~ 當然,還有一個問題就是如果相似結果太多的話也會有很多頁,但是有分頁攔著,一般也不會太差勁吧.稍後的話可以繼續最佳化一下.

 

  W  _葉落星辰

ExtJs_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.