Tuesday 8 January 2013

Select/Deselect all Check boxces:- (08/01/12)


Hi guys we have a scenario, When we select/deselect the header check box the other all
 check boxes Should be select/deselect. And display the selected check boxes .


<apex:page controller="test7">
    <apex:form >
        <apex:pageBlock Title="Accounts with CheckBoxes">
            <apex:pageBlockSection Title="List of Available Accounts">
                <apex:pageBlockTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column >
                        <apex:facet name="header"> <apex:inputCheckbox >
                            <apex:actionSupport event="onclick" action="{!GetSelected1}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
                            </apex:inputCheckbox>
                        </apex:facet>
                        <apex:inputCheckbox value="{!a.selected}" id="checkedone">
                        <apex:actionSupport event="onclick" action="{!GetSelected1}" rerender="Selected_PBS"/></apex:inputCheckbox>
                    </apex:column>
                    <apex:column headervalue="Account Name" value="{!a.acc.Name}" />
                    <apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />
                    <apex:column headervalue="Phone" value="{!a.acc.Phone}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>

            <apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS">
                <apex:dataTable value="{!SelectedAccounts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column headervalue="Account Name" value="{!s.Name}" />
                    <apex:column headervalue="Account Number" value="{!s.AccountNumber}" />
                    <apex:column headervalue="Phone" value="{!s.Phone}" />
                </apex:dataTable>
            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>
    <script>
        function checkAll(cb)
        {
            var inputElem = document.getElementsByTagName("input");
            for(var i=0; i<inputElem.length; i++)
            {
                if(inputElem[i].id.indexOf("checkedone")!=-1)
                inputElem[i].checked = cb.checked;
            }
        }
    </script>
</apex:page>



public class test7 {
 
    List<accountwrapper> accountList = new List<accountwrapper>();
    List<Account> selectedAccounts = new List<Account>();

    public List<accountwrapper> getAccounts()
    {
        for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 5])
        accountList.add(new accountwrapper(a));
        return accountList;
    }

    public PageReference getSelected1()
    {
        selectedAccounts.clear();
        for(accountwrapper accwrapper : accountList)
        if(accwrapper.selected == true)
        selectedAccounts.add(accwrapper.acc);
        return null;
    }

    public List<Account> GetSelectedAccounts()
    {
        if(selectedAccounts.size()>0)
        return selectedAccounts;
        else
        return null;
    }   

    public class accountwrapper
    {
        public Account acc{get; set;}
        public Boolean selected {get; set;}
        public accountwrapper(Account a)
        {
            acc = a;
            selected = false;
        }
    }
}



-----------------

Here we have a scenario, Search the records by giving it's Name.


<apex:page standardController="Account" Extensions="secondinterview" standardStylesheets="true">
 
  <apex:form >
    <apex:pageBlock id="id" title="Interview Two">
   
        <apex:pageBlockSection title="Search">
            Search:<apex:inputtext value="{!str1 }"/>
           &nbsp; <apex:commandButton action="{!Search}" value="search" reRender="id"/>
        </apex:pageBlockSection>
       
        <apex:pageBlockButtons >
            
        </apex:pageBlockButtons>
    
     <apex:pageBlockSection title="Result" columns="1">
    <apex:pageBlockTable value="{!Search}" var="Test">            
                   <apex:column headerValue="Account Name">
                       <apex:outputField Value="{!Test.Name}"/>
                   </apex:column>
                   <apex:column headerValue="Billing State/Province">
                         <apex:outputField value="{!Test.BillingState}"/>
                   </apex:column>
                    <apex:column headerValue="Phone">
                           <apex:outputField value="{!Test.Phone}"/>
                    </apex:column>
                    <apex:column headerValue="Website">
                        <apex:outputField value="{!Test.Website}"/>
                    </apex:column>
    </apex:pageBlockTable>
   
    </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>


public class secondinterview {

public String str1 {get;set;}
String str2;
public Account Account{get;set;}
    public secondinterview(ApexPages.StandardController controller) {
        Account=new Account();
    }
public void search1(String s1)
    {
    str1=s1;
   
    }
    Public String search1()
    {
    return str1;
    }
    List<Account> conobj;
     public void Search()
     {
         System.Debug('********************'+Account.Name);
        conobj=[select id,name,BillingState,Phone,Website from Account where Name=:str1];

     }
  
  Public List<Account> getSearch()
  {
    return conobj;
 
  }
}

No comments:

Post a Comment