列表也其實也很簡單,說白了直接在數據庫取出數據,而後直接遍歷到模版便可,首先來修改Admin控制器的add方法:javascript
而後到add模板頁裏使用volist進行遍歷:php
而後測試下看看數據是否成功被取出:css
而後回到控制器寫他的分頁:html
<?php namespace Admin\Controller; use Think\Controller; class AdminController extends Controller { public function lists(){ $mod = M('admin'); $totalRows = $mod->count(); //建立分頁對象時,分頁對象須要總記錄數和分頁條數 $page = new \Think\Page($totalRows,2); $page -> rollPage =3; //分頁列表上顯示多少條 $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'); $page->setConfig('first','首頁'); $page->setConfig('prev','上一頁'); $page->setConfig('next','下一頁'); $page->setConfig('last','尾頁'); $pageHtml = $page -> show();//生成分頁的鏈接誒效果(分頁工具條的html代碼) $this -> assign('pageHtml',$pageHtml);//分配分頁欄到模版 //2.查詢出當前頁面的列表數據 $data = $mod ->order('id desc')->page(I('get.p',1),$page->listRows)->select(); $this -> assign('data',$data); $this -> display(); } public function add(){ $mod = D("admin"); if(IS_POST){ $data['username'] = I('username'); $data['password'] = I('pass'); $data['passer'] = I('passer'); if($data['password'] == $data['passer']){ $data['password'] = md5($data['password']); if($mod->create($data)){ if($mod->add($data)){ $this -> success('管理員添加成功'); }else{ $this->error('管理員添加失敗'); } }else{ $this->error($mod->getError()); } }else{ $this->error('確認密碼錯誤'); } return;//這裏的return主要是爲了防止跳轉 } $this -> display(); } public function edit(){ $this -> display(); } public function del(){ $this -> display(); } }
而後直接在模板頁直接將分頁遍歷出來:java
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>後臺管理</title> <link rel="stylesheet" type="text/css" href="__PUBLIC__/Admin/css/common.css"/> <link rel="stylesheet" type="text/css" href="__PUBLIC__/Admin/css/main.css"/> <script type="text/javascript" src="__PUBLIC__/Admin/js/libs/modernizr.min.js"></script> </head> <body> <div class="topbar-wrap white"> <div class="topbar-inner clearfix"> <div class="topbar-logo-wrap clearfix"> <h1 class="topbar-logo none"><a href="index.html" class="navbar-brand">後臺管理</a></h1> <ul class="navbar-list clearfix"> <li><a class="on" href="index.html">首頁</a></li> <li><a href="#" target="_blank">網站首頁</a></li> </ul> </div> <div class="top-info-wrap"> <ul class="top-info-list clearfix"> <li><a href="http://www.jscss.me">管理員</a></li> <li><a href="http://www.jscss.me">修改密碼</a></li> <li><a href="http://www.jscss.me">退出</a></li> </ul> </div> </div> </div> <div class="container clearfix"> <div class="sidebar-wrap"> <div class="sidebar-title"> <h1>菜單</h1> </div> <div class="sidebar-content"> <ul class="sidebar-list"> <li> <a href="#"><i class="icon-font"></i>管理員管理</a> <ul class="sub-menu"> <li><a href="__MODULE__/Admin/lists"><i class="icon-font"></i>管理員列表</a></li> <li><a href="__MODULE__/Admin/add"><i class="icon-font"></i>添加管理員</a></li> </ul> </li> <li> <a href="#"><i class="icon-font"></i>經常使用操做</a> <ul class="sub-menu"> <li><a href="__MODULE__/Anli/index"><i class="icon-font"></i>做品管理</a></li> <li><a href="design.html"><i class="icon-font"></i>博文管理</a></li> <li><a href="design.html"><i class="icon-font"></i>分類管理</a></li> <li><a href="design.html"><i class="icon-font"></i>留言管理</a></li> <li><a href="design.html"><i class="icon-font"></i>評論管理</a></li> <li><a href="design.html"><i class="icon-font"></i>友情連接</a></li> <li><a href="design.html"><i class="icon-font"></i>廣告管理</a></li> </ul> </li> <li> <a href="#"><i class="icon-font"></i>系統管理</a> <ul class="sub-menu"> <li><a href="system.html"><i class="icon-font"></i>系統設置</a></li> <li><a href="system.html"><i class="icon-font"></i>清理緩存</a></li> <li><a href="system.html"><i class="icon-font"></i>數據備份</a></li> <li><a href="system.html"><i class="icon-font"></i>數據還原</a></li> </ul> </li> </ul> </div> </div> <!--/sidebar--> <div class="main-wrap"> <div class="crumb-wrap"> <div class="crumb-list"><i class="icon-font"></i><a href="__MODULE__/Index/index">首頁</a><span class="crumb-step">></span><span class="crumb-name">管理員管理</span></div> </div> <div class="result-wrap"> <form name="myform" id="myform" method="post"> <div class="result-title"> <div class="result-list"> <a href="__MODULE__/admin/add"><i class="icon-font"></i>新增管理員</a> <a id="batchDel" href="javascript:void(0)"><i class="icon-font"></i>批量刪除</a> </div> </div> <div class="result-content"> <table class="result-tab" width="100%"> <tr> <th class="tc" width="5%"><input class="allChoose" name="" type="checkbox"></th> <th>ID</th> <th>標題</th> <th>操做</th> </tr> <volist name="data" id="vo"> <tr> <td class="tc"><input name="id[]" value="59" type="checkbox"></td> <td>{$vo.id}</td> <td title="">{$vo.username}</td> <td> <a class="link-update" href="__MODULE__/admin/edit/{$vo.id}">修改</a> <a class="link-del" href="#">刪除</a> </td> </tr> </volist> </table> <div class="list-page"> {$pageHtml} 2 條 1/1 頁</div> </div> </form> </div> </div> <!--/main--> </div> </body> </html>
可是沒辦法發現了一個BUG,尾頁無法顯示,那麼就想辦法去解決掉他。。數據庫
咱們來到在shop\ThinkPHP\Library\Think\Page.class.php 對她進行修改:緩存
咱們找到以下代碼:ide
public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
咱們將他修改爲以下,BUG便可解決,TP3.2.3一直都有這BUG。。。。工具
public function setConfig($name,$value) { if(isset($this->config[$name])) { $this->config[$name] = $value; if($name == 'last'){ $this->lastSuffix = false; } } }
這樣他便可完美解決掉了,直接看效果:post