jquery实现分页

来源:互联网 发布:两少一宽废除知乎 编辑:程序博客网 时间:2024/06/13 11:20

jquery分页

function dividePage(elem, page, totalPage) {    if (page < 1) {        page = 1;    }    if (totalPage < 1) {        totalPage = 1;    }    var str = '';    if (page > 1) {        str += '<a data-page="1">首页</a>';        str += '<a data-page="' + (page - 1) + '">上一页</a>';    }    var showPage = 5;    var startPage = 1;    var endPage = totalPage;    var pageCnt = Math.floor(showPage / 2);    if (totalPage > showPage) {        if (page > pageCnt + 1) {            var tmpCnt = totalPage - page;            if (tmpCnt < pageCnt) {                startPage = (page - showPage) + tmpCnt + 1;            } else {                startPage = page - pageCnt;            }        }        endPage = startPage + showPage - 1;    }    for (var i=startPage; i<=endPage; i++) {        if (i == page) {            str += '<span>' + i + '</span>';        } else {            str += '<a data-page="' + i + '">' + i + '</a>';        }    }    if (page < totalPage) {        str += '<a data-page="' + (page + 1) + '">下一页</a>';        str += '<a data-page="' + totalPage + '">尾页</a>';    }    if(totalPage>1){        elem.html(str);    }}

$(document).on('click', '.reply_body li .del_reply', function() {    var _this = $(this);    OC.dialogs.confirm(t('topic', 'Are you sure you want to delete it?'), t('core', 'Warning'), function(res) {        if (res) {            var type = $('#req_type').val();            var li = _this.parent().parent();            var ul = _this.parent().parent().parent();            var replyBody = ul.parent();            var div = replyBody.find('.divide');            var p = replyBody.find('.edit_area>p');            var contentId = ul.data('id');            var replyId = li.data('id');            $.post(                OC.filePath("topic", "ajax", "topic_reply.php"),                {userType:"createUser", req: 'delData', type: type, replyId: replyId, contentId: contentId},                function (json) {                    if (json.result == 'OK') {                        li.remove();                        div.html("");//tip:div can not be removed, this node userd for function dividepage.                        showReply(ul, json.data);                        dividePage(div, json.page, json.totalPage);                        p.html('');                        p.next().hide();                        var unRead = json.total;                        replyBody.parent().find('.time .reply').data('msg', '回复(' + unRead + ')');                        OC.dialogs.alert(t('topic', 'Delete success'), t('topic', 'Information'));                    } else if(json.result == null) {//for the last delete reply action                        OC.dialogs.alert(t('topic', 'Delete success'), t('topic', 'Information'));                        $('.user_content .time .reply').text('回复');                        li.remove();                        replyBody.slideUp();                    } else {                        OC.dialogs.alert(t('topic', 'Delete failed'), t('core', 'Error'));                    }                }            );        }    },true);});


topic_reply.php

if ($_POST['req'] == 'getData') {    if (isset($_POST['contentId'])) {        $contentId = $_POST['contentId'];        $limit = OC_Default::TOPIC_REPLY_LIMIT;        $offset = 0;        $total = OC_Topic::getReplyCnt($table, $contentId);        $page = 1;        $totalPage = ceil($total / $limit);        if (isset($_POST['page'])) {            $page = (int)$_POST['page'];            if ($page < 1) {                $page = 1;            }            if ($page > $totalPage) {                $page = $totalPage;            }            $offset = ($page - 1) * $limit;        }        $data = OC_Topic::getContentReply($table, $contentId, $offset);        if ($data) {            $result = array('result' => 'OK', 'totalPage' => $totalPage, 'page' => $page, 'data' => $data);        }    }}

PHP分页

/** * 分页 */public static function dividePage($currentPage, $totalPage, $reqUrl) {   if (!$currentPage) {      $currentPage = 1;   }   if (!$totalPage) {      $totalPage = 1;   }   $sign = '/';   $str = '<div style="text-align: center"><div class="page" style="display: inline-block">';   if ($currentPage == 1) {      $str .= '<a href="#" class="previous" style="color: #cecece">首页</a>';      $str .= '<a href="#" class="previous" style="color: #cecece">上一页</a>';   } else {      $str .= '<a href="' . $reqUrl . $sign . '1" class="previous">首页</a>';      $str .= '<a href="' . $reqUrl . $sign . ($currentPage - 1) . '" class="previous">上一页</a>';   }   $showPage = 5; //一页显示的分页数量   $startPage = 1; //开始页   $endPage = $totalPage; //结束页   $pageCnt = floor($showPage / 2);   if ($totalPage > $showPage) {      if ($currentPage > $pageCnt + 1) {         $tmpCnt = $totalPage - $currentPage;         if ($tmpCnt < $pageCnt) {            $startPage = ($currentPage - $showPage) + $tmpCnt + 1;         } else {            $startPage = $currentPage - $pageCnt;         }      }      $endPage = $startPage + $showPage - 1;   }   for ($i=$startPage; $i<=$endPage; $i++) {      if ($i == $currentPage) {         $str .= '<a href="#" class="first">' . $i . '</a>';      } else {         $str .= '<a href="' . $reqUrl . $sign . $i . '">' . $i . '</a>';      }   }   if ($currentPage == $totalPage) {      $str .= '<a href="#" class="next" style="color: #cecece">下一页</a>';      $str .= '<a href="#" class="next" style="color: #cecece">尾页</a>';   } else {      $str .= '<a href="' . $reqUrl . $sign . ($currentPage + 1) . '" class="next">下一页</a>';      $str .= '<a href="' . $reqUrl . $sign . $totalPage . '" class="next">尾页</a>';   }   $str .= '<a href="#" class="count"><span>' . $totalPage . '</span></a></div></div>';   echo $str;}

原创粉丝点击