开发者

Combining a Form and CPagination in Yii

开发者 https://www.devze.com 2023-01-14 02:52 出处:网络
I\'ve set up a page that displays开发者_JS百科 server logs and added a filter form so I can filter the logs on specific actions, dates, etc.I can\'t seem to figure out how to use CPagination to split

I've set up a page that displays开发者_JS百科 server logs and added a filter form so I can filter the logs on specific actions, dates, etc. I can't seem to figure out how to use CPagination to split the results into pages, however. If I add the CPagination widget, when a page is selected from the widget, the filter form isn't resubmitted and my filter results are lost.

I'm at a loss for a simple solution to maintain my filter form data with the CPagination widget. Any suggestions would be great.

Update

I have this functioning but I had to set a session state to maintain my form data.

$filter=false;
$form=new LogFilterForm;
if(isset($_POST['LogFilterForm'])){
    $form->attributes=$_POST['LogFilterForm'];
    Yii::app()->user->setState('filter',$form);
    $filter=true;
}
else if(Yii::app()->user->hasState('filter')){
    $form=Yii::app()->user->getState('filter');
    $filter=true;
}

Any ideas on how I can clear the session state on navigation to another page?


If you were using CGridView it would be easy to add filters with paging - it's built in. Just look at the admin.php view rendered by the Gii crud tool (or the blog demo maybe too). Perhaps you don't want to display the logs in table form though.

Instead of setting session variables you could make the form a stateful form and save the previous state using setPageState(). I don't know if the pagination submits the form though... you might have to bind a jQuery event to submit the form when clicking on the pagination links. That would be kinda ugly if you had to do that.

As for clearing the session state variables, my only idea right now is to override onBeginRequest() or something to check if the user is on the search page, and if they aren't, then clear the session variable. You could just leave it too - set a short expiration date and forget it. Or save it to your database instead of the session? :P

EDIT

If you go the route of overriding onBeginRequest() it looks like it's as easy as declaring a callback in the config file. More info here in the Yii forums, and here too. I've never actually done it myself though. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消