【问题标题】:How to set cakephp pagination records limit dynamic如何设置cakephp分页记录限制动态
【发布时间】:2014-05-31 04:42:32
【问题描述】:

到目前为止,我可以通过设置静态限制来加载结果。但是为了我的目的,我需要这个限制可以由一个函数来设置,该函数每次有一个新的调用以获得更多结果时都会生成一个介于 2-8 之间的数字(我已经做了那个函数,但不知道它在代码中的位置)

function random_cols () {
 $min_cols=2;
 $max_cols=8;
 $cols = array();
 $n = rand($min_cols,$max_cols);
 array_push($cols, $n);
 $var = 12 - $n;
 while ($var>0) {
  $n = rand($min_cols,$var);
  if ($var-$n<=1) {
   $n = $var;
  }
 $var = $var - $n;
 array_push($cols, $n);
 }
 return $cols;
}

$mylimit_arr = random_cols ();
$mylimit = count($mylimit_arr);

控制者:

public $paginate = array(
        'limit' => $mylimit,
        'order' => array('Post.id' => 'asc' ),      
);

public function index() {

    $posts = $this->paginate('Post');   
        $this->set('posts', $posts);

}

查看(使用无限滚动插件):

<div id="posts-list"> 
 <div class="post-item"></div>
</div>

<script>
  $(function(){
    var $container = $('#posts-list');

    $container.infinitescroll({
      navSelector  : '.next',    // selector for the paged navigation 
      nextSelector : '.next a',  // selector for the NEXT link (to page 2)
      itemSelector : '.post-item',     // selector for all items you'll retrieve
      debug         : true,
      dataType      : 'html',
      loading: {
          finishedMsg: 'No more posts to load. All Hail Star Wars God!',
          img: '<?php echo $this->webroot; ?>img/spinner.gif'
        }
      }
    );
  });

</script>

您可能想知道我为什么需要它,但这是因为设计和我显示结果的方式

【问题讨论】:

    标签: jquery ajax twitter-bootstrap cakephp pagination


    【解决方案1】:
    $this->paginate = array('limit' => $mylimit);
    

    成功了。

    您的控制器应如下所示:

    public function index() {
      $mylimit_arr = random_cols();
      $mylimit = count($mylimit_arr);
      $this->paginate = array(
        'order' => array('Post.id' => 'asc'),
        'limit' => $mylimit
      );
      $posts = $this->paginate('Post');   
    
      $this->set('posts', $posts);
    }
    

    【讨论】:

    • 您好,很抱歉,您能解释一下吗? :)
    猜你喜欢
    • 2022-10-18
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2016-12-21
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多