How to find out which checkboxes have been selected on the next page in VisualForce?

后端 未结 2 1347
死守一世寂寞
死守一世寂寞 2021-02-03 13:42

I have a data table which iterates through a custom object and generates checkboxes. On the second page, I want to determine which of these checkboxes have been selected.

<
2条回答
  •  一个人的身影
    2021-02-03 14:26

    Ok, if you want to handle it with Javascript, use Pavel's method, otherwise use the following to do it via the controller. You must create a wrapper class for whatever you wish to track. I'm not sure how it works, but somehow if you name a boolean variable "selected" in your wrapper class, it is mapped to the checkbox. Below is the code:

    So in your Visual force page, do:

    
        
             
        
     
     
    

    In your Controller, do the following: 1) Make a Wrapper class with the boolean "selected", which somehow maps to the inputCheckbox selected:

    public class wFoo {
        public Foo__c foo {get; set}
        public boolean selected {get; set;}
    
        public wFoo(Foo__c foo) {
            this.foo = foo;
            selected = false; //If you want all checkboxes initially selected, set this to true
        }
    }
    

    2) declare the list variables

    public List foos {get; set;}
    public List selectedFoos {get; set;}
    

    3) Define the accessor for the List

    public List getFoos() {
        if (foos == null) {
            foos = new List();
            for (Foo__c foo : [select id, name from Foo__c]) {
                foos.add(new wFoo(foo));
            }
        }
        return foos;
    }
    

    4) Define the method to process the selected checkboxes and place them in a List for use on another page

    public void processSelectedFoos() {
        selectedFoos = new List();
        for (wFoo foo : getFoos) {
            if (foo.selected = true) {
                selectedFoos.add(foo.foo); // This adds the wrapper wFoo's real Foo__c
            }
        }
    }
    

    5) Define the method to return the PageReference to the next page when the submit button is clicked

    public PageReference getPage2() {
        processSelectedFoos();
        return Page.Page2;
    }
    

提交回复
热议问题