【问题标题】:Ajax Pagination backward move in cakephp 3Cakephp 3中的Ajax分页向后移动
【发布时间】:2016-09-27 07:54:02
【问题描述】:

我在 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();

谢谢

【问题讨论】:

  • 在 mysql 中,您可以使用 LIMIT start,end (LIMIT 100,10) 等限制,它会在跳过 100 条记录后获取 10 条记录。 cakephp 中应该有相同的选项用于分页
  • @Minesh Patel 做了一些研发,找到了一些最好的和更少的代码来在 cakephp 3X 中制作这种类型的分页,我认为这是最好的分页。如果有人想要我可以发布代码。
  • 是的,您可以发布您的答案,以帮助其他人
  • 好的,我一定会发布我的答案。 @Minesh Patel

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


【解决方案1】:

你的控制器动作代码应该是

$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;
    });
});

【讨论】:

  • 每次点击都会触发这个 ajax 吗?我的意思是每次点击任何索引时都会获取数据。还是在页面加载时首先加载所有内容,我们只是逐个移动索引?
  • 是的,每次点击都会触发 ajax...并且每次都会覆盖 #pagination_list_container 块,并在分页链接和排序链接上绑定点击事件,我已经对 javascript 代码进行了更改,因为我错过了选择器中的 # .. ;P
猜你喜欢
  • 1970-01-01
  • 2016-12-29
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多