function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vinay JVinay J 

render and rerender

Dear friends,

I have a map(integer, list) and a picklist on page with these integers (plus some extra values not present as kep in map). When user selects a value, related list should be diplayed in pageblocktable. If there's no list associated, error should be displayed and pageblocktable should not be visible at all. I have written below code which works file till user selects a value not present as key in the map. Also, in real scenario, it is possible that there's no data inobject and hence, map is empty.. in that case, neither the pageblocktable, nor the picklist should be visible. Can someone please help. It's quite urgent. Below is the code..

*********** Page ************

<apex:page controller="test">
<apex:form >

    <apex:pageMessage detail="{!sError}" severity="Warning" rendered="{!sError <> null}" strength="1" title="No Data" id="message"/> 
   
    <apex:outputPanel rendered="{!isDataPresent == true}">
    <apex:pageblock id="table" >
    <apex:pageblocktable value="{!Data}" var="m"  >
       
        <apex:column value="{!m}" headerValue="Text"/>
    </apex:pageblocktable>
    </apex:pageblock>
    </apex:outputPanel>

    <apex:outputLabel value="Payment Year" ></apex:outputLabel> &nbsp; &nbsp;
    <apex:selectlist id="pmname" value="{!selectedNumber }" multiselect="false" size="1" >
        <apex:selectoptions value="{!listYearsForPage }" >
        </apex:selectoptions>
        <apex:actionSupport event="onchange" reRender="table,message"/>
    </apex:selectlist>


</apex:form>
</apex:page>


**************** Controller ****************

public class test {
   
    public List<SelectOption> listYearsForPage {get;set;}
    public string selectedNumber {get;set;}
    public boolean isDataPresent {get;set;}
    public string sError {get;set;}
   
    public test()
    {
        listYearsForPage = new List<SelectOption>();
        listYearsForPage.add(new selectoption('1','1'));
        listYearsForPage.add(new selectoption('2','2'));
        listYearsForPage.add(new selectoption('3','3'));
        listYearsForPage.add(new selectoption('4','4'));
        isDataPresent = true;   
    }
   
    public map<integer, list<string>> perpData()
    {
        map<integer, list<string>> a = new map<integer, list<string>>();
        a.put(1,new List<String>{'a','b','c'});
        a.put(2,new List<String>{'d','e','f'});
        a.put(3,new List<String>{'g','h','i'});
       
        return a;
    }
   
    public list<string> getData()
    {
        sError = null;
        list<string> l = new list<String>();
        if(selectedNumber == null)
            selectedNumber = '1';
        l = perpData().get(Integer.valueOf(selectedNumber));
        if(l != null)
            isDataPresent = true;
        else {
            isDataPresent = false;
            sError = 'No data';
        }   
        return l;
    }
   
}
Best Answer chosen by Vinay J
SarfarajSarfaraj
Hi Vinay

Here is the code,
<apex:page controller="test">
<apex:form id="form" >
    <apex:outputPanel id="message">
        <apex:pageMessage detail="{!sError}" severity="Warning" rendered="{!sError <> null}" strength="1" title="No Data"/> 
    </apex:outputPanel>   
    <apex:outputPanel id="table">
    <apex:pageblock rendered="{!isDataPresent && isMapNonEmpty}">
    <apex:pageblocktable value="{!Data}" var="m"  >
       
        <apex:column value="{!m}" headerValue="Text"/>
    </apex:pageblocktable>
    </apex:pageblock>
    </apex:outputPanel>
    
    <apex:outputLabel value="Payment Year"  rendered="{!isMapNonEmpty}" ></apex:outputLabel> &nbsp; &nbsp;
    <apex:selectlist id="pmname" value="{!selectedNumber }" multiselect="false" size="1" rendered="{!isMapNonEmpty}" >
        <apex:selectoptions value="{!listYearsForPage }" >
        </apex:selectoptions>
        <apex:actionSupport event="onchange" action="{!reset}" reRender="form"/>
    </apex:selectlist>
</apex:form>
</apex:page>
public class test {
    public Boolean isMapNonEmpty { get; private set; }
    public list<string> Data{get;private set;}
    private map<integer, list<string>> dataMap;
    public void reset() {
        if(resetAll)
            sError = null;
        list<string> l = new list<String>();
        if(selectedNumber == null)
            selectedNumber = '1';
        l = dataMap.get(Integer.valueOf(selectedNumber));
        if(l != null)
            isDataPresent = true;
        else {
            isDataPresent = false;
            sError = 'No data';
        }   
        data = l;
        resetAll = true;
    }

   
    public List<SelectOption> listYearsForPage {get;set;}
    public string selectedNumber {get;set;}
    public boolean isDataPresent {get;set;}
    public string sError {get;set;}
    private Boolean resetAll;
    public test()
    {
        listYearsForPage = new List<SelectOption>();
        listYearsForPage.add(new selectoption('1','1'));
        listYearsForPage.add(new selectoption('2','2'));
        listYearsForPage.add(new selectoption('3','3'));
        listYearsForPage.add(new selectoption('4','4'));
        prepData();
        resetAll = false;
        reset();
    }
   
    public void prepData()
    {
        dataMap = new map<integer, list<string>>();
        dataMap.put(1,new List<String>{'a','b','c'});
        dataMap.put(2,new List<String>{'d','e','f'});
        dataMap.put(3,new List<String>{'g','h','i'});
        isMapNonEmpty = true; //If map is empty put false to hide the table and picklist.
        if(!isMapNonEmpty)
            sError = 'No Data';
    }
   
}



All Answers

Bhushan.AdhikariBhushan.Adhikari
Hi,

You have hardcode selectedNumber = '1'; so it will always return u the first list. 
regarding ur scenario, u need to check if the map is empty or not.. if its not empty then u will have to check whether that map contains the selected number as the key, if it does then only do 'l = perpData().get(Integer.valueOf(selectedNumber));' else u can show appropirate message.


Vinay JVinay J
I have hardcoded so that I can expain the problem. I know the concept, but unable to implement.. Can you please execute the code and then reply with solution.
SarfarajSarfaraj
Hi Vinay

Here is the code,
<apex:page controller="test">
<apex:form id="form" >
    <apex:outputPanel id="message">
        <apex:pageMessage detail="{!sError}" severity="Warning" rendered="{!sError <> null}" strength="1" title="No Data"/> 
    </apex:outputPanel>   
    <apex:outputPanel id="table">
    <apex:pageblock rendered="{!isDataPresent && isMapNonEmpty}">
    <apex:pageblocktable value="{!Data}" var="m"  >
       
        <apex:column value="{!m}" headerValue="Text"/>
    </apex:pageblocktable>
    </apex:pageblock>
    </apex:outputPanel>
    
    <apex:outputLabel value="Payment Year"  rendered="{!isMapNonEmpty}" ></apex:outputLabel> &nbsp; &nbsp;
    <apex:selectlist id="pmname" value="{!selectedNumber }" multiselect="false" size="1" rendered="{!isMapNonEmpty}" >
        <apex:selectoptions value="{!listYearsForPage }" >
        </apex:selectoptions>
        <apex:actionSupport event="onchange" action="{!reset}" reRender="form"/>
    </apex:selectlist>
</apex:form>
</apex:page>
public class test {
    public Boolean isMapNonEmpty { get; private set; }
    public list<string> Data{get;private set;}
    private map<integer, list<string>> dataMap;
    public void reset() {
        if(resetAll)
            sError = null;
        list<string> l = new list<String>();
        if(selectedNumber == null)
            selectedNumber = '1';
        l = dataMap.get(Integer.valueOf(selectedNumber));
        if(l != null)
            isDataPresent = true;
        else {
            isDataPresent = false;
            sError = 'No data';
        }   
        data = l;
        resetAll = true;
    }

   
    public List<SelectOption> listYearsForPage {get;set;}
    public string selectedNumber {get;set;}
    public boolean isDataPresent {get;set;}
    public string sError {get;set;}
    private Boolean resetAll;
    public test()
    {
        listYearsForPage = new List<SelectOption>();
        listYearsForPage.add(new selectoption('1','1'));
        listYearsForPage.add(new selectoption('2','2'));
        listYearsForPage.add(new selectoption('3','3'));
        listYearsForPage.add(new selectoption('4','4'));
        prepData();
        resetAll = false;
        reset();
    }
   
    public void prepData()
    {
        dataMap = new map<integer, list<string>>();
        dataMap.put(1,new List<String>{'a','b','c'});
        dataMap.put(2,new List<String>{'d','e','f'});
        dataMap.put(3,new List<String>{'g','h','i'});
        isMapNonEmpty = true; //If map is empty put false to hide the table and picklist.
        if(!isMapNonEmpty)
            sError = 'No Data';
    }
   
}



This was selected as the best answer
Vinay JVinay J
Thanks Man.. that's exactly what I was looking for.. and I understood the mistake I was doing.. Thank you very much friend.. you saved the day for me..