SFDC_05(内部类)

本例主要实现的是前台页面上有复选框,选复选框后把所对应的三条数据留下。

点击清除复选,就取消复选。

 

<apex:page controller="CL_01">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!cList}" var="c" columns="3" >
                <apex:column value="{!c.ac.Id}"/>
                <apex:column value="{!c.ac.Name}"/>
                
                <apex:column headervalue="复选框">
                    <apex:inputCheckbox id="checkbox" value="{!c.check}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageblockButtons >
                <apex:commandButton action="{!clear}" value="清除复选"/>    
                <apex:commandButton action="{!save}" value="保存"/>
            </apex:pageblockButtons>
        </apex:pageBlock>    
    </apex:form>
</apex:page>
public with sharing class CL_01 {
    
    public list<Account> aList{set;get;}
    public list<CL_01DTO> cList{set;get;}
    
    public CL_01(){
        aList = [Select Name, Id From Account];
        cList = new list<CL_01DTO>();
     
        for(Account a : aList){
            CL_01DTO c = new CL_01DTO();
            c.check = false;
            c.ac = a;
            cList.add(c);
          }
    }
    public void save() {
        list<CL_01DTO> cList2 = new list<CL_01DTO>();
        for(CL_01DTO c : cList){
            if(c.check == true){
                cList2.add(c);
            }
        }
        cList.clear();
        cList = cList2;
    }
    public void clear() {
        for(CL_01DTO c : cList){
             c.check = false;       
        }
    }
   //定义一个内部类让它有两个属性,一个是check为复选框,一个是Account属性。这样就把复选框和前面的一条数据关联上
   //关键在于前端页面上checkbox 必须加一个属性value 这样才能前后关联上
    public class CL_01DTO{
        public Boolean check{set;get;}
        public Account ac{set;get;}
    }
}

如果有更好的方法非常感谢。

posted @ 2016-06-14 15:45  诸葛四郎  阅读(203)  评论(0编辑  收藏  举报