thinkphp分頁格式的完全自訂,直接輸入數字go到輸入數字頁

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

實現分頁效果如下:

以下標註紅色字型的為重點 找到檔案page.class.php在ThinkPHP/Library/Thinkpage.class.php並開啟檔案,複製函數show,在本檔案中黏貼並改成你自訂的函數名,在此我更改為show1,更改上一頁,下一頁,第一頁和最後一頁的邏輯輸出,將數字連結for迴圈刪除,其他不變,更改如下:
 public function show1() {        if(0 == $this->totalRows) return '';        /* 產生URL */        $this->parameter[$this->p] = '[PAGE]';        $this->url = U(ACTION_NAME, $this->parameter);        /* 計算分頁資訊 */        $this->totalPages = ceil($this->totalRows / $this->listRows); //總頁數        if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {            $this->nowPage = $this->totalPages;        }        /* 計算分頁臨時變數 */        $now_cool_page      = $this->rollPage/2;        $now_cool_page_ceil = ceil($now_cool_page);        $this->lastSuffix && $this->config['last'] = $this->totalPages;        //上一頁        $up_row  = $this->nowPage - 1;        $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . 上一頁 . '</a>' : '<a class="prev" href="javascript:(0);">' . 上一頁 . '</a>';        //下一頁        $down_row  = $this->nowPage + 1;        $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' .下一頁 . '</a>' : '<a class="next" href="javascript:(0);">' .下一頁 . '</a>';        //第一頁        $the_first=$this->nowPage > 1 ? '<a class="first" href="' . $this->url(1) . '">' . 首頁 . '</a>' : '<a class="first" href="javascript:(0);">' . 首頁 . '</a>';        //最後一頁        $the_end = $this->nowPage < $this->totalPages ? '<a class="end" href="' . $this->url($this->totalPages) . '">' .末頁. '</a>' : '<a class="end" href="javascript:(0);">' .末頁. '</a>';        //數字串連        $link_page = "";        //替換分頁內容        $page_str = str_replace(            array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),            array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first,$link_page, $the_end, $this->totalRows, $this->totalPages),            $this->config['theme']);        return "{$page_str}";    }

在定義的model中定義查詢資料庫中資料並分頁

public function getDocPageList($table,$where,$p, $pageSize,$order='docid desc'){        $list= M($table)->where($where)->order($order)                            ->page($p.','.$pageSize)                            ->select();        $count = M($table)->field('*')                        ->where($where)                        ->order($order)                        ->count();           $Page = new \Think\Page($count, $pageSize);        $module_name = '/' . MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;//此處為擷取路徑,模組名/控制器名/方法名       //$page便是上面效果的靜態代碼顯示
     $Page->setConfig('header','<p><span>%NOW_PAGE%</span>/<span title="%TOTAL_PAGE%" class="all">%TOTAL_PAGE%</span></p> <p>轉到<input type="text" id="page_num" class="txt_page" />頁</p> <a class="gopage" href="'.$module_name.'/p/">GO</a>'); $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'); $show = $Page->show1(); $result['pages'] = $show; $result['list'] = $list; $result['count'] = $count; return $result; }

以上分頁的css樣式如下

/*分頁*/.d_page{width: 700px;clear: both;margin:0px auto;padding-top:20px;}.d_page a,.d_page p{display:inline-block;margin:0px 8px;}.d_page a,.d_page input{border:1px solid #e6e6e6;padding:5px 10px;}.d_page input{width:40px}.d_page a:hover{background:#d57a22;border:1px solid #d57a22;color:#fff;}

 

在控制器中調用上面model下的函數,此處我以查詢user資料表為例:

//定義對象,引用控制器public function __construct() {        parent::__construct();        $this->docinfo_model = new \Common\Model\DocinfoModel();    }$result = $this->docinfo_model->getDocPageList('user',$where,$p, $pageSize,$order);$this->assign("doclist", $result['list']);$this->assign("count", $result['count']);$this->assign("pages", $result['pages']);$this->display();

在模板中輸出的時候直接輸出pages

<div>{$pages}</div>

使用jquery實現分頁的跳轉

 <script src='__PUBLIC__/js/jquery-1.8.3.min.js'></script>
<script> $(function(){ //分頁跳轉 $('#page_num').blur(function(){ var url=$('.gopage').attr('href'); var strs=url.substr(0); var value=this.value; var all=$('.all').attr("title"); var i=parseFloat(all);//將字元轉化為數字 if(value<=i && !isNaN(value) && value>0){ value=Math.ceil(value); }else{ value=''; } var nuwurl=strs+value; $(".gopage").attr("href",nuwurl); }) }); </script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.