Skip to main content
Greg Coogan a posé une question dans #Visualforce
I have written a custom controller and Visualforce page that displays records from a custom object, and related records, in a few HTML tables. I have a checkbox on the page and a Save button to display the refined results. Do I need to write a function that returns a modified list of records, basically copying what is in my controller's main class to a "PageReference save()" class? Ideally, the button would result in the controller running again with the updated value of the checkbox. I've seen examples but having trouble. Please help.
5 réponses
  1. 3 avr. 2017, 17:25
    Hi Greg,

    I  have made some minor changes in your controller class to call the data fetching in both constructor as well as Save() method.

    Here  is the code.

    public class TestCaseController {

    public class Lists{

    public Test_Case__c testcase{get;set;}

    public List<Related_Test_Record__c> test1{get;set;}

    public Lists(){

    this.testcase = new Test_Case__c();

    this.test1 = new List<Related_Test_Record__c>();

    }

    }

    public List<Lists> lstList{get;set;}

    //public List<Lists> lstTestCases{get;set;}

    public List<Lists> lstTest1{get;set;}

    public Boolean cbTest{get;set;}

    public PageReference save(){

    fetchData();

    return null;

    }

    public TestCaseController(){

    if(cbTest == null)

    cbTest = false;

    if(lstList == null)

    lstList = new List<Lists>();

    //lstTestCases = new List<Lists>();

    fetchData();

    }

    public void fetchData(){

    //cbTest = System.currentPageReference().getParameters().get('cbTest');

    List<Test_Case__c> lstTestCases = [SELECT Id, Name,

    RecordType.Name, Status__c

    (SELECT Id FROM Related_Test_Record__c),

    FROM Test_Case__c];

    if(lstTestCases.size()>0){

    for(Test_Case__c testcase:lstTestCases){

    Lists lst = new Lists();

    lst.testcase = testcase;

    if(testcase.Related_Test_Record__r.size() > 0){

    lst.test1 = testcase.Related_Test_Record__r;

    }

    if(cbTest == true && testcase.Top_List__c == true){

    lstList.add(lst);

    //cbTest == null is included because I wanted records to render even if my custom checkbox isn't passing a value currently

    } else if(cbTest == false){

    lstList.add(lst);

    }

    }

    }

    }

    }

    And, I suggest you use reRender option on the Save button, to rerender your second section.

    Line 26 of your VF page would be like this.

     

    <apex:pageBlock title="Case Report" id="CaseReport">

    And, line 21 would be..

    <apex:commandButton action="{!save}" value="Update Report" rerender="CaseReport"/>

    Let me know if this works for you.

     
0/9000