如何找出在 VisualForce 的下一页上选择了哪些复选框? [英] How to find out which checkboxes have been selected on the next page in VisualForce?
问题描述
我有一个数据表,它遍历自定义对象并生成复选框.在第二页上,我想确定哪些复选框已被选中.
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.
在 VisualForce 页面中:
In the VisualForce page:
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>
在控制器中:
public String age {get; set; }
public List<Area_Of_Interest__c> getAreas() {
areas = [select id, name from Area_Of_Interest__c];
return areas;
}
在我的第二页上,我可以使用 {!age}
检索用户放入文本框age"中的值.如何检索已选中的复选框?
On my second page, I can retrieve the value that the user put in the textbox "age" by using {!age}
. How Do I retrieve which checkboxes have been checked?
谢谢.
推荐答案
好吧,如果你想用 Javascript 来处理它,使用 Pavel 的方法,否则使用以下通过控制器来完成.您必须为要跟踪的任何内容创建一个包装类.我不确定它是如何工作的,但不知何故,如果您在包装类中将布尔变量命名为selected",它会映射到复选框.代码如下:
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:
因此,在您的 Visual force 页面中,执行以下操作:
So in your Visual force page, do:
<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"/>
在您的控制器中,执行以下操作:1) 使用布尔值selected"创建一个 Wrapper 类,它以某种方式映射到所选的 inputCheckbox:
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) 声明列表变量
public List<wFoo> foos {get; set;}
public List<Foo__c> selectedFoos {get; set;}
3) 定义列表的访问器
3) Define the accessor for the 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;
}
4) 定义处理选中复选框的方法,并将它们放入一个列表中以供其他页面使用
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<Foo__c>();
for (wFoo foo : getFoos) {
if (foo.selected = true) {
selectedFoos.add(foo.foo); // This adds the wrapper wFoo's real Foo__c
}
}
}
5) 定义点击提交按钮时返回PageReference到下一页的方法
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;
}
这篇关于如何找出在 VisualForce 的下一页上选择了哪些复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!