0

I have a VF page in which I am saving and adding the values through two buttons. Buttons are working fine. when i click the add button, a new row is added and when i click on save button,data is saved in Read only mode. But when i refresh the page , the values are saved in edit mode and not in read only mode. So, my questions is how to Save the Data in Read only mode on Page refresh.

Visualforce Page:

<apex:page standardcontroller="Assigned_tasks__c" extensions="TestTaskController1">
<apex:form >
<apex:pageBlock > 
<div id="hide">
 <apex:pageBlockSection title="Input Page" id="pgsec" rendered="{!saved}">
<apex:pageBlockTable value="{!reportlist}" var="acc"> 
<apex:column headerValue="Report Name"> 
<apex:inputField value="{!acc.Report_Name__c}" /> 

 <br /> </apex:column> 
  <apex:column headerValue="Comments">
 <apex:inputField value="{!acc.Comments__c}"/> 
 </apex:column> 
 <br/>
 </apex:pageBlockTable>
  </apex:pageBlockSection>
  </div>
 <apex:pageBlockSection title="Output Page"  rendered="{!saved1}">
 <apex:pageBlockTable value="{!reportlist}" var="acc"> 

 <apex:column headerValue="Report Name"> 
<apex:outputField value="{!acc.Report_Name__c}" />
 </apex:column> <br/>
  <apex:column headerValue="Comments">
<apex:outputField value="{!acc.Comments__c}" /> 
<br /> </apex:column> 

</apex:pageBlockTable>
 </apex:pageBlockSection>
 <apex:pageBlockButtons > 
<apex:commandButton value="Add Report" action="{!addAccount}"/>
 <apex:commandButton value="Save Report" action="{!saveAccount}"/> 
</apex:pageBlockButtons>
 </apex:pageBlock> 
</apex:form>
</apex:page>

Apex Class:

public class TestTaskController1 {

Report_Information__c task = new Report_Information__c();
Public boolean saved {get; set;}
Public boolean saved1 {get; set;}
public String parentId;

public list<Report_Information__c> reportlist{ get; set; }

    public TestTaskController1(ApexPages.StandardController controller) {
              reportlist=new list<Report_Information__c>();
              parentId= controller.getId();
              reportlist = [SELECT id,Report_Name__c,Comments__c,ATask__c from Report_Information__c where ATask__c=:parentId];              
              reportlist.add(task);
              saved=true;
              saved1 = false;


    }


Public void addAccount()
{
Report_Information__c acc = new Report_Information__c();
reportlist.add(acc);
saved=true;
saved1=false;
}


public PageReference saveAccount() {
for(Report_Information__c  re :reportlist)
{

re.ATask__c= parentId;
}

if(reportlist.size()>1){
insert reportlist;
saved=false;
saved1= true;


}

return null;

}}

Visualforce Screenshot: enter image description here

0

Thanks for your reply, but how do i access my wrapper variables outside ? I created a variable from my wrapper class but how do I set my flag to true or false based on this variable ?

 public class TestTaskController2 {

 public class ReportInformation{
 Report_Information__c task = new Report_Information__c();

 Public boolean saved1 {get; set;}
 public String parentId;
  Public boolean saved {get; set;}
  }

 public ReportInformation rep {get;set;} ----> variable for my wrapper class
 public list<Report_Information__c> reportlist{ get; set;}
 public TestTaskController2(ApexPages.StandardController controller) {


          reportlist=new list<Report_Information__c>();
          rep.parentId= controller.getId();
          reportlist = [SELECT id,Report_Name__c,Comments__c,ATask__c from Report_Information__c where ATask__c=:rep.parentId];              
          reportlist.add(rep.task);
          saved = false; ----> how to set this flag using my wrapper class ?

}
| improve this answer | |
0

You should have a wrapper class inside your controller that holds the custom object row and the flag:

public class TestTaskController1 {

   public class ReportInformation{
     public Report_Information__c task{get;set;}
     public Boolean saved{get;set;} 
   }

   public List<ReportInformation> reportInfos{get;set;}
 ...
}

Then on your page, you can display read-only vs editable view of the row like this:

<apex:pageBlockTable value="{!reportInfos}" var="rInfo">
      <apex:column headerValue="Report Name">
         <apex:outputField value="{!rInfo.task.Report_Name__c}" rendered="{!rInfo.saved}"/>
         <apex:inputField value="{!rInfo.task.Report_Name__c}" rendered="{!NOT(rInfo.saved)}"/>
      </apex:column>
      <br/>
      <apex:column headerValue="Comments">
           <apex:outputField value="{!rInfo.task.Comments__c}" rendered="{!rInfo.saved}"/>
           <apex:inputField value="{!rInfo.task.Report_Name__c}" rendered="{!NOT(rInfo.saved)}"/>
           <br /> 
      </apex:column>
</apex:pageBlockTable>

UPDATE:

In the constructor, set them all to false, so the input fields are displayed. Then in your action method, set it to true. You should have a list of ReportInformation items like I have in my answer. Iterate over your results from Report_Information__c and then add to the reportInfos list.

  for(Report_Information__C ri: [select id,... from Report_Information__C])
  {
   ReportInformation repInfo = new ReportInformation();
   repInfo.saved = false;
   repInfo.task = ri;
   repInfos.add(repInfo);
 }

HTH

| improve this answer | |
  • 0 down vote accept Thanks for your reply, but how do i access my wrapper variables outside ? I created a variable from my wrapper class but how do I set my flag to true or false based on this variable ? – user8305 May 20 '16 at 8:51
  • I am not able to understand, in my save action method or add action method I use it ?? – user8305 May 25 '16 at 8:04

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.