[SalesForce] Render Based on Map Keys

I'm fairly new to developing on the Salesforce platform and I'm running into a bit of a problem. I have an Apex class with a map of Opportunity Stages and total values, and I'm trying to render a list item only if that map contains the related key – however I'm getting "Unknown function keyExists" error on the Visualforce page.

Apex Class:

Public Map<string,decimal> rawMap{get;set;}
public boolean keyExists(String checkKey) {
    return rawMap.containsKey(checkKey);
}

Visualforce Attempt 1:

<apex:outputPanel rendered="{!keyExists('Closed Lost')}">
            <-- Some Text -->
</apex:outputPanel>

Visualforce Attempt 2:

<apex:form>
    <apex:actionFunction action="{!keyExists}"  name="test">
        <apex:param value="Closed Lost" />
    </apex:actionFunction>
</apex:form>

Is there something obvious I'm missing? Or any ideas on how I can get around this?

UPDATE: I found a messy solution…

  1. Put the Map into a Javascript string variable on the Visualforce page.
  2. Create a Javascript method that checks the strings contents and sets a hidden div to contain the output boolean.
  3. Set your <apex:outputPanel> to render on that hidden components value

Apex Class:

Public Map<string,decimal> rawMap{get;set;}

Visualforce:

<script type='text/javascript'>
    var rawMap = '{!rawMap}';

    /* Checks if map contains a certain Opportunity Stage */
    function containsKey(findKey) {
        document.getElementById('containsKeyVar').innerHTML = rawMap.includes(findKey);
    }
</script>

<!-- This element is being used as a global variable: does Opportunity map contain  -->
<div id="containsKeyVar" style="display: none;">false</div>

<!-- Check if Stage Exists, and only then render -->
<script type="text/javascript">
    containsKey('Closed Lost');
</script>

<apex:outputPanel rendered="{!$Component.containsKeyVar}">
    <!-- Some Text -->
</apex:outputPanel>

Anyone have a better solution?

Best Answer

You can iterate through map keys and rerender section if particular key is specified:

public with sharing class MyController {
    Public Map<string,decimal> rawMap{get;set;}
    public MyController() {
        rawMap = new Map<string,decimal>();
        rawMap.put('Closed Lost', 11.1); 
    }
}

<apex:page controller="MyController" tabStyle="Account">
    <apex:form >
        <apex:pageBlock title="My Content"> 
            <apex:repeat value="{!rawMap}" var="dirKey">
                <apex:outputPanel rendered="{!dirKey=='Closed Lost'}">
                        <apex:outputText value="Closed Lost" />
                </apex:outputPanel>
                <apex:outputPanel rendered="{!dirKey=='Closed Won'}">
                    <apex:outputText value="Closed Won" />
                </apex:outputPanel>
            </apex:repeat>
        </apex:pageBlock> 
    </apex:form>
</apex:page>