[SalesForce] Close/dismiss button in apex addmessage

I'm looking a way to have a close button on the pagemessage after it displays the message or dismiss after few seconds something similar to JQuery.

VFP:

<apex:pageMessages id="showmsg"></apex:pageMessages>

APEX:

ApexPages.addmessage(new ApexPages.message(
             ApexPages.severity.CONFIRM,'Saved successfully!'));

Best Answer

I have tried something similar in the past.

For click to clear action

Although I didn't get any close symbol, I was able to capture any click on the outputPanel which contained the pageMessage, and call an actionFunction which would simply reRender the panel. This would clear the messages. You could try to inject some symbol or message through JavaScript.

Timed clearing

So the important thing here is that on save I make the timer boolean as true. And then rerender the panel that contains the ActionPoller. The ActionPoller waits for 10 seconds and then calls the timerOff method and rerenders the outputPanel containing the pageMessage.

See the relevant VF sections and controller below to achieve the above two scenarios. It uses pure Visualforce and controller logic to achieve this.

Visualforce

 <apex:outputPanel id="messagePanel" onclick="clearMessage();">
       <apex:pageMessages id="message"></apex:pageMessages>
    </apex:outputPanel>

    .....

    <apex:commandButton value="Save" 
             reRender="pbId,timerPanel" 
             rendered="{!acc.id==editId}" 
             action="{!saveRecord}">    
     .....


    <apex:outputPanel id="timerPanel">
        <apex:actionPoller reRender="messagePanel" 
                  interval="10" 
                  enabled="{!timer}" 
                  action="{!timerOff}"/> 
    </apex:outputPanel>

    <apex:actionFunction name="clearMessage" 
                action="{!timerOff}" 
                reRender="messagePanel"/>

Controller

public class AccountController {

    public List<Account> accountList{get; set;}
    public boolean timer {get; set;}

    public AccountController(ApexPages.StandardController controller) {
        accountList = [select id,name,billingcity,Phone,Website from Account limit 20];
    }

    public void saveRecord(){
        update accountList;
        editId = null;
        ApexPages.addmessage(new ApexPages.message(
             ApexPages.severity.CONFIRM,'Saved successfully!'));

        timerOn();
    }

    public void clearMessage(){
        ApexPages.getMessages().clear();

    }

    public void timerOn(){
        timer = true;
    }

    public void timerOff(){
        timer = false;
    }

}
Related Topic