yii与数组分页

时间:2013-03-18 13:54:56

标签: php yii pagination

我正在尝试yii分页并遵循此example。但它似乎对我不起作用。我想分页一组字符串值。以下就是我所拥有的:

CONTROLLER

$location = $this->location($_SERVER['SERVER_NAME']);
$files = $this->getFiles("/data/scan/invoice");
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$item_count = count($files);        
$pages = new CPagination($item_count);
$pages->setPageSize(Yii::app()->params['listPerPage']);
$criteria = new CDbCriteria();

$pages->applyLimit($criteria);  // the trick is here!       

$this->render('index',array(
            "location"=>$location,
            "files"=>$files,
            'item_count'=>$item_count,              
            'page_size'=>Yii::app()->params['listPerPage'],
            'pages'=>$pages
));

查看

<div id="main">
    <table>
        <?php foreach($files as $q): ?>

        <form>
            <tr>
                <td rowspan=2>
                    <div>
                        <object data="<?php echo '/data/scan/invoice/'.$q; ?>" type="application/pdf">
                            <p>
                                It appears you don't have a PDF plugin for this browser. No
                                biggie... you can <a href="#">click
                                    here to download the PDF file.</a>
                            </p>
                        </object>
                    </div>
                </td>
                <td><input id="lognumber" type="text" /> <input type="button" value="Search" onclick="search()"/> 
                <div style="height:40px;width:100%;">
                </div>
                    <br> <input type="button" value="Save" /> 
                    <br> <input type="button"value="Delete" /> <br>
                    <div id="lookup"></div>
                </td>
            </tr>
            <tr>
                <td height="300px"><?php //echo $this->paginationControl($this->paginator,'Jumping','partial/my_pagination_control.phtml'); ?>
                </td>
            </tr>
        </form>
        <?php endforeach; ?>
    </table>
</div>

<?


$this->widget('CLinkPager', array(
        'currentPage'=>$pages->getCurrentPage(),
        'itemCount'=>$item_count,
        'pageSize'=>$page_size,
        'maxButtonCount'=>5,
        //'nextPageLabel'=>'My text >',
        'header'=>'',
        'htmlOptions'=>array('class'=>'pages'),
));

字符串值数组是文件路径,用于在对象查看器中显示pdf文件。

1 个答案:

答案 0 :(得分:1)

问题是您对空CDbCriteria应用限制:

$criteria = new CDbCriteria();
$pages->applyLimit($criteria);  // the trick is here!

然后你永远不会使用$criteria。在您的示例中,他们对$criteria应用限制,然后他们使用它来获取数据,因此当yii获取数据时,它知道它将被分页:

$this->render('index',array(
                        'model'=> ModelNameX::model()->findAll($criteria), 
        ));

要执行您想要的操作,您需要使用CArrayDataProvider来获取需要分页的数据。

$dataProvider=new CArrayDataProvider($files);
$dataProvider->setPagination($pages);

在您看来,您需要致电

$dataProvider->getData() //will return a list of arrays.