[SalesForce] Render columns in page block table based on method

I have a pageblock table with 2 columns.. Interests(custom field) and another column – just a static text that will be rendered based on some condition from a method.

<apex:pageBlockTable value="{!engListWrap}" var="item">
<apex:column >        
       <label>Interests</label>
       <apex:outputText value="{!item.eng.Interests_Top__c}" />
</apex:column>
<apex:column > 
       <apex:inputCheckbox value="{!item.selected}" rendered="{!NOT(checkForSavedMatch)}"/>  
       <apex:outputText value="Saved" rendered="{!checkForSavedMatch}" />                    
 </apex:column>
</apex:pageBlockTable>

Here is the method Im calling in the rendered property

public boolean getCheckForSavedMatch(){  
     List<Match__c> savedMatches = new List<Match__c>();   
     for(EngagementWrapper engwrapper : engListWrap)
        {                        
            savedMatches = [SELECT Id FROM Match__c WHERE Engagement__c=:engwrapper.eng.Id LIMIT 1];
        }      
        if(savedMatches.size()>0){
            return true;
        }
        else
            return false;
 }

My output is 4 rows and 2 columns (4 rows : engListWrap had 4 records).

The problem here is I have pass that item.eng.Id into renderedMethod CheckForSavedMatches. So, for that particular eng.Id, I will run a query based on that engId and return true or false. I need to use that eng.Id in the WHERE condition of the query in that method.

How do I pass that engId to the method? I tried apex:param, but didn't really work. Any idead how to proceed? I want to know whats the current item that we are looping through.

Help please.

Here is the wrapper code

public class EngagementWrapper
{
    public Engagement__c eng{get; set;}
    public Boolean selected{get; set;}                

    public EngagementWrapper(Engagement__c e)
    {
       eng = e;
       selected = false;

       System.debug('*****e:'+e);
       System.debug('*****selected:'+selected);
    }
}

Best Answer

When you initialise your wrapper items, add the following code after that in order to identify which of your items should be selected or not.

Set <Id> savedMatchIds = new Set <Id> ();
Set <Id> engagementIds = new Set <Id> ();

for (EngagementWrapper engwrapper : engListWrap)
{
    engagementIds.add(engwrapper.eng.Id);
}

for (Match__c match : [SELECT Id, Engagement__c FROM Match__c WHERE Engagement__c IN :engagementIds AND Status__c = 'Saved'])
{
    savedMatchIds.add(match.Engagement__c);
}

for (EngagementWrapper engwrapper : engListWrap)
{
    if (savedMatchIds.contains(engwrapper.eng.Id))
    {
        engwrapper.selected = true;
    }
    else
    {
        engwrapper.selected = false;
    }
}

Then in your page simply render the column based on the "selected" variable:

<apex:column rendered="{!item.selected}">

I don't know what else is happening in your code, but based on what you've provided so far in your question, this should work.

Related Topic