【问题标题】:Maintaining CheckBox status in apex controller在顶点控制器中维护 CheckBox 状态
【发布时间】:2013-02-23 13:54:23
【问题描述】:

我正在编写一个visualforce页面,其中用户必须选择机会列表当用户单击上一个按钮时我实现了分页然后它将再次呈现列表面板并且我在调用我的visualforce页面段时更改了列表的值

<apex:pageBlockTable value="{!numbers}" var="n" align="center">
    <apex:column >
        <apex:inputCheckbox value="{!n.checked}"/>
    </apex:column> 
    <apex:column value="{!n.cat.Id}" />
    <apex:column value="{!n.cat.Name}" />
    <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
</apex:pageBlockTable>

我有一个 OpportunityWrapper 类来维护 checkItem:

public class OpportunityWrapper {
    public Boolean checked { get; set; }
    public Opportunity cat { get; set; }

    public OpportunityWrapper(){
        cat = new Opportunity();
        checked = false;
    }

    public OpportunityWrapper(Opportunity c){
        cat = c;
        checked = false;
    }

    public OpportunityWrapper(Opportunity c, Boolean checked){
        cat = c;
        this.checked = checked ;
    }
}

在自定义控制器中获取OpportunityWrapper列表的代码段是

public List<OpportunityWrapper> getNumbers() {
    opp = new List<OpportunityWrapper>();
    if ( selectedPage != '0' )
        counter = list_size*integer.valueOf(selectedPage)-list_size;

    //we have to catch query exceptions in case the list is greater than 2000 rows
    try {
        for ( Opportunity o : [SELECT Id,Name from Opportunity order by name 
               limit :list_size offset :counter] ) {
            if ( !oppId.contains(o.Id) ) 
                opp.add(new OpportunityWrapper(o));
            else
                opp.add(new OpportunityWrapper(o,true));               
        } 
    } catch ( QueryException e ) {
        ApexPages.addMessages(e);                   
        return null;
    }    
    return opp;
}

当按下前一个按钮时,会调用下面的方法

public PageReference Previous() {
    //user clicked previous button
    for ( OpportunityWrapper o : opp ) { 
        if ( o.checked &&  oppId.contains(o.cat.Id) )
            oppId.add(o.cat.Id); 
    }
    selectedPage = '0';
    counter -= list_size;
    return null ;
}

以下是自定义类的公共成员

private integer counter = 0;  //keeps track of the offset
private integer list_size = 5;
public integer total_size; 
List<OpportunityWrapper> opp ;
public List<OpportunityWrapper> oppwrapper = new List<OpportunityWrapper>(); //list of Opportunity wrapper shown in the page
public  Set<String> oppId = new Set<String>(); //set for maintaining which Id's are checked

我的目标是当我检查任何机会包装器并转到下一个或上一个列表时,返回时应该检查该项目,但我得到的设置值始终为空,但我将选择值列表存储在上一个( ) 方法为什么它显示的 Set 总是空的??

【问题讨论】:

  • 你能找到解决这个问题的好办法吗?

标签: salesforce apex-code visualforce


【解决方案1】:

您可以在以下主题中找到两种解决方案(一种用 JS 实现,另一种用 apex 控制器实现):

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

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多