【问题标题】:I want to show the setFlash message for a longer period of time (20000 not 3000)我想更长时间地显示 setFlash 消息(20000 不是 3000)
【发布时间】:2013-10-04 11:15:32
【问题描述】:

我正在使用 setFlash 函数在页面顶部显示一条消息,但我想更改它显示多长时间(当前在 .js 文件中它显示为 3000,但我希望它显示 20秒不是 3)

有没有办法对特定 setFlash 的页面停留时间进行编码?而不是更改网站范围的 setFlash 默认值?

这是我的控制器页面中的代码。

 $this->Session->setFlash(__l('There is not enough in escrow to close the project ') , 'default', null, 'error');
            $this->redirect(array(
                'controller' => 'escrow_accounts',
                'action' => 'add',
                'project' => $escrowAccount['Project']['id']
            ));

【问题讨论】:

    标签: php cakephp cakephp-2.0


    【解决方案1】:

    您需要为 Flash 消息创建自定义模板。例如:

    // location /app/View/Elements/flash_message.ctp
    
    <div id="flashMessage" data-timeout="<?php echo $timeout;?>"> 
        <?php echo $message; ?> 
    </div>
    

    然后通过设置 Session flash message 来使用这个自定义模板:

    $this->Session->setFlash(
        __l('There is not enough in escrow to close the project '), 
        'flash_message', 
        array('timeout' => 3000), 'error');
    

    现在,您需要在 javascript 中获取超时参数。我假设,你正在使用 jQuery:

    var interval = $("#flashMessage").attr("data-timeout");
    

    就是这样,您可以随意设置自定义超时。

    【讨论】:

    • 我得到了前两个部分,但 jQuery 位 - 我如何取出时间参数?只是删除一些东西?我的 js 目前看起来像这样 $.fn.flashMsg = function() { $this = $(this); $alert = $this.parents('.js-flash-message'); var alerttimer = window.setTimeout(function() { $alert.trigger('click'); }, 3000); 那么我在哪里添加你提到的最后一行 var...
    • 试试这个:$.fn.flashMsg = function() { $this = $(this); $alert = $this.parents('.js-flash-message'); var interval = $("#flashMessage").attr("data-timeout"); var alerttimer = window.setTimeout(function() { $alert.trigger('click'); }, interval); }
    • 但这是否意味着我需要将array('timeout' =&gt; 20000), 'error'); 添加到当前没有设置超时功能的所有$this-&gt;Session-&gt;setFlash( 部分?还是以某种方式默认?
    • 对不起,我的意思是我有其他使用'error' 位的闪存消息,所以如果我更改上面的代码,那么我需要找到所有其他连接到@987654330 的错误闪存消息@并添加您所说的'20000'代码。如何将原始 Flash 消息弹​​出窗口保留为原始时间,并将新消息保留为 20000?
    • 我不能说我理解你的问题。如果你想使用默认 - 使用你以前的代码。如果你想使用自定义超时 - 使用我的。问题出在哪里?或者在任何地方使用我的自定义超时时间。
    【解决方案2】:

    Flash 消息可以有第四个参数,'key',然后您可以将其用作 css 和 js 的句柄。在这里阅读它:http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages

    因此,您将有一个名为“long-flash”或类似的键,只需将这些特定的闪存消息设置为显示 20 秒。

    【讨论】:

      【解决方案3】:

      让我举一个设置消息暂停的例子:这会给你 30 秒。

      $this->flash('Your message', array('action' => 'index'), 30);
      

      【讨论】:

      • 他询问的是“setFlash”方法,而不是“flash”方法。它们是两种不同的东西。 'flash' 是用来重定向的,这不是他想要的。
      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多