【问题标题】:yii pagination with arrayyii 数组分页
【发布时间】:2014-03-18 19:48:33
【问题描述】:

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

控制器

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

【问题讨论】:

    标签: php yii pagination


    【解决方案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.
    

    【讨论】:

    • 值得注意的是,如果你使用CArrayDataProvider(它基于数组创建数据提供者),它比使用CGridView有一个小小的飞跃,你可以让Yii来处理表格生成、排序、分页、布局等
    • 嗯,你也可以将它与 CListView 一起使用,让他通过调用另一个视图来完成他在 foreach 循环中所做的事情。
    • 我使用过 dataprovider,但我将如何实现它以供我使用?
    • 我用过dataprovider。但是CLinkPager 以子弹形式出现?
    • 如果我愿意,如何将结果转换为表格格式?
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多