天天看点

基于thinkphp5的数组分页

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/10/25
 * Time: 21:04
 */

namespace app\admin\controller;
use think\Controller;
use think\paginator\driver\Bootstrap;
//分页
class Paginate extends  Controller
{
    public $url;  
    public function _initialize(){
       $a=request()->action();
       $c=request()->controller();
       $m=request()->module();
        $this->url='/'.$m.'/'.$c.'/'.$a;
    }

    /***
     * @param $data   传入的数组
     * @param int $listRow // 默认为config的参数
     *  @param string $name // 在模版循环的渲染的名称
     *   @param string $page // 在模版分页显示
     */

        public   function page($data,$name,$page,$listRow=''){
          if (!is_array($data)||empty($data)||empty($name)||empty($page)){
        return false;
    }
          if (empty($listRow)){
              $listRow= intval(config('paginate')['list_rows']);  //获取config里面的参数
          }
    
            $curPage = input('page') ? input('page') : 1;//当前第x页,有效值为:1,2,3,4,5...
            $showData = array_slice($data, ($curPage - 1)*$listRow, $listRow,true);
    
            $p = Bootstrap::make($showData, $listRow, $curPage, count($data), false, [
                'var_page' => 'page',
    
                'path'     => url($this->url),
                'fragment' => '',
            ]);
            $p->appends($_GET);
            $this->assign($name, $p);
            $this->assign($page, $p->render());
        }
    }
           

举个例子:

class Test{
public function lst(){
   $arr=需要分页的数据;
  $paginate = new Paginate();
    $p =   $paginate->page($arr,'adminRes','plistpage',2);
}
}
           

最后在模版里面使用就ok,里面的双大括号是在config定义的

<tbody>
	{{volist name='adminRes' id="vo"}}
		<tr class="text-c">
			<td><input type="checkbox" value="1" name=""></td>
			<td>{{$vo.id}}</td>
			<td>{{$vo.title}}</td>
			<td>{{$vo.phone}}</td>
			<td>{{$vo.email}}</td>
			<td>{{$vo.title}}</td>
			<td>{{$vo.login_time}}</td>
			<td class="td-status"> {{$vo.status|status}}</td>
			<td class="td-manage"><a style="text-decoration:none" onClick="admin_stop(this,'10001')" href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  title="停用"><i class="Hui-iconfont">&#xe631;</i></a> <a title="编辑" href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  onclick="admin_edit('管理员编辑','admin-add.html','1','800','500')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  onclick="admin_del(this,'1')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a></td>
		</tr>
		 {{/volist}}
	</tbody>
</table>
<div>{{$plistpage}}</div>