CArrayDataProvider with CGridView pagination Yii

13,672

Not sure it will solve your problem, but in your CArrayDataProvider you use id to define the name of the key field instead of keyField. You could try the following:

$dataProvider=new CArrayDataProvider($users, array(
    'id'=>'users',
    'keyField' => 'id', 
    'keys'=>array('id','name', 'surname', 'phone', 'address'),
    'sort'=>array(
        'attributes'=>array(
            'name', 'surname', 'phone', 'address'
        ),
    ),
    'pagination'=>array(
        'pageSize'=>15,
    ),
));
Share:
13,672

Related videos on Youtube

Larry
Author by

Larry

Updated on June 04, 2022

Comments

  • Larry
    Larry almost 2 years

    I'm trying to do a pagination on a CGridView using CArrayDataProvider (my $rawData is a custom array - not from a DB/model). So, In the controller`s action a have the following:

    $form = new SearchUser;//here I have SearchUser form that extends CFormModel with the following attributes: 'id', 'name', 'surname', 'phone', 'address'
    $users = array();
    if (isset($_POST['SearchUser'])) {
    ....//prepare users array from my custom source-> not from DB/models etc
    }
    
    $dataProvider=new CArrayDataProvider($users, array(
                'id'=>'id',
                'keys'=>array('name', 'surname', 'phone', 'address'),
                'sort'=>array(
                    'attributes'=>array(
                        'name', 'surname', 'phone', 'address'
                    ),
                ),
                'pagination'=>array(
                    'pageSize'=>15,
                ),
            ));
    

    And:

    $this->render('index', array('dataProvider'=>$dataProvider, 'form'=>$form));
    

    On index.php I have:

    ...
    <?php echo CHtml::link('Search','#',array('class'=>'search-button')); ?>
    <div class="search-form" style="display:none">
    <?php $this->renderPartial('_search',array(
    'model'=>$form,
    )); ?>
    </div><!-- search-form -->
    <?php
    
    $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
    
        array(
            'name' => 'Name',          
            'type' => 'raw',
            'value' => 'CHtml::encode(@$data["name"])'
        ),
        array(
            'name' => 'Surname',          
            'type' => 'raw',
            'value' => 'CHtml::encode(@$data["surname"])'
        ),/*
        array(
            'name' => 'Phone',          
            'type' => 'raw',
            'value' => 'CHtml::encode(@$data["phone"])'
        ),*/
        array(
            'name' => 'Address',          
            'type' => 'raw',
            'value' => 'CHtml::encode(@$data["address"])'
        ),
    ),
    'enablePagination'=> true,
    ));
    

    The first page is displayed correctly but when I select another page, my filter is lost and all data are displayed in the grid instead of "filtered" ones.

    • darkheir
      darkheir about 11 years
      In your url do you have ?page=...?
    • Larry
      Larry about 11 years
      I have something like this: &id_page=2
  • Nikos Tsirakis
    Nikos Tsirakis over 10 years
    CArrayDataProvider doesn't have implemented the paging & sorting methods so you need to implement them based on your implementation
  • darkheir
    darkheir over 10 years
    Yes it have it! just check the example from the api yiiframework.com/doc/api/1.1/CArrayDataProvider. By the way the pagination is never implemented by the data provider. The data provider calls a CPagination object or a class that inherit from it
  • Jimmy Obonyo Abor
    Jimmy Obonyo Abor over 7 years
    Probably this answer should help you should not use $criteria->order if you intend to use the sorting functionality : stackoverflow.com/a/13561187/1226748