-1

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

控制器

$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 文件。

4

1 回答 1

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.
于 2013-03-18T14:41:00.037 回答