为什么不能在Salesforce Visualforce中遍历Wrapper类的列表?

问题描述

我试图遍历包装类内的记录列表,并在Visualforce页面上显示它们。自定义对象称为Campaign_Products__c,包装器类用于显示用户是否选择了要添加到“购物车”中的产品。

Apex控制器代码(去除了外部位):

public with sharing class CONTROLLER_Store {
   ...
    public List<productOption> cpList           { get; set; }
    public class productOption {
        public Campaign_Product__c product;
        public Boolean inCart;
        public Integer quantity;
    }
   ...
    public CONTROLLER_Store(){
    ...
        List<Campaign> cmpList = getCampaignWithProducts(CampaignId,''); 
        // method above calls a campaign with a related list of Campaign Product records
        if(cmpList.size() > 0){
            cmp         = cmpList[0];
            cpList      = new List<productOption>();
            for(Campaign_Product__c pro : cmp.Campaign_Products__r){
                productOption option = new productOption();
                option.product  = pro;
                option.inCart   = false;
                option.quantity = 0;
                cpList.add(option);
            }
        } else {
            cmp         = new Campaign();
            CampaignId  = null;
            cpList      = new List<productOption>();
        }
    ....
    }

Visualforce页面(去除了多余的位)

<apex:page controller="CONTROLLER_Store" >
    <apex:repeat value="{! cpList }" var="option">
        {!option.product.Product__r.Name}    
        <apex:inputCheckbox value="{! option.inCart }"/>
    </apex:repeat>
</apex:page>

尝试保存visualforce页面时出现此错误:

Unknown property 'CONTROLLER_Store.productOption.product'

解决方法

您还需要使包装器中的属性对VF可见。像

public class productOption {
    public Campaign_Product__c product {get; private set};
    public Boolean inCart {get; set};
    public Integer quantity {get; set};
}

(假设产品在VF中应为只读)。您需要这些访问修饰符或完整的getter / setter方法。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...