如何在zend框架中将zend_paginator转换为ajax

时间:2012-02-01 10:13:21

标签: zend-framework zend-paginator

这是我的动作,目前我的网页很新鲜,我希望它与ajax.so一样,没有刷新整个页面我的分页div是重新加载来自DB的内容。

 public function calllogsAction(){
         if(!Zend_Auth::getInstance()->hasIdentity()){
               $this->_redirect('login/login');
      }
      else{

       $request = $this->getRequest();
       $phone_service_id  =  $request->getParam("id");
       $registry = Zend_Registry::getInstance();  
       $DB = $registry['DB'];
       $sql ="SELECT caller_name,call_number,call_start_time,call_duration,call_direction FROM CALL_LOG WHERE phone_service_id = $phone_service_id";
       $result = $DB->fetchAll($sql);
       $page=$this->_getParam('page',1);
       $paginator = Zend_Paginator::factory($result);
       $paginator->setItemCountPerPage(10);
       $paginator->setCurrentPageNumber($page);
       $this->view->paginator=$paginator;
       $page = $paginator->getCurrentPageNumber();
       $perPage = $paginator->getItemCountPerPage();
       $total = $paginator->getTotalItemCount();
       $A = ($page - 1) * $perPage + 1;
       $B = min($A + $perPage - 1, $total);
       $C = $total;
       $this->view->assign('url', $request->getBaseURL());
       $this->view->assign('total',$total );
       $this->view->assign('page',$page );
       $this->view->assign('A',$A );
       $this->view->assign('B',$B );
       $this->view->assign('C',$C );


      }
    }

这是我的观点。我在视图中有更多的内容,但是这里的分页是我要刷新这个div

 <div class="container" id="actualbody" style="margin-left:168px;">
 <?php
 $sr_no = 1;

foreach($this->paginator as $record){
 if($sr_no % 2 == 0){
 $style = 'class="even-row"';
 }
 else{
 $style = 'class="odd-row"';
 <tr <?php echo $style;?>>
     <td class="sorting_1"> <input type="checkbox" /> </td>
     <td ><?php if($record->call_direction =="Outgoing"){?> <img src=" <?php echo $this->baseUrl('/assets/images/outgoing.png'); ?> " />  <?php } elseif($record->call_direction =="Missed") {?> <img src=" <?php echo $this->baseUrl('/assets/images/misses.png'); ?> " />  <?php } else { ?> <img src=" <?php echo $this->baseUrl('/assets/images/incoming.png'); ?> " />  <?php }?></td>
     <td><?php echo $record->caller_name;?></td>
     <td><?php echo $record->call_number;?></td>
     <td ><?php echo $record->call_start_time;?></td>
     <td ><?php echo $record->call_duration;?></td>
      </tr>
       <?php $sr_no ++; }?>

 <div> Showing <?= $this->A; ?> to <?= $this->B; ?> of <?=$this->C; ?> entries </div>
 <div>  <?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?></div>
 </div>

如何通过ajax

进行分页

这个pagination.phtml

<div class="pagination" style="width:100%">
<div style="float:left;width:28%">
</div>
<div style="float:right;width:70%;">
    <!-- First page link -->
    <?php if (isset($this->previous)): ?>
          <a href="<?= $this->url(array('page' => $this->first)); ?>"> <span class="first ui-corner-tl ui-corner-bl fg-button ui-button ui-state-default ui-state-disabled">Start</span></a> 
    <?php else: ?>
           <span class="next fg-button ui-button ui-state-default ui-state-disabled">Start</span> 
    <?php endif; ?>



    <!-- Previous page link -->

    <?php if (isset($this->previous)): ?>
          <a href="<?= $this->url(array('page' => $this->previous)); ?>"><span class="previous fg-button ui-button ui-state-default ui-state-disabled">Previous</span></a>
    <?php else: ?>
         <span class="next fg-button ui-button ui-state-default ui-state-disabled">Previous</span> 
    <?php endif; ?>
    <!-- Numbered page links -->
    <?php foreach ($this->pagesInRange as $page): ?>
        <?php if ($page != $this->current): ?>
            <a href="<?= $this->url(array('page' => $page)); ?>"><span class="fg-button ui-button ui-state-default"><?= $page; ?></a></span>
        <?php else: ?>
            <span class="fg-button ui-button ui-state-default ui-state-disabled" ><?= $page; ?></span>
        <?php endif; ?>
    <?php endforeach; ?>
    <!-- Next page link -->
    <?php if (isset($this->next)): ?>
          <a href="<?= $this->url(array('page' => $this->next)); ?>"><span class="next fg-button ui-button ui-state-default">Next</span></a> 
    <?php else: ?>
    <span class="next fg-button ui-button ui-state-default ui-state-disabled">Next</span>
    <?php endif; ?>
    <!-- Last page link -->
    <?php if (isset($this->next)): ?>
          <a href="<?= $this->url(array('page' => $this->last)); ?>"><span class="last ui-corner-tr ui-corner-br fg-button ui-button ui-state-default">End</span></a>
    <?php else: ?>
       <span class="next fg-button ui-button ui-state-default ui-state-disabled">End</span>
    <?php endif; ?>

</div>

编辑 我添加此代码

jQuery(function($){
var container = $('#paged-data-container');

var overlay = $('<div>').addClass('loading overlay');

$('.pagination-control').find('a').live('click', function(){
    var href = this.href;
    var pos = this.rel == 'next' ? '-120%' : '120%';
    if (Modernizr.history) {
        history.pushState(location.pathname, '', href);
    }
    container.find('.data').animate({
        left: pos
    }, 'slow', function(){
        var dataContainer = container.find('.paged-data').addClass('loading');
        $.get(href, { format: 'html' }, function(data){
            dataContainer.removeClass('loading');
            container.html(data);
        }, 'html');
    });
    return false;
});

var initialPath = location.pathname;

$(window).bind('popstate', function(e){
    // Prevent popstate handler dealing with initial page load
    if (location.pathname == initialPath) {
        initialPath = null;
        return;
    }
    container.append(overlay);
    $.get(location.pathname, { format: 'html' }, function(data){
        container.html(data);
    }, 'html');
});
});

还将此代码添加到我的控制器

public function init(){
$this->_helper->ajaxContext->addActionContext('calllogs', 'html')
                           ->initContext();
}

现在我的collogs.phtml看起来像这样

<?php
include_once("header.phtml");
include_once("blue1.phtml");
include_once("sidebar.phtml");
?>
<div id="paged-data-container">
<?php echo $this->render('login/calllogs_page.phtml') ?>
 </div>
<?php include_once("footer.phtml"); ?>

这是我的calllogs_page.phtml

<?php $paginationControl = $this->paginationControl(
$this->paginator, 'All', 'pagination.phtml') ?>
   old stuff here
 <div class="pagination-control">
            <div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix" style="height:35px">
            <?php if (count($this->paginator)): ?>
            <div class="dataTables_info" style="float:left;"> Showing <?= $this->A; ?> to <?= $this->B; ?> of <?=$this->C; ?> entries </div>
            <div class="pagination-control" style="margin-left:173px;">
              <?php echo $paginationControl ?>
               </div>

现在,当我点击任何分页链接时,我的页面在地址栏中发生变化但没有任何反应

这是jquery

$(function($){
$('.pagination-control').find('a').live('click', function(){
    var link = $(this);

    var container = link.parents('.paged-data-container');
    $.get(link.attr('href'), { format: 'html' }, function(data){
        container.html(data);
    }, 'html');

    return false;
});
});

1 个答案:

答案 0 :(得分:1)

请遵循以下教程:Zend framework video tutorials

从视频11 Zend_Paginator开始介绍。稍后,它被修改为在另一个视频中使用Ajax。 我真的建议你调查一下。