简体   繁体   English

Yii框架:分页中的部分视图

[英]Yii Framework: Pagination in partial view

Working under the Yii framework, I have a page that loads and lists different sections. 在Yii框架下工作,我有一个页面可以加载并列出不同的部分。 This is, of course, done with renderPartial a few different views. 当然,这是通过renderPartial一些不同的视图完成的。 One of the sections takes an array and displays a list of the items in it. 这些部分之一采用一个数组并显示其中的项目列表。 What I need to do is have pagination only for this section, preferably using AJAX so I don't reload all the content upon page selection. 我需要做的是仅对该部分进行分页,最好使用AJAX,这样我就不会在选择页面时重新加载所有内容。

That's the first part of the problem. 那是问题的第一部分。 I've looked into using CActiveDataProvider with CListView , however, the problem there is that I'm displaying information from an array that's unrelated to the database. 我已经研究过将CActiveDataProviderCListView CActiveDataProvider使用,但是,问题在于我正在显示来自与数据库无关的数组中的信息。 In other words, there's no conditions and so on to create the array, I just have a bunch of $items and that's about it. 换句话说,没有条件等等来创建数组,我只有一堆$items ,仅此而已。 Any tips appreciated! 任何提示表示赞赏!

You can use a CArrayDataProvider instance as the dataProvider for the CListView . 您可以将CArrayDataProvider实例用作CListViewdataProvider It has inbuilt pagination support though you may have to explicitly set the pagination's currentPage . 它具有内置的分页支持,尽管您可能必须显式设置分页的currentPage

You can use CLinkPager without CGridView, there is an example: 您可以在不使用CGridView的情况下使用CLinkPager,有一个示例:

In controller: 在控制器中:

$criteria = new CDbCriteria;
$criteria -> order = 'date';
$pages = new CPagination(Posts::model() -> count());
$pages -> pageSize = 50;
$pages -> applyLimit($criteria);
$posts= Posts::model() -> findAll($criteria);
$this -> controller -> render('list', array('posts' => $posts, 'pages' => $pages));

In view: 鉴于:

<?php $this->widget('CLinkPager',array('pages'=>$pages, "cssFile" => false)); ?>
<ul>
<?php foreach($posts as $x => $post): ?>
  <li><?php echo $post -> title ?></li>
<?php endforeach; ?>
</ul>
<?php $this->widget('CLinkPager',array('pages'=>$pages, "cssFile" => false)); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM