Show messages based on Severity in two p:growl
Asked Answered
W

3

5

I'm using PrimeFaces p:growl.

<p:growl id="msgsInfo"
         rendered="true"
         showDetail="true" />
<p:growl id="msgsError"
         globalOnly="true"
         showDetail="true"
         sticky="true" />

I need to show in the first growl only Info messages while in the second I need to show Error messages. Using globalOnly when I add error message this is show 2 times.

Any idea?

Walachia answered 15/1, 2011 at 17:42 Comment(1)
Currently you can do. In fact, looks like it has been available for a time #20466174Vierra
E
3

It would in theory be possible if it supported infoClass, errorClass, etc attributes like as h:messages. You could then just specify a CSS class which does a display: none.

But the p:growl doesn't support those attributes. On the severity level all you can do is changing the icon by infoIcon, errorIcon, etc. So you're pretty lost here.

It might be worth a feature request.

Note that the globalOnly="true" only displays messages which have a null client ID, regardless of their severity.

Epigraphic answered 16/1, 2011 at 12:32 Comment(2)
Ok, also if i use rendered=message_severity? I have think that but i'don't know how to implementWalachia
This won't work since that would apply on p:growl component itself, not on each of the messages it is displaying.Epigraphic
S
3

Please see my answer

PrimeFaces growl change color dynamically (Multiple messages)

You can also find the source code of the project which produces the page below:

enter image description here

Summons answered 18/10, 2016 at 8:37 Comment(0)
C
1

I was looking for the same functionality (to set the growl to sticky from a specific message severity). PrimeFaces (6.1) doesn't offer this functionality, but it is quite easy to hack the growl JavaScript. More specifically, in the bindEvents function they check if sticky was configured and based on that:

//hide the message after given time if not sticky
if(!sticky) {
    this.setRemovalTimeout(message);
}

So, you could prototype (override) the bindEvents function and set sticky based on the message severity.

PrimeFaces.widget.Growl.prototype.bindEvents = function(message) {
    var _self = this,
    sticky = this.cfg.sticky;

    // Start hack
    if (!sticky) {
      // Your rule
    }
    ...

You could also prototype renderMessage and add severity as a parameter to bindEvents. I chose to used a quick hack and read it from the className.

I've added these utility functions:

var SEVERITIES = [ "info", "warn", "error", "fatal" ];

function getSeverity(domNode) {
  // HACK Severity can be found in the className after the last - character.
  var severity = domNode.className;
  return severity.substring(severity.lastIndexOf("-") + 1);
}

function getSeverityIndex(severityString) {
  return SEVERITIES.indexOf(severityString);
}

Now you can use the following check:

if (!sticky) {
  sticky = getSeverityIndex(getSeverity(message[0])) >= getSeverityIndex("error");
}

I might create a pull request at GitHub where you can set the minimum severity to stick massages using the stickSeverity attribute on the p:growl component.

Here is the full JavaScript hack (PrimeFaces 6.1):

var SEVERITIES = [ "info", "warn", "error", "fatal" ];

function getSeverity(domNode) {
  // HACK Severity can be found in the className after the last - character.
  var severity = domNode.className;
  return severity.substring(severity.lastIndexOf("-") + 1);
}

function getSeverityIndex(severityString) {
  return SEVERITIES.indexOf(severityString);
}

PrimeFaces.widget.Growl.prototype.bindEvents = function(message) {
    var _self = this,
    sticky = this.cfg.sticky;

    // Start customization
    if (!sticky) {
      sticky = getSeverityIndex(getSeverity(message[0])) >= getSeverityIndex("error");
    }
    // End customization

    message.mouseover(function() {
        var msg = $(this);

        //visuals
        if(!msg.is(':animated')) {
            msg.find('div.ui-growl-icon-close:first').show();
        }
    })
    .mouseout(function() {
        //visuals
        $(this).find('div.ui-growl-icon-close:first').hide();
    });

    //remove message on click of close icon
    message.find('div.ui-growl-icon-close').click(function() {
        _self.removeMessage(message);

        //clear timeout if removed manually
        if(!sticky) {
            clearTimeout(message.data('timeout'));
        }
    });

    //hide the message after given time if not sticky
    if(!sticky) {
        this.setRemovalTimeout(message);
    }
}
Chicky answered 7/5, 2017 at 17:11 Comment(2)
Worth filing an enhancement in githubAllover
@Allover I'm not sure if they will accept it. Probably they just say to use multiple growls and use the severity attribute.Chicky

© 2022 - 2024 — McMap. All rights reserved.