How to create a "Model browser"?

Hello to everyone.

I am wondering if Yii provides any means to easily create a model browser in the form CLinkPager does (i.e. << Previous | post 3 of 24 posts | Next >>).

What I am trying to accomplish is to provide Previous and Next links at the show view of a model, that would allow the user to browse through the items of a list.

Thanks in advance for your help.

I think you might be able to accomplish this with the CLinkPager and a pageSize of 1. Basically, create a view that’s a combination of the list and show views. You want a list of 1 that shows the model. Not sure if this is the best way, but it seems like it should do the trick.

Thanks for your answer Jaz.

I couldn’t find a way to create the necessary links (wasn’t able to pass the model id) plus had problems with sorted lists (couldn’t preserve the sorting).

I came up with the following solution, in case this interests anyone:

  1. Create a string of the id’s in list (at the list or admin action), comma separated

  2. Store the string to the session

  3. Read the stored session string at the show action of the controller

  4. Pass to the show view the Previous and Next id’s needed to to create the necessary links

Step 4 is quite easy to perform using the explode and array_search functions of php.

side note: how do I delete a post?

CAction




public function run()

{

    $criteria = new CDbCriteria;

    $pages=new CPagination(MyModel::model()->count());

    $pages->pageSize=1;

    $pages->applyLimit($criteria);

    $models=MyModel::model()->findAll($criteria);

    $this->render('myshow', array('models'=>$models, 'pages'=>$pages));

    //note if using CAction outside the controller class, it's $this->getController()->render...

}



The view myshow just needs to be the same as your normal show view, but with the pager from the list view included for $pages. If you extend the CLinkPager class, you can make modifications to the buttons and/or change them to do ajax instead of full page loads.