+ Start a Discussion
tobibeertobibeer 

HowTo: Component attribute of type Map

I am struggling to pass down a map to a component. The following...

 

    <apex:attribute name="dict" type="map" description="descr" required="true"/>

... does not work as type map does not seem supported. So, I have set out to define my own apex class...

 

//simple wrapper for a custom map class
global with sharing class MyDict{
    global MyDict(){}

    private Map<String,String> dictionary = new Map<String,String>();
    
    public String get(String entry){
        return this.dictionary.get(entry);
    }

    public Void put(String entry, String translation){
        this.dictionary.put(entry,translation);
    }

    public map<String,String> getDict(){
        return this.dictionary;
    }
}

 

Well... so far so good, I can create, populate and retrieve an instance of MyDict in my page controller like this...

 

    //create dict instance
    private MyDict dict = new MyDict();

    //populate dict
    List<Dict__c> dic = Dict__c.getall().values();
    for (Dict__c d : dic) {
        this.dict.put(d.name,String.ValueOf(d.get(this.lang+'__c')));
    }    
    
    //return dict
    public SettingsDict getDict()  {
        return this.dict;
    }

 

Well, however, I fail to retrieve a corresponding value of said map in my Component...

 

 

<apex:component controller="SubmitCaseController" allowDML="true">
    <apex:attribute name="dict" type="MyDict" description="the dictionary" required="true"/>
    <h1>{!dict.get('someEntry')}</h1>
</apex:component>

 

How am I to access the data in the underlying map from within a Component? Or, how else could I hand down a map from a controller to a page to a component being called form within that page?

 

 

Cheers, Tobias.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can't pass a parameter to a function.  There is support for setting some information into a controller prior to executing an action method via the apex:param component, but that's not really using method parameters.

 

You should be able to access the value from a map based on the key though.  Having just re-read the code that I posted, that's not quite dynamic binding map syntax.  Try:

 

 

<h1>{!dict.dict['someEntry']}</h1>

 

 

All Answers

bob_buzzardbob_buzzard

When you say that you fail to retrieve the data in the component, do you get an error or is it just an empty?

 

I don't think this line:

 

<h1>{!dict.get('someEntry')}</h1>

 

will work, as that is using the Map dynamic binding syntax, but your class isn't a map, its a custom class that happens to have a get that works like a Map.

 

What happens if you access the underlying map,  e.g.

 

<h1>{!dict.dict.get('someEntry')}</h1>
tobibeertobibeer

In both cases, exactly the same thing happens, which is... 

Error: Unknown function dict.get. Check spelling.


or in your case...

Error: Unknown function dict.dict.get. Check spelling.


Is there no way to pass down a parameter to a function call ...or, alternatively, to pass down a (reference to an actual) map to a component for that matter?
 
Cheers, Tobias.
bob_buzzardbob_buzzard

You can't pass a parameter to a function.  There is support for setting some information into a controller prior to executing an action method via the apex:param component, but that's not really using method parameters.

 

You should be able to access the value from a map based on the key though.  Having just re-read the code that I posted, that's not quite dynamic binding map syntax.  Try:

 

 

<h1>{!dict.dict['someEntry']}</h1>

 

 

This was selected as the best answer
tobibeertobibeer
<h1>{!dict.dict['someEntry']}</h1>

Thanks a lot, that worked!

aballardaballard

You may be able to pass a map into a component directly by just specifying the type as 'Object", which is accepted.   I haven't verified if it actually works, however. 

 

I'll create a work item to support type='map', but that will likely be a low priority.