Ajax Pagination在cakephp中向后移动3

时间:2016-09-27 07:54:03

标签: php ajax pagination cakephp-3.x cakephp-3.2

我在cakephp 3.2中进行ajax分页

通过获取最后一个ID,我已经完成了分页前移的代码。

如果我想倒退,我知道,分页将不起作用。

我怎样才能以正确的方式做到这一点,以便它既适用于双向,也可以直接点击任何分页索引。

下面我附上了一些我的代码,这些代码只适用于分页的前移。

我知道代码不能用于向后移动。

我该怎么做?

      ///////////////////////////////////PAGINATION STARTS HERE/////////////////////////////////////////////////
        if(isset($_POST["page"])){
        $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
        if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
    }else{
        $page_number = 1; //if there's no page number, set it to 1
    }
        $item_per_page=5;
       $get_total_rows = $this->Orders->find('all')->where($condition)->count(); //hold total records in variable
        $total_pages = ceil($get_total_rows/$item_per_page);
       $page_position = (($page_number-1) * $item_per_page);
          if($page_number>1)
          {
           $condition[] = ['Orders.id >' => $_POST["lastId"]];
          }//this one fetch all list greater than last id 


$Lists = $this->Orders->find('all')->where($condition)->order(['Orders.id' => 'ASC'])->limit($item_per_page)->toArray();

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

您的控制器操作代码应为

$this->paginate = [
    'order'=>[
        'field_name'=>'desc'
    ]  
];
$condition = [];
$query = $this->YourModel->find()->where($conditions);
$this->set('records', $this->paginate($query));

在视图中你的代码应仅用于列出部分,我不知道你的HTML结构是什么,但你可以遵循这一点,不要忘记你父表中的id=pagination_list_container和分页链接代码。

<div class="panel-body" id="pagination_list_container">
        <div class="inner-spacer">
            <table class="table table-striped table-hover margin-0px">
                <thead>
                    <tr>
                        <th><?php echo $this->Paginator->sort('field_1', 'Column 1') ?></th>
                        <th><?php echo $this->Paginator->sort('field_2', 'Column_2') ?></th>
                        <th><?php echo $this->Paginator->sort('field_3', 'Column_3') ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if (empty($records->toArray())) {
                        ?>
                        <tr><td colspan="100%" class="text-danger text-center">No record found</td></tr>
                        <?php
                    } else {
                        foreach ($records as $record):
                            ?>
                            <tr>
                                <td><?php echo $record->field_1 ?></td>
                                <td><?php echo $record->field_2; ?></td>
                                <td><?php echo $record->field_3; ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php } ?>
                </tbody>
            </table>
        </div>
        <div class="row">
            <div class="col-md-6">
                <ul class="pagination">
                    <?php
                    $this->Paginator->templates([
                        'current' => '<li class="active"><a>{{text}}</a></li>',
                        'number' => '<li><a href="{{url}}">{{text}}</a></li>'
                    ]);
                    echo $this->Paginator->prev('«');
                    echo $this->Paginator->numbers();
                    echo $this->Paginator->next('»');
                    ?>
                </ul>
            </div>
            <div class="col-md-6 text-right">
                <div class="mt30">
                    <?php
                    echo $this->Paginator->counter(
                            'Page {{page}} of {{pages}}, showing {{start}} to {{end}} of {{count}}'
                    );
                    ?>
                </div>
            </div>
        </div>
    </div>

在视图中制作AJAX行为

您必须将ajax行为的一些Javascript事件应用到#pagination_list_container

$(document).ready(function(){
    $("document").on('click','#pagination_list_container .pagination li a, #pagination_list_container table th > a', function(e){
      e.preventDefault();
      var link= $(this).attr('href');
      if(link!="")
      {
            $("#pagination_list_container").load(link+ "#pagination_list_container", function(){
                console.log("data loaded");
            })
      }
      return false;
    });
});