TP5 laypage分页+搜索

控制器

public function show()
{
    $key = input('key');
    $map = [];
    if($key&&$key!=="")
    {
        $map['title'] = ['like',"%" . $key . "%"];
    }

    //当前页码
    $Nowpage = input('get.page') ? input('get.page') : 1;
    $limits = 5;// 获取总条数
    $count = Db::name('information')->where($map)->where(['state'=>0])->count();//计算总页面
    $allpage = intval(ceil($count / $limits));
    $lists = Db::name('information')->where($map)->where(['state'=>0])->page($Nowpage, $limits)->order('id desc')->select();
    $first_names = array_column($lists, 'id');
    foreach ($lists as $k => $v) {
        $lists[$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
        $lists[$k]['modification'] = date('Y-m-d H:i:s', $v['modification']);
    }
    $this->assign('val', $key);
    $this->assign('Nowpage', $Nowpage); //当前页
    $this->assign('allpage', $allpage); //总页数
    if (input('get.page')) {
        return json($lists);
    }
    return $this->fetch('show',['lists'=>$lists]);
}
<form name="admin_list_sea" class="form-search" method="post" action="{:url('addd/show')}">
    <div class="col-sm-3">
        <div class="input-group">
            <input type="text" id="key" class="form-control" name="key" value="{$val}" placeholder="关键字" />
            <span class="input-group-btn">
                <button type="submit" class="btn btn-primary"><i class="fa fa-search">i> 搜索button>
            span>
        div>
    div>
form>
   //laypage分页
    Ajaxpage();
    function Ajaxpage(curr){
        var key=$('#key').val();
        $.getJSON('{:url("Addd/show")}', {page: curr || 1,key:key}, function(data){
            $(".spiner-example").css('display','none'); //数据加载完关闭动画
            if(data==''){
                $("#list-content").html('暂无数据');
            }else{
                var tpl = document.getElementById('list-template').innerHTML;
                laytpl(tpl).render(data, function(html){
                    document.getElementById('list-content').innerHTML = html;
                });
                laypage({
                    cont: $('#AjaxPage'),//容器。值支持id名、原生dom对象,jquery对象,
                    pages:'{$allpage}',//总页数
                    skip: true,//是否开启跳页
                    skin: '#1AB5B7',//分页组件颜色
                    curr: curr || 1,
                    groups: 3,//连续显示分页数
                    jump: function(obj, first){
                        if(!first){
                            Ajaxpage(obj.curr)
                        }
                        $('#allpage').html('第'+ obj.curr +'页,共'+ obj.pages +'页');
                    }
                });
            }
        });
    }
 
  
 
  
 
  

你可能感兴趣的:(TP5 laypage分页+搜索)