Tuesday 12 April 2016

Student Regestration with Wrapper

Scenario  When we click on the insert button  success fully inserted records should be displayed in another pagBlock below thelist


2. When you click on add button it should ad three more rows

3.When you click on the Reset button entire data in the pageBlocktable should be reset

public class StudentLine {
    public List<Student__c> listrec {set;get;}
    public List<Student__c> success {set;get;}
    public String selected {set;get;}
    public List<StudentWrap> students{set;get;}
    public StudentLine(){
        students=new List<StudentWrap>();
        add();
       
    }
    public void add(){
        for(Integer i=0;i<3;i++){
            StudentWrap sw=new StudentWrap();
            students.add(sw);
        }
    }
    public void createStudents(){
        listrec=new List<Student__c>();
         for(StudentWrap sw:students){
                if(sw.name!=null  || sw.name!=''){
                    Student__c s=new Student__C();
                    s.name__c=sw.name;
                    s.Course__c=sw.course;
                    s.fee__c=sw.fee;
                    listrec.add(s);
               }
         }
         List<Id> ids=new List<Id>();
      
         Database.saveResult[] res=Database.insert(listrec,false);
       
         for(Database.saveResult sr:res){
             if(sr.issuccess()){
                 ids.add(sr.getId());
             }
         }
        success=[select id,name__c,Course__c,Fee__c from Student__C where id in:ids];
    }
    public void reset(){
        students.clear();
        add();
    }
    public void feeDetails(){
       
    }
    public class StudentWrap{
        public String name {set;get;}
        public String course {set;get;}
        public Decimal fee{set;get;}
        public StudentWrap(){
            name=null;
            fee=0;
        }
    }
}

<apex:page controller="StudentLine">
    <apex:form >
        <apex:pageBlock title="Student Registration" id="one">
            <apex:pageBlockTable value="{!students}" var="a">
                <apex:column headerValue="Student Name">
                    <apex:inputText value="{!a.Name}" />
                </apex:column>
                 <apex:column headerValue="Course">
                   <apex:selectList value="{!a.Course}" size="1">
                       <apex:selectOption itemLabel="-None-" itemValue="none" />
                       <apex:selectOption itemLabel="Java" itemValue="Java" />
                       <apex:selectOption itemLabel="sfdc" itemValue="sfdc" />
                       <apex:selectOption itemLabel="oracle" itemValue="oracle" />
                   </apex:selectList>
                </apex:column>
                 <apex:column headerValue="Fee ">
                    <apex:inputText value="{!a.Fee}" />
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="insert" action="{!createStudents}" />
                <apex:commandButton value="Add" action="{!add}" />
                <apex:commandButton value="Reset" action="{!reset}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Success" rendered="{! !ISNULL(success)}">
            <apex:pageBlockTable value="{!success}" var="a">
                <apex:column value="{!a.Name__c}" />
                <apex:column value="{!a.Course__c}" />
                <apex:column value="{!a.Fee__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>

</apex:page>

No comments:

Post a Comment