yii2.0下,JqPaginator与Pjax实现无刷新翻页

控制器部分

<?php
namespace backend\controllers;

use common\models\Common;
use Yii;
use yii\base\Controller;
use common\models\Student;
/**
 * Site controller
 */
class SiteController extends Controller
    public function actionTest()
    {
        $query=Student::find();
        $result=Common::createPage($query);
        return $this->render("test",['data'=>$result]);
    }
    public function actionContent()
    {
        if(Yii::$app->request->isPjax){
            $query=Student::find();
            $result=Common::createPage($query);
            return $this->renderPartial("info_content",['data'=>$result]);
        }else{
            //如果不是Pjax请求
            $this->redirect("/site/test?page=$_GET[page]");
        }
    }

Common部分同上一篇 http://www.cnblogs.com/water0729/p/5751763.html

主页面test.php

<?php

/* @var $this yii\web\View */
?>
<style>
    input[type="text"]{
        height:34px;
    }
    .row{
        margin-left: 0px;
    }
</style>
<link type="text/css" rel="stylesheet" href="http://cdn.staticfile.org/twitter-bootstrap/3.1.1/css/bootstrap.min.css"/>
<script type="text/javascript" src="/js/jquery-pjax-master/jquery-1.9.1.min.js"></script>//Pjax要求的jQuery版本为1.9
<script type="text/javascript" src="/js/jqPaginator.js"></script>
<script src="/js/jquery-pjax-master/jquery.pjax.js"></script>//引入Pjax文件
<script type="text/javascript">
    $(function(){
        $.jqPaginator('#pagination1', {
            totalPages: <?=$data['pagenum']?>,
            visiblePages: <?=$data['pagesize']?>,
            currentPage: 1,
            onPageChange: function (num, type) {
                if(type!='init'){
                    $.pjax({url: '/site/content?page='+num, container: '#load-table'});//改动的部分
                }
            }
        });
    })
</script>
<div id="content">
    <div id="content-header">
        <div id="breadcrumb"> <a href="/site/index" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
            <a href="#" class="tip-bottom">info</a></div>
    </div>
    <div id="w0" class="grid-view">
        <div id="load-table">
            <div class="summary">Showing <b><?=$data['begin']?>-<?=$data['end']?></b> of <b><?=$data['count']?></b> items.
            </div>
            <table class="table table-striped table-bordered load-table">
                <thead>
                <tr><th><a href="/site/info?sort=id" data-sort="id">Id</a></th><th>Username</th><th>Score</th><th>status</th><th>Photo</th><th class="action-column">操作</th></tr>
                </thead>
                <tbody>
                <?php foreach ($data['data'] as $k){
                    ?>
                    <tr data-key="132"><td><?=$k['id']?></td><td><?=$k['username']?></td><td><?=$k['score']?></td><td><?=$k['tag']?></td><td><img src="<?=$k['photo']?>" width="80" height="30" alt=""></td><td><a href="/site/update?id=132" title="Update" aria-label="Update" data-pjax="0"><span class="glyphicon glyphicon-pencil"></span></a><a href="javascript:void(0)" title="View" aria-label="View" data-pjax="0">
                                <span class="glyphicon glyphicon-eye-open" url="132"></span></a></td></tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
            <ul class="pagination" id="pagination1"></ul>
    </div>
</div>

待加载页面info_content.php

同上一篇http://www.cnblogs.com/water0729/p/5751763.html

posted @ 2016-08-09 13:06  demo_php  阅读(317)  评论(0编辑  收藏  举报