【问题标题】:Disabling pagination on certain views in CakePHP在 CakePHP 中禁用某些视图的分页
【发布时间】:2014-01-19 20:13:07
【问题描述】:

在我的使用 GoogleMapsv3 助手的 CakePHP 2.4 应用程序中,我有两个视图:分页列表视图和 Google 地图视图。我想在地图视图上显示所有标记,但是当我使用 foreach 循环将它们回显到 Google 地图中时,分页助手会介入并且一次只回显 20 个标记。

如何在我的地图视图上禁用 PaginatorHelper 以便显示所有结果?

我的 foreach 循环被分页助手打断了:

foreach ($posts as $post):
  $marker_options = array(
     'showWindow' => true,
     'windowText' => '<b>' . $post['Post']['title'] . '</b>' . '<br>' . $post['Post']['body'],
     'markerTitle' => '',
     'markerIcon' => 'http://labs.google.com/ridefinder/images/mm_20_green.png',
     'markerShadow' => 'http://labs.google.com/ridefinder/images/mm_20_greenshadow.png',
  );
 echo $this->GoogleMap->addMarker(
    "map_canvas",
    $post['Post']['id'], array(
        'latitude' => $post['Post']['lat'],
        'longitude' => $post['Post']['lng']
                                ), 
        $marker_options);


endforeach; ?>

【问题讨论】:

    标签: php cakephp pagination


    【解决方案1】:

    这应该发生在您的控制器中。

    假设您有一个变量,可以说您的网址是这样的:

    http://your.server.com/posts/index (for paginated posts)
    

    http://your.server.com/posts/index/map for your map view
    

    在您的控制器中,您应该执行以下操作:

    class PostsController extends AppController{
        function index($isMap = null){
          if($isMap == 'map'){
             $this->set('posts', $this->Post->find('all', ...));
          } else {
             $this->set('posts', $this->paginate());
        }
    }
    

    这种方式取决于您将返回完整或分页结果的 url。请记住,加载所有结果,特别是如果您添加某种关系可能会减慢页面速度。

    【讨论】:

    • 谢谢,您的回答为我指明了正确的方向。我不需要修改我的 index 方法,只需修复从 index one 复制的 map 方法。
    【解决方案2】:

    解决方案实际上非常简单。除非特别调用,否则分页器不会启动:我最初从 index 方法中复制了 map 方法,该方法也复制了分页器。愚蠢的错误。

    有效的方法:

    public function map() {
        $this->Post->recursive = 0;
        $this->set('posts', $this->Post->find('all'));
    }
    

    【讨论】:

    • 这样你不会有分页的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多