Zend_Paginator 相关

Scrolling Style

All 显示所有页数 $pagination->count(),下拉列表时使用.
Elastic 显示页数 PageRange 随当前页数 CurrentPageNumber 的增加而增加.
Jumping 当当前页数 CurrentPageNumber % PageRange == 0 时,重新设置页数范围.
Sliding 当前页数 CurrentPageNumber 始终居中于页数范围 PageRange.

使用方法

//在视图的paginationControl中指定Scrolling Style为Sliding
$this->paginationControl($this->paginator, 'Sliding', 'my_pagination_control.phtml');
//在控制器中直接设置
Zend_Paginator::setDefaultScrollingStyle('Sliding');

自定义Scrolling Style

class Color_Paginator_ScrollingStyle_XXX extends Zend_Paginator_ScrollingStyle_Sliding
{
  public function getPages(Zend_Paginator $paginator, $pageRange = null)
  {
    //TODO
  }
}

Adapter

使用方法

  1. 控制器中
    //使用Factory返回Zend_Paginator实例
    $paginator = Zend_Paginator::factory($array);
    
    //Adapter::Array
    //$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));
    //Adapter::Zend_Db_Select
    //$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($DbSelect));
    //Adapter::Zend_Db_Table_Select
    //$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($DbTableSelect));
    //Adapter::Null
    //$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num_rows));
    //Adapter::Iterator
    //$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Iterator($iterator));
    
    $paginator->setCurrentPageNumber($this->_getParam('page'));//设置当前页数
               ->setItemCountPerPage(10);//设置每页显示条数
               ->setPageRange(10);       //设置PageRange
    
    //设置分页视图
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('paginator.phtml');
    
    //设置缓存(Zend_Cache_Core $cache)
    Zend_Paginator::setCache($cache);
    
    //开启自动缓存
    $paginator->setCacheEnabled();
    
    //Assign
    $this->view->paginator = $paginator;
    
  2. 视图页面调用
    <?php 
    if (count($this->paginator)):
    	foreach ($this->paginator as $item): 
    ?>
    <span><?php echo $item['username']?></span>
    <?php 
    	endforeach;
    endif;
    ?>
    <div class="paginator">
    <?php echo $this->paginationControl($this->paginator, 
                                          'Sliding', 
                                          //如果调用其它module中的视图,可使用array(partialname, module)
                                          'page/paginator.phtml',
                                          array('requestParams' => $this->requestParams)); ?>
    </div>
    

自定义Adapter

class Color_Paginator_Adapter_XXX implements Zend_Paginator_Adapter_Interface
{
  //Returns an collection of items for a page.
  public function getItems($offset, $itemCountPerPage)
  {
    //TODO
  }

  public function count()
  {
    //TODO 非必须实现
  }
}  

pagination control

分页控制参数

属性 类型 描述
first integer 第一页
firstItemNumber integer 当前页中数据项的第一条
firstPageInRange integer PageRange中第一项(Scrolling Style)
current integer 当前页数
currentItemCount integer 当前页显示条数
itemCountPerPage integer 每页显示最大条数(即每页条数)
last integer 最后页数
lastItemNumber integer 当前页中数据欺罔的最后一条
lastPageInRange integer PageRange中最后一项(Scrolling Style)
next integer 下一页数
pageCount integer 总页数
pageInRange array 页数范围(PageRange)的数组(Scrolling Style)
previous integer 上一页数
totalItemCount integer 总条数

搜索分页

<! --
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
-->
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    &lt; Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>
 
<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url(array('page' => $page)); ?>">
        <?php echo $page; ?>
    </a> |
  <?php else: ?>
    <?php echo $page; ?> |
  <?php endif; ?>
<?php endforeach; ?>
 
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a>
<?php else: ?>
  <span class="disabled">Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>

内容分页

<! --
  See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
-->
 
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
of <?php echo $this->totalItemCount; ?>
 
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
    First
  </a> |
<?php else: ?>
  <span class="disabled">First</span> |
<?php endif; ?>
 
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    &lt; Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>
 
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a> |
<?php else: ?>
  <span class="disabled">Next &gt;</span> |
<?php endif; ?>
 
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
    Last
  </a>
<?php else: ?>
  <span class="disabled">Last</span>
<?php endif; ?>
 
</div>
<?php endif; ?>

下拉列表分页

<?php if ($this->pageCount): ?>
<select id="paginationControl" size="1">
<?php foreach ($this->pagesInRange as $page): ?>
  <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
  <option value="<?php echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
    <?php echo $page; ?>
  </option>
<?php endforeach; ?>
</select>
<?php endif; ?>
 
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
$('paginationControl').change(function() {
    window.location = this.options[this.selectedIndex].value;
})
</script>

自定义分页

<?php 
if ($this->pageCount): 
$this->requestParams ? '' : $this->requestParams = array(); 
?>
<?php if (isset($this->previous) && ($this->current - $this->first >= count($this->pagesInRange)/2)): ?>
  <a href="<?php echo $this->url($this->requestParams + array('page' => $this->first)); ?>">
    <?php echo $this->first?>
  </a> 
  <a href="<?php echo $this->url($this->requestParams + array('page' => ($this->current-10 < 1 ? 1 : $this->current-10))); ?>">
    ...
  </a> 
<?php endif; ?>

<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url($this->requestParams + array('page' => $page)); ?>">
        <?php echo $page; ?>
    </a> 
  <?php else: ?>
    <span class="currentpage"><?php echo $page; ?></span> 
  <?php endif; ?>
<?php endforeach; ?>

<?php if (isset($this->next) && ($this->last - $this->current >= count($this->pagesInRange)/2)): ?>
  <a href="<?php echo $this->url($this->requestParams + array('page' => ($this->current+10>$this->last?$this->last:$this->current+10))); ?>">
    ...
  </a> 
  <a href="<?php echo $this->url($this->requestParams + array('page' => $this->last)); ?>">
    <?php echo $this->last?>
  </a>
<?php endif; ?>
<span class="paginatorinfo">[ <?php echo $this->firstItemNumber; ?> /<?php echo $this->totalItemCount; ?> ]</span>
<?php endif; ?>

自定义COUNT查询

$adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
//自定义COUNT查询(当在其它表存储了count计数时,可以更快捷的得到count)
$adapter->setRowCount(
    $db->select()->from('item_counts', array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count'))
);
$paginator = new Zend_Paginator($adapter);

表单参数传递

  1. 在控制器中把表单接收的参数赋值
    $this->view->requestParams = $input->getUnescaped();
    
  2. 在视图中传递给paginationControl视图助手
    $this->paginationControl($this->paginator, 'Sliding', 'paginator.phtml', array('requestParams' =>
          $this->requestParams));
    
  3. 在Paginator.phtml调用 $this->requestParams
    $this->requestParams ? '' : $this->requestParams = array(); 
    
    //方法一: 手动构建参数传递
    $url = '&'.http_build_query($this->requestParams);
    echo $this->url() . '?page=' . $this->first.$url;
    
    //方法二: 使用url助手建立标准路由链接
    echo $this->url($this->requestParams + array('page' => $this->first));