如何使用jquery ajaxify zend_pagination 结果(已经使用partialoop)
在控制器中我有:
$paginator = Zend_Paginator::factory($mdlPost->getPosts($this->moduleData->accordion, 'name ASC'));
if(isset($params['cities'])) {
$paginator->setCurrentPageNumber(intval($params['cities']));
}
$paginator->setItemCountPerPage(4);
$this->view->posts = $paginator;
在视图中我有这样的东西:
if ($this->posts != null) {?>
<div id="cities_accord" class="news">
<?php echo $this->partialLoop('partials/post-min.phtml', $this->posts); ?>
</div>
<?php echo $this->paginationControl($this->posts,
'Sliding',
'public/pagination_cont.phtml');
}
partial/post-min.phtml
<?php
$color = array(1=>'spring',2=>'summer',3=>'autumn',4=>'winter');
?>
<div id='<?php echo $color[$this->partialCounter] ?>' class="accordion_post">
<?php
$link = Digitalus_Uri::get(false, false, array('openCity' =>
$this->id));//$color[$this->partialCounter]));
?>
<h1 class="accordion_post_title"><?php echo $this->title ?></h1>
<p><?php echo $this->teaser ?> <a href="<?php echo $link;?>"><i>read more</i></a></p>
</div>
pagination_cont.phtml 取自此链接 zend ( http://framework.zend.com/manual/en/zend.paginator.usage.html ) 将显示将参数传递给控制器以获取相应的整个页面的链接,该页面目前工作正常,
但我想更改此设置,以便我能够ajaxify返回的(即只有一个分页值而不是重新加载整个页面)结果我如何使用 jquery 做到这一点以及我应该更改什么..
**编辑:如果可能的话,对于禁用 javascript 的浏览器(用户)来说,最好有一个失败保存通过重新加载页面看到同样的事情(即保持 if(javascript_not_enabled ) 的当前状态)**
in the controller i have :
$paginator = Zend_Paginator::factory($mdlPost->getPosts($this->moduleData->accordion, 'name ASC'));
if(isset($params['cities'])) {
$paginator->setCurrentPageNumber(intval($params['cities']));
}
$paginator->setItemCountPerPage(4);
$this->view->posts = $paginator;
in the view's i have some thing like this :
if ($this->posts != null) {?>
<div id="cities_accord" class="news">
<?php echo $this->partialLoop('partials/post-min.phtml', $this->posts); ?>
</div>
<?php echo $this->paginationControl($this->posts,
'Sliding',
'public/pagination_cont.phtml');
}
the partial/post-min.phtml
<?php
$color = array(1=>'spring',2=>'summer',3=>'autumn',4=>'winter');
?>
<div id='<?php echo $color[$this->partialCounter] ?>' class="accordion_post">
<?php
$link = Digitalus_Uri::get(false, false, array('openCity' =>
$this->id));//$color[$this->partialCounter]));
?>
<h1 class="accordion_post_title"><?php echo $this->title ?></h1>
<p><?php echo $this->teaser ?> <a href="<?php echo $link;?>"><i>read more</i></a></p>
</div>
the pagination_cont.phtml taken from this link zend ( http://framework.zend.com/manual/en/zend.paginator.usage.html )
will show links that will pass params to the controller to fetch the corresponding whole page which is working alright for now
but i want to change this so that i will be able ajaxify the returned ( i.e. only a single paginated value rather than reloading the whole page ) results how can i do that using jquery and what should i change ..
** EDIT: it would be nice to have a fail-save ,if possible, for browsers(users) that disabled javascript to see the same thing by reloading the page (i.e. keeping the current status for if(javascript_not_enabled ))**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我过去所做的。
首先,设置 AjaxContext 操作助手,用于在控制器操作上启用
html
上下文。添加一个
.ajax.phtml
视图,该视图仅包含可通过 AJAX 替换的标记部分以及分页控制链接。您可能可以将其复制到正常视图之外。将普通视图中的该部分替换为类似这样的内容,这将确保您的初始请求和任何非 AJAX 请求仍将包含正确的内容。
id 纯粹用于引用 JavaScript 中的可加载块。
另请确保仅在普通视图中包含 JS 文件(使用
headScript
)。现在,在 JS 文件中,不显眼地将适当的事件绑定添加到分页器链接。由于您将替换分页控制部分以反映正确的当前页面和其他链接,因此最好使用 jQuery
live
绑定来执行此操作。我还假设您将使用某种可识别元素包装分页控件(例如)
请记住,在使用此方法时,您将无法使用正常的后退/前进浏览器按钮导航分页请求。您还将失去直接为页面添加书签的能力(尽管您始终可以提供指向当前页面的永久链接作为 AJAX 加载内容的一部分)。
如果你真的很担心,你可以使用 jQuery 历史记录插件之类的东西,但这会需要更多的客户端工作。
另一个警告是,上述内容仅适用于分页链接。如果您想使用带有下拉页面选择的表单,则需要为提交添加另一个事件处理程序。
This is what I've done in the past.
First, setup the AjaxContext action helper to enable the
html
context on your controller action.Add a
.ajax.phtml
view that just contains the section of markup that may be replaced via AJAX as well as the pagination control links. You can probably just copy this out of your normal view. Replace that section in your normal view with something likeThis will ensure that your initial and any non-AJAX requests will still include the right content. The
<div>
id is purely for referencing the loadable block in JavaScript.Also make sure you include your JS file (using
headScript
) in the normal view only.Now, in your JS file, unobtrusively add the appropriate event binding to the paginator links. As you'll be replacing the pagination control section in order to reflect the correct current page and other links, it's probably best to do this using the jQuery
live
binding. I'm also assuming you'll wrap the pagination control with some kind of identifiable element (<div class="pagination-control">
for example)Keep in mind that in using this method, you will lose the ability to navigate the paged requests using the normal back / forward browser buttons. You will also lose the ability to bookmark pages directly (though you could always provide a permanent link to the current page as part of the AJAX loaded content).
You can use something like the jQuery history plugin if you're really concerned but that will require more client-side work.
Another caveat is that the above will only work with pagination links. If you want to use a form with dropdown page selection, you need to add another event handler for the submission.
明白了,非常感谢@Phil Brown:
在控制器 int() 中,将分页
控制器上的视图之一(最有可能在 pagination_cont.phtml 中)的响应类型更改为 json,添加 ajax 链接
并添加 JavaScript 函数js_method(json_data) 使用 json 数据修改 id = 'div_id' 的 div
GOT IT and big Thanks to @Phil Brown :
in the controller int() change the response type to json
in one of the views (most probably in the pagination_cont.phtml) on the pagination controller add the ajax links
and add a JavaScript function of js_method(json_data) to modify the div with id = 'div_id' with a json data