• tp5+jquery实现搜索后分页


    控制器代码:

    public function userLists(){
            $model = new User();
            $word = input('get.word');
            //接收当前页
            $page = input('get.page');
            $cpage = empty($page) ? 1 : $page;
            //设置每页显示的条数
            $length = 3;
            //获取总条数
            if(empty($word)){
                $count = $model->getCount();
            }else{
                $count = $model->getCount2($word);
            }
    
            //求出总页数
            $num_page = ceil($count/$length);
            //偏移量
            $limit = ($cpage-1)*$length;
            //查询
            if(empty($word)){
                $data = $model->getLists($limit,$length);
            }else{
                $data = $model->getLists2($limit,$length,$word);
            }
    
            $arr['word'] = empty($word) ? "" : $word;
            $arr['home'] = 1;
            $arr['prev'] = $cpage-1<=1 ? 1 : $cpage-1;
            $arr['next'] = $cpage+1>=$num_page ? $num_page : $cpage+1;
            $arr['last'] = $num_page;
            $arr['list'] = $data;
            return view('userLists',['arr'=>$arr]);
        }

    模型层代码:

    //总条数
        public function getCount(){
            return $this->count();
        }
    
        public function getCount2($word){
            return $this->where('username','like',"%$word%")->count();
        }
        //分页查询
        public function getLists($limit,$length){
            return $this->limit($limit,$length)->select();
        }
        public function getLists2($limit,$length,$word){
            return $this->where('username','like',"%$word%")->limit($limit,$length)->select();
        }

    静态页代码:

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
    <input type="text" name="word" value="{$arr.word}">
    <input type="button" value="搜索" onclick="page(1)">
    <table border="1">
        <tr>
            <td>主键</td>
            <td>用户名</td>
            <td>手机</td>
            <td>邮箱</td>
            <td>地址</td>
        </tr>
    
        {volist name="arr.list" id="v"}
            <tr>
                <td>{$v.id}</td>
                <td>{$v.username}</td>
                <td>{$v.tel}</td>
                <td>{$v.email}</td>
                <td>{$v.address}</td>
            </tr>
        {/volist}
    </table>
    
    <a href="javascript:void(0)" onclick="page({$arr.home})">首页</a>
    <a href="javascript:void(0)" onclick="page({$arr.prev})">上一页</a>
    <a href="javascript:void(0)" onclick="page({$arr.next})">下一页</a>
    <a href="javascript:void(0)" onclick="page({$arr.last})">尾页</a>
    </body>
    </html>
    <script src="__STATIC__/js/jq.js"></script>
    <script>
        function page(obj) {
            var word = $("input[name='word']").val();
            $.get("{:url('Testone/userLists')}?page="+obj+"&word="+word,function (data) {
                $("body").html(data);
            })
        }
    </script>
  • 相关阅读:
    Asp.Net WebApi核心对象解析(一)
    关于.NET参数传递方式的思考
    关于.NET异常处理的思考
    吃瓜群众的三言两语,想听的就进来看看吧!
    C#文件安全管理解析
    开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
    免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
    免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)
    免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)
    免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
  • 原文地址:https://www.cnblogs.com/jiangshiguo/p/11189934.html
Copyright © 2020-2023  润新知