PHP異常處理:set_error_handler()函數的用法
732
2023-12-14
困惑了我一段時間的網頁分頁,今天特地整理了一下我完成不久的項目。下面我要分享下我這個項目的分頁代碼,前后端通吃。希望前輩多多指教。
一、效果圖
下面我先上網頁前臺和管理端的部分分頁效果圖,他們用的是一套代碼。
二、上代碼前的一些知識點
此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 title="'+info.caption+'" >'+info.caption+'</a></h3><dt><a title="'+info.caption+'" ><img src="http://www.gimoo.net/t/1901/<%=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 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部分的分頁也可以參考以上代碼。
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!