+ Start a Discussion
cristcrist 

Urgent-----Help required on using map on VF using apex:repeat?

Hi,

 

 I need a help on using Map on VF page .

 

public Map<Integer, String> bucketOrderMap{get;set;}

 

String bucketQuery = 'select Id, Name, Tutorial_Bucket_Unique_Name__c, Sort_Order__c from Tutorial_Bucket_Order__c where Name like \''+ tCategory.getName() +'\'';// and sort_order__c >='+ (currentBucket.sort_order__c - 1) + ' and sort_order__c <= '+currentBucket.sort_order__c + 1 ;       

 

bucketOrderMap = new Map<Integer, String>();               

for(Tutorial_Bucket_Order__c  curr:Database.query(bucketQuery))  {           

bucketOrderMap.put(Integer.valueOf(curr.sort_order__c), curr.Tutorial_Bucket_Unique_Name__c);     

  }

 

ON VF=========

 

 <ul>                         

  <apex:repeat value="{!bucketOrderMap}" var="c">         

          <apex:outputLabel value="{!c.Label}" />                     

          <!-- <li id="buck_{!c.}">{!c.Label}</li> -->               

    </apex:repeat>       

  </ul>  

 

In Vf page i got prob. can any body help me how to do it on this withour <apex:pageblock table>

 

thanks,

kp

Best Answer chosen by Admin (Salesforce Developers) 
ritika@developerforceritika@developerforce

Hello,

 

As a rule, a map is not sorted. Event though you put the order as the key in a map, it will be traversed randomly only.

If you need to maintain the order, use lists and sort in the soql query itself.

 

public with sharing class TutorialController {

	public List<String> bucketOrderList{get;set;}

	public MarketoTutorialController(ApexPages.StandardController stdController){

        article = (Tutorial__kav) stdController.getRecord();
bucketOrderList= [select Id, Name, Tutorial_Bucket_Unique_Name__c, Sort_Order__c from Tutorial_Bucket_Order__c where Name =: tCategory.getName() order by Sort_Order__c];

and in VF page

 

<ul>
                       
      <apex:repeat value="{!bucketOrderList}" var="c">
       <li id="buck_{!c.Tutorial_Bucket_Unique_Name__c}">{!c.Sort_Order__c}  {!c.Tutorial_Bucket_Unique_Name__c}</li> 
      </apex:repeat> 
                           
   <!-- <apex:repeat value="{!tCategory.childCategories}" var="c">
   <li id="buck_{!c.Name}">{!c.Label}</li>  
        </apex:repeat> -->
 </ul>   

This should give you the list sorted. Please make sure of any syntax errors / variable naming errors before running this.

 

Hope this helps.

Regards,

Ritika

All Answers

Ankit AroraAnkit Arora

Though am not sure what exactly you want to ask. But I have prepared a sample for you, please use this and let me know if it doesn't resolves your problem.

 

VFP:

<apex:page controller="DisplayMapValue">
    <apex:pageBlock >

        <!-- With PageBlock Tabel -->
        <apex:pageBlockSection >
            <apex:pageBlockTable value="{!accMap}" var="val">
                <apex:column value="{!accMap[val].Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        
        <!-- Without PageBlock Tabel -->
        <apex:pageBlockSection >
            <apex:repeat value="{!accMap}" var="val">
                <apex:outputLabel value="{!accMap[val].Name}"/>
            </apex:repeat>
        </apex:pageBlockSection>
        
        <!-- Without PageBlock Tabel and Without repeat -->
        <apex:pageBlockSection >
            <apex:outputLabel value="{!intMap[1]}"/>
        </apex:pageBlockSection>
        
    </apex:pageBlock>
</apex:page>

 

Class :

public class DisplayMapValue
{
    public Map<Id , Account> accMap {get; set;}
    
    public Map<Integer , Integer> intMap {get; set;}
    
    public DisplayMapValue()
    {
        accMap = new Map<Id, Account>([select id , name from account limit 10]) ;

        intMap = new Map<Integer , Integer>() ;
        intMap.put(1 , 1) ;
        intMap.put(2 , 2) ;
    }
}

 Read the comments on visualforce page.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

cristcrist

Thanks For the reply.. But its not solving my prob...

 

Actualy my prob is .. I created a map as follows..

 

public with sharing class TutorialController {

	public Map<Integer, String> bucketOrderMap{get;set;}

	public MarketoTutorialController(ApexPages.StandardController stdController){

        article = (Tutorial__kav) stdController.getRecord();	
       String bucketQuery = 'select Id, Name, Tutorial_Bucket_Unique_Name__c, Sort_Order__c from Tutorial_Bucket_Order__c where Name like \''+ tCategory.getName() +'\'';

        bucketOrderMap = new Map<Integer, String>();
        
        for(Tutorial_Bucket_Order__c  curr:Database.query(bucketQuery)){
            bucketOrderMap.put(Integer.valueOf(curr.sort_order__c), curr.Tutorial_Bucket_Unique_Name__c);
        }

 

map is giving me the correct value like...

 

============bucketOrderMap========={1=Manage_Tags, 2=Manage_Channels}

 

now my prob is how to show these values on a VF page.

 <ul>
                       
      <apex:repeat value="{!bucketOrderMap}" var="c">
       <li id="buck_{!c}">{!c}</li> 
      </apex:repeat> 
                           
   <!-- <apex:repeat value="{!tCategory.childCategories}" var="c">
   <li id="buck_{!c.Name}">{!c.Label}</li>  
        </apex:repeat> -->
 </ul>   

 

if i run the commented part than it will run ok bt the prob is its not giving me the sorted order of the tutorials..

 so because of sorting i used map and know i dnt knw how to use this in VF page...

 

 

I hope now i explain myself..

 

if still have any doubt .. i will explain it more..

 

Thanks,

Crist

 

ritika@developerforceritika@developerforce

Hello,

 

As a rule, a map is not sorted. Event though you put the order as the key in a map, it will be traversed randomly only.

If you need to maintain the order, use lists and sort in the soql query itself.

 

public with sharing class TutorialController {

	public List<String> bucketOrderList{get;set;}

	public MarketoTutorialController(ApexPages.StandardController stdController){

        article = (Tutorial__kav) stdController.getRecord();
bucketOrderList= [select Id, Name, Tutorial_Bucket_Unique_Name__c, Sort_Order__c from Tutorial_Bucket_Order__c where Name =: tCategory.getName() order by Sort_Order__c];

and in VF page

 

<ul>
                       
      <apex:repeat value="{!bucketOrderList}" var="c">
       <li id="buck_{!c.Tutorial_Bucket_Unique_Name__c}">{!c.Sort_Order__c}  {!c.Tutorial_Bucket_Unique_Name__c}</li> 
      </apex:repeat> 
                           
   <!-- <apex:repeat value="{!tCategory.childCategories}" var="c">
   <li id="buck_{!c.Name}">{!c.Label}</li>  
        </apex:repeat> -->
 </ul>   

This should give you the list sorted. Please make sure of any syntax errors / variable naming errors before running this.

 

Hope this helps.

Regards,

Ritika

This was selected as the best answer
cristcrist

Thanks for the reply...

 

 

I also try this after this post and it works and when m going to write this your reply is there...

 

 

Thanksssssss alot....