2010-02-19 27 views
5

Bạn bè,Phân trang trong Zend

Tôi muốn tạo phân trang trong Zend Framework. Tôi mới đến ZF.

index.phtml được đưa ra dưới đây

<table> 
<tr> 
    <th>Name</th> 
    <th>Quantity</th> 
    <th>&nbsp;</th> 
</tr> 
<?php foreach($this->orders as $order) : ?> 
<tr> 
    <td><?php echo $this->escape($order->name);?></td> 
    <td><?php echo $this->escape($order->quantity);?></td> 
    <td> 
     <a href="<?php echo $this->url(array('controller'=>'index','action'=>'edit', 'id'=>$order->id));?>">Edit</a> 
     <a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'delete', 'id'=>$order->id));?>">Delete</a> 
    </td> 
</tr> 
<?php endforeach; ?> 
</table> 
<p><a href="<?php echo $this->url(array('controller'=>'index','action'=>'add'));?>">Add new album</a></p> 

điều khiển chỉ số của tôi là dưới

class IndexController extends Zend_Controller_Action 
{ 
    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() 
    { 
     $this->view->title = "My Orders"; 
     $this->view->headTitle($this->view->title, 'PREPEND'); 
     $orders = new Model_DbTable_Orders(); 
     $this->view->orders = $orders->fetchAll(); 
    } 
} 
+0

Tôi có một câu trả lời và tôi đã cố gắng và nhận được chỉ output.I một phần là tạo ra một 'pagination.phtml' và lưu trữ nó trong 'views/scripts /' và thêm dòng được đưa ra trong chỉ số của tôi .phtml '$ this-> paginationControl ($ this-> paginator,' Sliding ',' pagination.phtml ') ;. Nhưng chỉ có 10 mục đầu tiên được hiển thị, không hiển thị tiếp theo - các liên kết trước. Làm thế nào để giải quyết nó ?? – Rakes

Trả lời

17

Tôi cố gắng giúp bạn dựa trên những gì tôi có trong dự án ZF của tôi.

Vì vậy, trong lớp học của bạn Model_DbTable_Order bạn có thể xác định phương thức được gọi là ví dụ: getOnePageOfOrderEntries() như sau:

/** 
* Return one page of order entries 
* 
* @param int $page page number 
* @return Zend_Paginator Zend_Paginator 
*/ 
public function getOnePageOfOrderEntries($page=1) { 

    $query = $this->select(); 
    $paginator = new Zend_Paginator(
      new Zend_Paginator_Adapter_DbTableSelect($query) 
    ); 
    $paginator->setItemCountPerPage(100); 
    $paginator->setCurrentPageNumber($page); 
    return $paginator; 
} 

Thần trong indexAction bạn có thể có một cái gì đó như thế này:

public function indexAction() 
{ 
    $this->view->title = "My Orders"; 
    $this->view->headTitle($this->view->title, 'PREPEND'); 
    $orders = new Model_DbTable_Orders(); 
    //$this->view->orders = $orders->fetchAll(); 

    $page = $this->_request->getParam('page'); 
    if (empty($page)) { $page = 1; } 

    $paginator = $orders->getOnePageOfOrderEntries($page); 
    $this->view->paginator = $paginator; 

} 

Trong index.phtml của bạn xem bạn có thể có một cái gì đó tương tự như sau:

<?php if (count($this->paginator)): ?> 
    <table> 
    <tr> 
     <th>Name</th> 
     <th>Quantity</th> 
     <th>&nbsp;</th> 
    </tr> 
      <?php foreach($this->paginator as $order): ?> 

    <tr> 

     <td><?php echo $order->name;?></td> 
     <td><?php echo $order->quantity;?></td> 
     <td><!-- And the rest what you want --></td>   
    </tr> 

    <?php endforeach; ?> 
</table> 
<?php endif; ?> 
    <?php echo $this->paginationControl($this->paginator, 
    'Sliding','partial/my_pagination_control.phtml'); ?> 

Trường hợp my_pagination_control.phtml như sau (đây chỉ là sao chép-dán những gì tôi có và nó là từ các tài liệu tham khảo ZF quide):

<?php if ($this->pageCount): ?> 
    <div class="paginationControl"> 
    <!-- Previous page link --> 
    <?php if (isset($this->previous)): ?> 
    <a href="<?php echo $this->url(array('page' => $this->previous)); ?>"> 
    Previous 
     </a> <span class="bar"> | </span> 
     <?php else: ?> 
     <span class="disabled"> Previous</span> <span class="bar"> | </span> 
<?php endif; ?> 
<!-- Numbered page links --> 
<?php foreach ($this->pagesInRange as $page): ?> 
    <?php if ($page != $this->current): ?> 
    <a href="<?php echo $this->url(array('page' => $page)); ?>"> 
     <?php echo $page; ?> 
    </a> <span class="bar"> | </span> 
    <?php else: ?> 
    <?php echo $page; ?> <span class="bar"> | </span> 
    <?php endif; ?> 
<?php endforeach; ?> 
<!-- Next page link --> 
<?php if (isset($this->next)): ?> 
    <a href="<?php echo $this->url(array('page' => $this->next)); ?>"> 
    Next 
    </a> 
<?php else: ?> 
    <span class="disabled">Next </span> 
<?php endif; ?> 
</div> 
<?php endif; ?> 

Hy vọng nó sẽ hữu ích.

+1

+1 để sử dụng 'new Zend_Paginator_Adapter_DbTableSelect()' thay cho toàn bộ mảng; và trả về 'Zend_Paginator' từ mô hình. – chelmertz

+0

+1 dành cho người quay về trang quay số :) –

+0

Tôi nhận thấy thêm// action/page/pageno vào url. Làm cho hình ảnh và css của tôi không đúng cách. Điều gì sẽ là nguyên nhân. Làm cách nào để tải hình ảnh và css của tôi? – shorif2000