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

时间:2013-02-11 23:48:12

标签: salesforce visualforce force.com

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

在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>

在控制器中:

 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”中放置的值。如何检索已选中的复选框?

谢谢。

2 个答案:

答案 0 :(得分: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"/>

在您的控制器中,执行以下操作: 1)使用布尔“selected”创建一个Wrapper类,它以某种方式映射到所选的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
    }
}

2)声明列表变量

public List<wFoo> foos {get; set;}
public List<Foo__c> selectedFoos {get; set;}

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;
}

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
        }
    }
}

5)定义在单击提交按钮时将PageReference返回到下一页的方法

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

答案 1 :(得分:3)

在我目前的项目中,我遇到了同样的问题。我使用了apex:pageBlockTable,但我猜你可以使用我的代码(我在代码中对对象名称进行了一些更改)

<apex:pageBlockTable value="{!Areas}" var="areas">
    <apex:column width="25px">
        <apex:facet name="header">
            <input type="checkbox" onClick="selectAll();" />
        </apex:facet>
        <input type="checkbox" id="{!areas.Id}" onClick="saveSelection();" />
    </apex:column>
... some additional columns
</apex:pageBlockTable>

我已将自定义对象ID放在html中的输入ID中,它看起来像

<input id="003R000000lCIq6IAG" onclick="saveSelection();" type="checkbox">
<input id="003R000000lCIoJIAW" onclick="saveSelection();" type="checkbox">

saveSelection()函数已写在javascript

<script>
var areaIds = [];

function saveSelection() {
    var selectedIds = areaIds.join('');
    $j(':checkbox').each(function(){
        if (this.checked) {
            if (selectedIds.indexOf(this.id) === -1) {
                areaIds.push(this.id);
                selectedIds = selectedIds + this.id;
            }
        } else {
            if (selectedIds.indexOf(this.id) !== -1) {
                for (i=0; i < areaIds.length; i++) {
                    if (areaIds[i] === this.id) {
                        areaIds.splice(i, 1);
                        selectedIds = areaIds.join('');
                    }
                }
            }
        }                      
    });
}

并且用于恢复使用了以下代码

function restoreSelection() {
    contIds = areaIds.join('');
    i = 0;
    $j(':checkbox').each(function(){ if(this.id !== ''){  if(contIds.indexOf(this.id) !== -1){this.checked=true;};}});

}

我在这里使用jQuery,这意味着您还应该在页面中包含以下代码

<apex:includeScript id="JQuery" value="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"/>
... some code
<script>
    window.$j = jQuery.noConflict();
    ... some code
</script>

js也来自分页按钮:

<apex:panelGrid columns="7">
   <apex:commandButton status="fetchStatus" reRender="results" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page" onClick="saveSelection();" oncomplete="   restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page" onClick="saveSelection();"    oncomplete="restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page" onClick="saveSelection();" oncomplete="   restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page" onClick="saveSelection();" oncomplete="   restoreSelection()" />
   <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>
   <apex:outputPanel style="color:#4AA02C;font-weight:bold">
       <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
   </apex:outputPanel> 
</apex:panelGrid>

我希望这对你有所帮助。