Find your content:

Search form

You are here

How do I access a Map in my Visualforce page?

 
Share

I have a custom controller that returns a map like this:

Map<String, String>

In my visualforce page, how do I access this map? Before the Winter '13 release, I could do this:

<apex:variable value="{!results[key]}" var="result"/>

However, this no longer works with the Winter '13 release. How do I do this in the latest release?

Edit: The VF page is at API version 24.0. The Controller is at 25.0.

This is in my Visualforce Page:

<c:Query_article_view>
    <apex:variable value="{!results[articleid]}" var="result"/>
        {!result}
</c:Query_article_view>

This is my component:

<apex:component controller="Article_View" access="global">


    <apex:variable value="{!results}" var="results"/>
    <apex:componentBody />
</apex:component>

This is my Apex Class:

global with sharing class Article_View {

    global Map<String, String> results {
        get {
            KnowledgeArticleViewStat result;
            List<KnowledgeArticleViewStat> results = [SELECT NormalizedScore, Id, ParentId FROM KnowledgeArticleViewStat WHERE Channel='Csp'];
            Map<String, String> article_views = new Map<String, String>();
            for (Integer i = 0; i < results.size(); i++) {
                 article_views.put(((String)results[i].get('ParentId')).substring(0,15) , String.valueOf(((Decimal)results[i].get('NormalizedScore')).round()));
            }
            return article_views;
        } 
        set; 
    }
}

When I try and save my Visualforce Page, I get this error: Error: Expression of type Text cannot be subscripted


Attribution to: Di Zou

Possible Suggestion/Solution #1

I have a page and controller on version 25.0, and this example work for me where 'Gender' & Type are the keys in a map called results

public with sharing class Article_View {

public Map<String, String> results;

public Map<String, String> getresults () {
         results = new Map<String, String> ();
         results.put ('Gender', 'All');
         results.put ('Type', 'All');

         return results;
    } 

}

<apex:page controller="Article_View" >

<apex:variable value="{!results['Gender']}" var="result"/>
   Gender: {!result}

       <br/>

<apex:variable value="{!results['Type']}" var="type"/>
        Type: {!type}        

</apex:page>

Attribution to: BritishBoyinDC

Possible Suggestion/Solution #2

It appears that the VF compiler thinks that variable inside the component is returning the key to the map instead of the results map. If you try this with

map <Integer,String> 

you get an error that an integer can't be subscripted.

Can you pass in the key as an attribute to the component? Ex:

component:

<apex:component controller="Article_View" access="global">
<apex:attribute name="key" description="key for results" type="String" required="false"/>

 <apex:variable value="{!results[key]}" var="result"/> 
<apex:componentBody />

page:

<c:Query_article_view key="{!articleid}">

    {!result}
</c:Query_article_view>

Attribution to: Greg Grinberg
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3347

My Block Status

My Block Content