4

I am trying to access a Map<String,Map<String,Integer> in VF but I dont want to use repeat tag, as I know we can access a Map<String,String> as {!MapVal['Key']} in a page without any error.

but for the Map<String,Map<String,Integer> when I do this {!MapVal['Key']} it through a error as value not found in map. But at lest I should get the inside Map!!

Any Help!!

   <apex:page controller="JqueryDragDropController" id="pageId" showHeader="false">

<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
    $j = jQuery.noConflict();
    var count = 0;
    var drArray = null ;
    $j(document).ready(function() {  
        drArray = $j( "[Id*='06690000003mytx:draggableMaster']" );
        drArray.draggable();
        callAction();
        var cursorAt = drArray.draggable( {
            start: function(event,ui) {
                var drbutton = $j( "[Id*='droppossition']" );
                drbutton.text("");
            },
            stop: function(event,ui) {
                var drbutton = $j( "[Id*='droppossition']" );
                drbutton.text("top:"+ui.position.top+" left:"+ui.position.left);
                callAction();
            }
            });

    });

    function revertAll(){
        // setter

        if(count == 0){
            drArray.draggable( "option", "revert", true);
            count = 1;
        }
        else{
            drArray.draggable( "option", "revert", false);
            count = 0;
        }
    }

    function callAction(){
        var valuesF = "";
        for(i = 0; i < drArray.length; i++){
            var offset = drArray.eq(i).offset();
            valuesF += drArray[i].id+","+offset.left+","+offset.top+";";
        }
        echo(valuesF);
    }

</script>
<style>
    .divDragBody{
        color:black;
        background-color:red;
        height:150px;
        width:150px;
        text-align:center;
        border-style:solid;
        float:left;
        margin:0px 0px 0px 20px;
        z-index:2;
    }

    .divDragChild{
        color:black;
        background-color:white;
        height:100px;
        width:100px;
        text-align:center;
        margin-left:auto;
        margin-right:auto;
        border-style:solid;
        z-index:4;
    }
    #draggable { width: 150px; height: 150px; padding: 0.5em; }
</style>

<apex:form id="formId">
    <apex:actionFunction name="echo" action="{!echoVal}" reRender="formId" >
        <apex:param name="JQIDPossition" assignTo="{!pageJQIdPoss}" value="" />
    </apex:actionFunction>

    <apex:repeat value="{!CurrentJQPoss}" var="in">
        {!in}<br></br>
        <apex:outputText value="{!CurrentJQPoss[in].Top}"/><br></br>
        <apex:outputText value="{!CurrentJQPoss[in].Left}"/><br></br>
    </apex:repeat>
 <apex:outputText value="{!CurrentJQPoss['06690000003mytx:draggableMaster1']['Left']}"/>
</apex:form>
    <button type="button" onclick="revertAll()">Revert</button>  

    <div id="{!JQElementId[0]}" class="divDragBody" style="left:0px;top:0px;">
      Drag Element 1 
    </div> 

    <div id="{!JQElementId[1]}" class="divDragBody">
      Drag Element 2 {!mpStr['11']}
    </div>
    <div id="droppossition">
    </div> 


</apex:page>

controller:

    global with sharing class JqueryDragDropController {
    public Map<String,JqueryDragDrop_PointSave__c> JQPossition {get;set;}
    public Map<String,Map<String,Integer>> CurrentJQPoss{get;set;}
    public Map<String,String> mpStr {get;set;}
    public List<String> JQElementId{get;set;}
    public Id SFDCId = UserInfo.getOrganizationId();
    public String preIdCode{get;set;}
    public String pageJQIdPoss{get;set;}

    public JqueryDragDropController (){
        JQPossition = new Map<String,JqueryDragDrop_PointSave__c> ();
        CurrentJQPoss = new Map<String,Map<String,Integer>> ();
        mpStr = new Map<String,String> ();
        mpStr.put('11','***eleven');
        preIdCode = '06690000003mytx';
        JQElementId = new List<String>();
        JQElementId.add(preIdCode+':draggableMaster1');
        JQElementId.add(preIdCode+':draggableMaster2');
    }

    public void fillJQPossiotion(){
        JQPossition = JqueryDragDrop_PointSave__c.getAll();

            List<JqueryDragDrop_PointSave__c > JQPLstInsert = new List<JqueryDragDrop_PointSave__c >();
            List<JqueryDragDrop_PointSave__c > JQPLstUpdate = new List<JqueryDragDrop_PointSave__c >();

            for(String str: JQElementId){
                if(JQPossition.containsKey(str) == false){
                    JqueryDragDrop_PointSave__c jqp = new JqueryDragDrop_PointSave__c();
                    jqp.Name = str;
                    jqp.Element_Left__c = string.valueof(CurrentJQPoss.get(str).get('Left'));
                    jqp.Element_Top__c = string.valueof(CurrentJQPoss.get(str).get('Top'));
                    JQPLstInsert.add(jqp);
                }
                else{
                    JqueryDragDrop_PointSave__c jqp = new JqueryDragDrop_PointSave__c();
                    jqp = JQPossition.get(str);
                    jqp.Element_Left__c = string.valueof(CurrentJQPoss.get(str).get('Left'));
                    jqp.Element_Top__c = string.valueof(CurrentJQPoss.get(str).get('Top'));
                    JQPLstUpdate.add(jqp);
                }
            }
            if(!JQPLstInsert.isEmpty()){
                insert JQPLstInsert ;
            }    
            else if(!JQPLstUpdate.isEmpty()){
                system.debug('***update'+JQPLstUpdate);
                update JQPLstUpdate;
            }

    }

    public void echoVal(){
        system.debug('***pageJQIdPoss**'+pageJQIdPoss);
        String subStr = '';
        for(Integer i=0;pageJQIdPoss!='';i++){
            subStr = pageJQIdPoss.substring(0,pageJQIdPoss.indexOf(';')+1);
            system.debug('***subStr **'+subStr );
            string subStrId = subStr.substring(0,subStr.indexOf(','));
            system.debug('***subStrId **'+subStrId );
            String subStrTop = subStr.substring(subStr.indexOf(',')+1,subStr.lastindexOf(','));
            system.debug('***subStrTop **'+subStrTop );
            String subStrLeft = subStr.substring(subStr.lastindexOf(',')+1,subStr.indexOf(';'));
            system.debug('***subStrLeft **'+subStrLeft );
            Map<String,Integer> mp = new Map<String,Integer>();
            mp.put('Left',Integer.valueof(subStrLeft) );
            mp.put('Top',Integer.valueof(subStrTop) );
            CurrentJQPoss.put(subStrId ,mp);
            pageJQIdPoss = pageJQIdPoss.remove(subStr); 
            system.debug('***CurrentJQPoss**'+CurrentJQPoss);

        }
        fillJQPossiotion();
    }

}
  • Please post a code sample showing your problem given that Martin's answer is not working for you. – Mike Chale Jan 30 '14 at 16:18
  • @MikeChale i have updated the code here.. – user614946 Jan 30 '14 at 16:41
2

Try :

 {!MapVal['Key']['KeyMapB']} 

Complete sample:

Apex

   Map<String,Integer> x = new Map<String,Integer>();
    x.put('b',3);

    Map<String,Map<String,Integer>> m = new Map<String,Map<String,Integer>>();
    m.put('a',x);

VF

   {!m['a']['b']} >>> this will show 3
  • hi i tried as {!CurrentJQPoss['06690000003mytx']['Top'] but it gives error as "Map key 06690000003mytx not found in map" but the value is there. – user614946 Jan 30 '14 at 15:45
  • Are you sure that the key 06690000003mytx exists?, because the error is saying that the problem is in the key of the first map, not the second one. Try first something simpler (map filled by constants) – Martin Borthiry Jan 30 '14 at 15:53
  • the map is actually filled with constants the values are displaying on page as: {06690000003mytx:draggableMaster1={Left=15, Top=20}, 06690000003mytx:draggableMaster2={Left=15, Top=196}} this is the map values and i am doing this : {!CurrentJQPoss['06690000003mytx:draggableMaster1']['Left']} – user614946 Jan 30 '14 at 15:55
  • yes I have done that but nothing same error, se my above comment – user614946 Jan 30 '14 at 16:03
  • I've updated my answer using a simple example that is working. – Martin Borthiry Jan 30 '14 at 16:06

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.