標籤:
近來項目需求,在新增或查詢的時候有一個標籤對象需要使用下拉式功能表,但是資料可多可少的.少了的時候看起來還不錯,緊湊一點.但是一旦躲起來看著就那麼彆扭了.人說百度不可靠,我發現這話真沒錯,想著做個分頁來解決這玩意兒,沒想到百度出來的結果催人淚下啊.好多不負責任的博主什麼的直接複製粘貼給連結就結束了,弄得我一愣一愣的.靠天靠地不如靠自己這真是老祖宗的至理名言.為了方便以後檢閱自己的成果,記錄一下.
先是JS內 var shop_store=new Ext.data.Store();
1 shop_store=new Ext.data.Store({//建立資料來源 2 reader : new Ext.data.JsonReader({ 3 totalProperty : "totalCount", 4 root : ‘result‘ 5 }, Ext.data.Record.create([{ 6 name : ‘id‘ 7 }, { 8 name : ‘text‘ 9 }])),10 proxy : new Ext.data.HttpProxy({11 url : Ext.getDom(‘root‘).value + ‘/helper/helper!shopCombox.do‘//資料查詢地址12 })13 });
建立資料來源,擷取相應後台查詢資料.大小由後台組裝完成
這是Java後台查詢部分,怎麼查就不詳述了.就記著怎麼分就好了
1 public void shopCombox() throws Exception { 2 List<ShopInfo> list = dataCacheUtil.getShopList();//調用DataCacheUtil模版方法查詢所有資料 3 4 if (list.size()==0) {//防止空值異常 5 printJson("{\"result\":[],\"totalCount\":0}" ); 6 return; 7 } 8 9 int start = Integer.parseInt(this.getRequest().getParameter("start")); //開始數 10 int limit = Integer.parseInt(this.getRequest().getParameter("limit")); //間隔數11 12 PrintWriter out = this.getResponse().getWriter();13 int totalcount = list.size();14 15 int end=start+limit;//防止尾頁越界16 if (list.size()<end) {17 end=list.size();18 }19 20 List<ShopStore> li=new ArrayList<ShopStore>();21 ShopStore s=null;22 23 for (int i = start; i < end; i++) {24 s=new ShopStore();25 s.setId(list.get(i).getId());26 s.setText(list.get(i).getShopname());27 li.add(s);28 }29 30 JSONArray jsonObject = JSONArray.fromObject(li);31 JSONObject reqObject = new JSONObject();32 reqObject.element("result", jsonObject);33 int totalPageCount = (int) Math.ceil((double) totalcount / 10);34 reqObject.element("totalPageCount", totalPageCount);35 reqObject.element("countPerPage", this.getRequest().getParameter("limit"));36 reqObject.element("totalCount", totalcount);// pages為總頁數。37 out.print(reqObject.toString());38 39 }
就像普通分頁一樣,其實ExtJs在這方面做的很不賴.每次調用方法就查詢start 到 start+limit的資料.然後返回給前台展示在下拉框裡.
1 xtype : ‘combo‘, 2 fieldLabel : ‘店鋪名稱‘, 3 valueField : ‘id‘, 4 displayField : ‘text‘, 5 hiddenName : ‘shopid‘, 6 name : ‘shopid‘, 7 store : shop_store, 8 //上面都是老生常談的玩意兒 9 mode:‘remote‘, //local是本機資料源 分頁要跟後台互動所以用remote10 triggerAction:‘all‘, //這個是做匹配用的 問題不大11 pageSize:10, //分頁量12 readOnly:true, //我版本3.1.1 這個屬性貌似沒用,但是放在這裡不影響啊13 listWidth:200 //下拉式清單的寬度 跟本來combobox的寬度沒關係
妥妥完美分頁啊~美中不足的是我忽然想到,要是有十萬個待選資料...- -# 還是奉勸客戶用手動輸入的吧.
葉落星辰 W
ExtJs_關於combobox的那些分頁二三事