成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? Ajax分頁插件Pagination從前臺jQuery到后端java總結
全站頭部文字 我要出現在這里
Ajax分頁插件Pagination從前臺jQuery到后端java總結 647 2024-02-24   

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

一、效果圖

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

查看圖片

查看圖片

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

此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部分的分頁也可以參考以上代碼。



主站蜘蛛池模板: 郑柔美个人简介| 安徽公共频道| 次元舰队| 虐猫视频哪里可以看| 庞敏| 李修蒙出生年月| 安装暖气片电话| 美女xxx69爽爽免费观妞| 南来北往电视剧剧情介绍| 小敏家| 褚阳| 尼康相机型号大全和价格| 卫平| 曹东| 用药错误应急预案演练脚本| 羞羞的影评| 延禧| 救急战队| 团结力量歌词大全图片| 小学生抽烟| 周超个人资料简介| nhk新闻| 4438x五月天| 阿斯美治疗咳嗽效果服法用量| 迷失之城 电影| 快乐宝贝电影免费观看| 永恒族 电影| 好看的抖音头像| 情事:秘密情事| 电商运营计划| 廖明| 裸色亮片| 四级词汇电子版| 应昊茗| 护航 电影| 精灵变粤语| 拔萝卜短剧| 伴娘| 二年级100个词语| 妈妈的朋友泡妞| 潜伏温子仁|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103