0

I am in a situation where i have a map in my Apex code and i want to access its data with a key that i am passing through my javascript function. I tried with a couple of things that include directly putting js variable as a key but it gives error :

Error: Unknown property

I also tried with assigning the value of javascript variable to visualforce variable within my javascript if else confitions. but that doesnt work too.. here is my code snippit.

    if( strName == 'ABC' ){
            alert(strSurveyName);
            <apex:variable var="keySurveyName" value = "ABC" />
    }else if( strName == 'XYZ' ){
            alert(strSurveyName);
            <apex:variable var="keySurveyName" value = "XYZ" />
    }

console.log("{!keySurveyName}");

<apex:repeat value="{!StatusObjMap[keySurveyName]}" var="temp">
.....
.....
.....

In both the conditions it displays XYZ in output for the last debug statement. Any help would be highly appriciated.

  • The code snippet you have posted shows that whichever value you have for the javascript variable strName the same value will be alerted - strSurveyName. The console log statement doesn't reference those JS variables, it references an Apex variable called keySurveyName. Is there more code you can show? – Dominic Oct 31 '14 at 9:31
  • Visualforce {!} expressions are evaluated when the page is initially generated on the server and JavaScript runs in the browser (later on) against the results of that generation. So I think you will have to re-think your approach, perhaps to use Visualforce re-rendering (or to use more JavaScript). – Keith C Oct 31 '14 at 9:36
  • @Dominic Thanks for showing concern, All i want to do is access my apex map with a javascript variable as a key.. – ScoRpion Oct 31 '14 at 9:41
  • @KeithC U may be right.. what u mean is i need to call functions in apex rather rendering an apex variable that is loaded prior to javascripts existance on page – ScoRpion Oct 31 '14 at 9:44
2

You can't use a javascript variable as a visualforce key to an apex map, because the Visualforce is evaluated server side, then rendered down to the browser where the javascript runs. The Visualforce renderer does not know about or understand your javascript variables.

You will need to think of a different way of solving your problem.

EDIT: which is what @KeithC said in his comment...

2

Not sure if you've got the answer. However, I would like to point out few things.

  1. JS variables cannot be used with "apex:" components.
  2. <apex:variable> cannot be re-declared. In your example, you have used JS block to declare same "apex:variable" based on some conditions. But VF will always use the last computed value. Basically this is how it is going to work: While rendering the VF page, it will execute all the "apex:" components irrespective of it declaration. In your case, even though you have if-else condition, the "apex:variable" will be executed in all the blocks and the final declaration will stand good. So, your "keySurveyName" value will always be "XYZ".

So the only way to use a "apex:variable" with some conditions is to declare it only once and use "Apex" expressions (with the help of Apex controller fields) to set the value based on conditions (again, JS cannot be used in expressions).

I am not sure what you are trying to do with your code snippet. You are assigning a value from the "map" into "apex:repeat". Unless your "map"'s value is a collection, it wouldn't make much sense.

Having said all these, you can do the following things:

  1. Declare "apex:variable" either in JS code or in regular DOM and use that variable to access "map" value. Example:
        <apex:variable var="mykey" value="abc" />
        <apex:repeat value="{! myMap[mykey]}" var="val">
            .. do something.
        </apex:repeat>

Or with JS:

<script>
    <apex:variable var="mykey" value="abc" /> 
</script>
<apex:repeat value="{! myMap[mykey]}" var="val">
    .. do something.
</apex:repeat>
  1. If you want to use Apex map values in JS then you can do as follows:
        <script>
            // assuming "HashMap" is a JS map implemenation with put/get functions.
            var myJsMap = new HashMap(); 
            $(function() { // jQuery is being used.
                console.log(myJsMap);
            });

        </script>

        <apex:repeat value="{! myMap}" var="myKey">
            <script>
                myJsMap.put('{! myKey}', '{! myMap[myKey]}');
            </script>
        </apex:repeat>

Hope these info help.

  • or better, keep it as one script block and use []= to set map values... but make sure to escape your myKey and myMap[myKey] values: <script type="text/javascript"> <apex:repeat value="{! myMap}" var="myKey"> myJsMap['{! JSENCODE(myKey)}'] = '{! JSENCODE(myMap[myKey])}'; </apex:repeat> </script> – Charles Koppelman Jan 8 '15 at 21:00
0

You can access the map like this

var mymapvale='{!yourmap['yourjavascriptvariable']}';
  • thats what i have mentioned in my question, that doesnt work , can u modify my snippit so that i can understand what u exactly mean – ScoRpion Oct 31 '14 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.