如何找出在VisualForce的下一页上选择了哪些复选框?

Mat*_*sen 7 salesforce force.com visualforce

我有一个数据表,它遍历自定义对象并生成复选框.在第二页上,我想确定选择了哪些复选框.

在VisualForce页面中:

 Age <apex:inputText value="{!age}" id="age" />
 <apex:dataTable value="{!Areas}" var="a">
      <apex:column >
      <apex:inputCheckbox value="{!a.name}" /> <apex:outputText value="{!a.name}" />
      </apex:column>
  </apex:dataTable>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

 public String age {get; set; }
  public List<Area_Of_Interest__c> getAreas() {
      areas = [select id, name from Area_Of_Interest__c];
      return areas;
  }
Run Code Online (Sandbox Code Playgroud)

在我的第二页上,我可以通过使用检索用户在文本框"age"中放置的值{!age}.如何检索已选中的复选框?

谢谢.

Mat*_*sen 4

好的,如果你想用 Javascript 来处理它,请使用 Pavel 的方法,否则使用以下通过控制器来完成。您必须为您想要跟踪的任何内容创建一个包装类。我不确定它是如何工作的,但不知何故,如果您在包装类中命名一个布尔变量“selected”,它就会映射到复选框。下面是代码:

因此,在您的 Visual Force 页面中,执行以下操作:

<apex:dataTable value="{!Foos}" var="f">
    <apex:column >
        <apex:outputLabel value="{!f.foo.name}" /> <apex:inputCheckbox value="{!f.selected}" />
    </apex:column>
 </apex:dataTable>
 <apex:commandButton action="{!getPage2}" value="go"/>
Run Code Online (Sandbox Code Playgroud)

在您的控制器中,执行以下操作: 1) 使用布尔值“selected”创建一个包装器类,该类以某种方式映射到选定的 inputCheckbox:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

2)声明列表变量

public List<wFoo> foos {get; set;}
public List<Foo__c> selectedFoos {get; set;}
Run Code Online (Sandbox Code Playgroud)

3) 定义List的访问器

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

4) 定义处理选定复选框的方法,并将它们放入列表中以供在另一个页面上使用

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

5) 定义点击提交按钮时返回下一页PageReference的方法

public PageReference getPage2() {
    processSelectedFoos();
    return Page.Page2;
}
Run Code Online (Sandbox Code Playgroud)