문제

I've set up a page that displays 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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top