zeshan ahmad
zeshan ahmad

Reputation: 46

yii parent child Clistview paging issue

I am using Yii framework of php. My scenario is I've number of projects and all the projects have posts. For projects i used Clistview and in the 'itemView' of this control another page is specified named '_post'. In '_post' page again ClistView is specified for showing post details.

But the problem is when paging occur in post details Clistview of the projects and changing the page number of the one post detail to next page number, all the post detail clistview page number changes.

I have also specified post detail clistview 'id' but in no vain.

For Projects:

<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView', 
              array(
                  'dataProvider'=>$dataProvider,
                  'id'=>'projectListView',
                  'itemView'=>'_post', // refers to the partial view
                  'enablePagination'=>true,
              ));
echo '</div><br />'
?> 

For Post detail:

<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
              array(
                  'dataProvider'=>$this->CallProjectPosts($data->id),
                  'id'=>'postListView'.$data->id,
                  'itemView'=>'_postDetail',
                  'enablePagination'=>true,
              ));
echo '</div>'
?>

Upvotes: 1

Views: 1832

Answers (1)

bool.dev
bool.dev

Reputation: 17478

This is how i finally got it working, it will work until you find a better solution.
i'm assuming that project_id is a foreign key in your posts table.
Which will generate(using gii) the necessary HAS_MANY relation in your project model, which in turn enables us to access the posts of a project easily, without calling the CallProjectPosts method.

So modify your Post view(_post.php):

<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';

$relatedPosts=new CArrayDataProvider($data->posts, // this is where the HAS_MANY relation comes into play
        array(
            'pagination'=>array(
                'pageSize'=>1, // whatever your size was
            )
        )
);
$this->widget('zii.widgets.CListView',
           array(
               'dataProvider'=>$relatedPosts,
               'id'=>'postListView'.$data->id,
               'itemView'=>'_postDetail',
               'enablePagination'=>true,
           ));
echo '</div>'
?>

For Project list :

<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView', 
          array(
              'dataProvider'=>$dataProvider,
              'id'=>'projectListView',
              'itemView'=>'_post', // refers to the partial view
              'enablePagination'=>true,
              'ajaxUpdate'=>false
          )
);
echo '</div><br />'
?>

As you will see, i have disabled ajaxUpdate for the project list view, if it is enabled, then the solution will not work, so if your requirement is to display/update the project list also through ajax then this will not work, currently only the project posts are updated through ajax.
Hope this helps.

Upvotes: 1

Related Questions