国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Ajax分頁插件Pagination從前臺jQuery到后端java總結

2019-11-20 09:26:05
字體:
來源:轉載
供稿:網友

困惑了我一段時間的網頁分頁,今天特地整理了一下我完成不久的項目。下面我要分享下我這個項目的分頁代碼,前后端通吃。希望前輩多多指教。

一、效果圖

下面我先上網頁前臺和管理端的部分分頁效果圖,他們用的是一套代碼。

二、上代碼前的一些知識點

此jQuery插件為Ajax分頁插件,一次性加載,故分頁切換時無刷新與延遲,如果數據量較大不建議用此方法,因為加載會比較慢。

三、前臺代碼部分

var pageSize =6; //每頁顯示多少條記錄var total; //總共多少記錄 $(function() { Init(0); //注意參數,初始頁面默認傳到后臺的參數,第一頁是0;  $("#Pagination").pagination(total, { //total不能少  callback: PageCallback,  prev_text: '上一頁',  next_text: '下一頁',  items_per_page: pageSize,  num_display_entries: 4, //連續分頁主體部分顯示的分頁條目數 num_edge_entries: 1, //兩側顯示的首尾分頁的條目數  });  function PageCallback(index, jq) { //前一個表示您當前點擊的那個分頁的頁數索引值,后一個參數表示裝載容器。  Init(index);  } });  function Init(pageIndex){ //這個參數就是點擊的那個分頁的頁數索引值,第一頁為0,上面提到了,下面這部分就是AJAX傳值了。 $.ajax({ type: "post", url:"../getContentPaixuServ?Cat="+str+"&rows="+pageSize+"&page="+pageIndex, async: false, dataType: "json", success: function (data) { $(".neirong").empty();/* total = data.total; */ var array = data.rows; for(var i=0;i<array.length;i++){ var info=array[i];  if(info.refPic != null){ $(".neirong").append('<dl><h3><a href="'+info.CntURL+'?ContentId='+info.contentId+'" title="'+info.caption+'" >'+info.caption+'</a></h3><dt><a href="sjjm.jsp?ContentId='+info.contentId+'" title="'+info.caption+'" ><img src="<%=basePathPic%>'+info.refPic+'" alt="'+info.caption+' width="150" height="95""></a></dt> <dd class="shortdd">'+info.text+'</dd><span>發布時間:'+info.createDate+'</span></dl>')  }else{ $(".neirong").append('<dl ><h3><a href="'+info.CntURL+'?ContentId='+info.contentId+'" title="'+info.caption+'" >'+info.caption+'</a></h3><dd class="shortdd">'+info.text+'</dd><span>發布時間:'+info.createDate+'</span></dl>'); }; }  }, error: function () { alert("請求超時,請重試!"); } }); };

四、后臺部分(java)
我用的是MVC 3層模型

servlet部分: (可以跳過)

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //獲取分頁參數 String p=request.getParameter("page"); //當前第幾頁(點擊獲取) int page=Integer.parseInt(p);  String row=request.getParameter("rows"); //每頁顯示多少條記錄 int rows=Integer.parseInt(row);  String s=request.getParameter("Cat"); //欄目ID int indexId=Integer.parseInt(s); JSONObject object=(new ContentService()).getContentPaiXuById(indexId, page, rows);  out.print(object); out.flush(); out.close(); }

Service部分:(可以跳過)

public JSONObject getContentPaiXuById(int indexId, int page, int rows) { JSONArray array=new JSONArray(); List<Content>contentlist1=(new ContentDao()).selectIndexById(indexId); List<Content>contentlist=paginationContent(contentlist1,page,rows); for(Content content:contentlist){ JSONObject object=new JSONObject(); object.put("contentId", content.getContentId()); object.put("caption", content.getCaption()); object.put("createDate", content.getCreateDate()); object.put("times", String.valueOf(content.getTimes())); object.put("source", content.getSource()); object.put("text", content.getText()); object.put("pic", content.getPic()); object.put("refPic", content.getRefPic()); object.put("hot", content.getHot()); object.put("userId", content.getAuthorId().getUserId()); int id = content.getAuthorId().getUserId(); String ShowName = (new UserService()).selectUserById(id).getString("ShowName"); object.put("showName", ShowName); array.add(object);  } JSONObject obj=new JSONObject(); obj.put("total", contentlist1.size()); obj.put("rows", array); return obj; }

獲取出每頁的的起止id(這部分是重點),同樣寫在Service中,比如說假設一頁有6條內容,那么第一頁的id是從1到6,第二頁的id是從7到12,以此類推

//獲取出每頁的內容 從哪個ID開始到哪個ID結束。 private List<Content> paginationContent(List<Content> list,int page,int rows){ List<Content>small=new ArrayList<Content>(); int beginIndex=rows*page; //rows是每頁顯示的內容數,page就是我前面強調多次的點擊的分頁的頁數的索引值,第一頁為0,這樣子下面就好理解了! System.out.println(beginIndex); int endIndex; if(rows*(page+1)>list.size()){  endIndex=list.size();  } else{ endIndex=rows*(page+1); } for(int i=beginIndex;i<endIndex;i++){  small.add(list.get(i));  }  return small; }

Dao層: (可以跳過)

public List selectIndexById(int indexId){ List<Content>list=new ArrayList<Content>(); try{ conn = DBConn.getCon(); String sql = "select * from T_Content,T_User where T_Content.AuthorId = T_User.UserId and CatlogId=? order by CreateDate desc"; pstm = conn.prepareStatement(sql); pstm.setInt(1, indexId); rs = pstm.executeQuery(); SimpleDateFormat ff=new SimpleDateFormat("yyyy年MM月dd日 hh時mm分"); while(rs.next()){ Content content = new Content(); content.setContentId(rs.getInt("ContentId"));  content.setCaption(rs.getString("Caption"));  content.setCreateDate(f.format(rs.getTimestamp("CreateDate")));  content.setTimes(rs.getInt("Times"));  content.setSource(rs.getString("Source"));  content.setText(rs.getString("Text"));  content.setPic(rs.getString("Pic"));  content.setRefPic(rs.getString("RefPic"));  content.setHot(rs.getInt("Hot"));  User user = new User();  user.setUserId(rs.getInt("UserId"));  content.setAuthorId(user);  Catlog catlog = new Catlog(); //CntURL待開發  catlog.setCatlogId(rs.getInt("CatlogId"));  content.setCatlog(catlog);  list.add(content); } }catch(Exception e){ e.printStackTrace(); }finally{ DBConn.closeDB(conn, pstm, rs); } return list; }

精彩專題分享:jquery分頁功能操作  JavaScript分頁功能操作

以上就是網頁所實現的分頁代碼,easy-ui部分的分頁也可以參考以上代碼。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六枝特区| 上杭县| 多伦县| 玉林市| 临清市| 育儿| 朔州市| 通海县| 镇康县| 临海市| 庐江县| 天长市| 永和县| 旬阳县| 成武县| 客服| 元朗区| 开原市| 南华县| 阿尔山市| 鄂尔多斯市| 诏安县| 夹江县| 洛浦县| 隆回县| 崇礼县| 新乐市| 温州市| 米泉市| 普兰店市| 舒兰市| 博客| 禹城市| 巴南区| 尉氏县| 吴桥县| 松原市| 石景山区| 循化| 雷波县| 开阳县|