在 Yii 中组合表单和 CPagination

发布于 2024-09-16 17:59:06 字数 693 浏览 2 评论 0原文

我已经设置了一个显示服务器日志的页面并添加了一个过滤器表单,以便我可以过滤特定操作、日期等的日志。我似乎不知道如何使用 CPagination 来但是,将结果分成几页。如果我添加 CPagination 小部件,则当从小部件中选择页面时,不会重新提交过滤器表单,并且我的过滤器结果会丢失。

我找不到一个简单的解决方案来使用 CPagination 小部件维护我的过滤器表单数据。任何建议都会很棒。

更新
我有这个功能,但我必须设置一个会话状态来维护我的表单数据。

$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;
}

关于如何清除导航到另一个页面时的会话状态有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-09-23 17:59:06

如果您使用 CGridView ,使用分页添加过滤器会很容易 - 它是内置的看看 Gii crud 工具渲染的 admin.php 视图(或者也可能是博客演示)。也许您不想以表格形式显示日志。

您可以使表单成为有状态表单并使用 setPageState() 保存先前的状态,而不是设置会话变量。我不知道分页是否提交表单...您可能必须绑定 jQuery 事件才能在单击分页链接时提交表单。如果你必须这样做的话,那就有点难看了。

至于清除会话状态变量,我现在唯一的想法是重写 onBeginRequest() 或其他方法来检查用户是否在搜索页面上,如果不在,则清除会话多变的。您也可以将其保留 - 设置一个较短的到期日期然后忘记它。或者将其保存到数据库而不是会话中? :P

编辑

如果您采用重写 onBeginRequest() 的方法,看起来就像在配置文件中声明回调一样简单。更多信息Yii 论坛 ,以及此处。但我自己从来没有真正这样做过。祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文