Skip to main content
Hello there,

I am creating 2 pablock section . which display data with checkbox's and select all check box for each section .

when I am selecting top section select all chekbox it is also selecting below check checkbox data and If  i am using mutilple object getting too many quiry error also.

below is the class and vf page. can any one please solve this issue. an

public class Checkbox_Class

{

 

    List<accountwrapper> accountList = new List<accountwrapper>();

    List<accountwrapper_Two> accountList2 = new List<accountwrapper_Two>();

    List<Account> selectedAccounts = new List<Account>();

    public List<accountwrapper> getAccounts()

    {

        for(Account a : [select Id, Name, AccountNumber, Phone from Account ])

        accountList.add(new accountwrapper(a));

        return accountList;

    }

     public List<accountwrapper_Two> getAccounts2()

    {

        for(Account a : [select Id, Name, AccountNumber, Phone from Account ])

        accountList2.add(new accountwrapper_Two(a));

        return accountList2;

    }

    public PageReference getSelected()

    {

        selectedAccounts.clear();

        for(accountwrapper accwrapper : accountList)

        if(accwrapper.selected == true)

        selectedAccounts.add(accwrapper.acc);

        return null;

    }

      public PageReference getSelected2()

    {

        selectedAccounts.clear();

        for(accountwrapper_Two accwrapper2 : accountList2)

        if(accwrapper2.selected == true)

        selectedAccounts.add(accwrapper2.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;

        }

    }

     public class accountwrapper_Two

    {

        public Account acc{get; set;}

        public Boolean selected {get; set;}

        public accountwrapper_Two(Account a)

        {

            acc = a;

            selected = false;

        }

    }

}

vf page

<!-- http://tablesorter.com/docs/-->

<apex:page controller="Checkbox_Class" Tabstyle="Account">

    

    <apex:includeScript value="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" />

    <apex:includeScript value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.17.8/js/jquery.tablesorter.min.js" />

    <apex:stylesheet value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.17.8/css/theme.blue.css" />

    

    <apex:form >

     

        <apex:pageBlock Title="Accounts with CheckBoxes">

            <apex:pageBlockSection Title="List of Available Accounts">

                <apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1" id="accsTable" styleclass="tablesorter">

                    <apex:column >

                        <apex:facet name="header"> <apex:inputCheckbox >

                            <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" reRender="Selected_PBS"/>

                            </apex:inputCheckbox>

                        </apex:facet>

                        <apex:inputCheckbox value="{!a.selected}" id="checkedone">

                        <apex:actionSupport event="onclick" action="{!GetSelected}" 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:dataTable>

            </apex:pageBlockSection>

            

            

            <apex:pageBlockSection Title="List of Available Accounts">

                <apex:dataTable value="{!accounts2}" var="a" columnswidth="50px,50px" cellpadding="4" border="1" id="accsTable" styleclass="tablesorter">

                    <apex:column >

                        <apex:facet name="header"> <apex:inputCheckbox >

                            <apex:actionSupport event="onclick" action="{!GetSelected2}" onsubmit="checkAll2(this)" reRender="Selected_PBS"/>

                            </apex:inputCheckbox>

                        </apex:facet>

                        <apex:inputCheckbox value="{!a.selected}" id="checkedone2">

                        <apex:actionSupport event="onclick" action="{!GetSelected2}" 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: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;

            }

        }

         function checkAll2(cb)

        {

            var inputElem = document.getElementsByTagName("input");

            for(var i=0; i<inputElem.length; i++)

            {

                if(inputElem[i].id.indexOf("checkedone2")!=-1)

                inputElem[i].checked = cb.checked;

            }

        }

        

      $(document).ready(function()

      {

        $("[id$='accsTable']").tablesorter({theme: 'none', dateFormat : "dd/mm/yyyy"});

      });

    </script>

</apex:page>
1 answer
  1. Feb 2, 2018, 6:15 PM
    - There are many ways you resolve this issue but this will try to do quick fix

    public List<accountwrapper> getAccounts()

        {

            if(!accountList.IsEmpty()) return accountList;   // Don't do SOQL everytime

            for(Account a : [select Id, Name, AccountNumber, Phone from Account ])

            accountList.add(new accountwrapper(a));

            return accountList;

        }

         public List<accountwrapper_Two> getAccounts2()

        {

           if(!accountList2.IsEmpty()) return accountList2;   // Don't do SOQL everytime

         

            for(Account a : [select Id, Name, AccountNumber, Phone from Account ])

            accountList2.add(new accountwrapper_Two(a));

            return accountList2;

        }
0/9000