function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

Issues with select All checkbox and header data binding

 I'm am trying to create a simple selectAll check box at the top of my table but I can not figure out why this is not working. It's as if the data binding is not occurring when the check box is located in the header section of the table. If I remove the check box from the table it works fine.
 
This uses standard fields so it should be easy to copy and paste to reproduce.

Code:
Page:

<apex:page>
<c:resellerAccountSearch />
</apex:page>

Component:

<apex:component controller="resellerAccountSearch" >
<apex:form >
<apex:pageblock >

<apex:inputText value="{!searchValue}" id="searchtext"/>
<apex:commandButton value="Search" action="{!search}" rerender="results" status="search"/>
<br/>

<!-- This check box works fine but if it is in the header section below it doesn't work
<apex:inputCheckBox value="{!selectAllCheckbox}">
<apex:actionSupport event="onclick" action="{!selectAll}" rerender="results"/>
</apex:inputCheckBox> -->

<apex:actionStatus id="search">

<apex:facet name="start">
<strong>Retrieving data... </strong>
</apex:facet>

<apex:facet name="stop">

<apex:pageblockTable value="{!cAccounts}" var="c" id="results">
<apex:column >
<apex:facet name="header">
<apex:inputCheckBox value="{!selectAllCheckbox}">
<apex:actionSupport event="onclick" action="{!selectAll}" rerender="results"/>
</apex:inputCheckBox>
</apex:facet>
<apex:inputCheckBox value="{!c.selected}"/>
</apex:column>
<apex:column value="{!c.acct.Name}"/>
<apex:column value="{!c.acct.BillingCity}"/>
<apex:column value="{!c.acct.CreatedDate}"/>
<apex:column value="{!c.acct.LastModifiedDate}"/>
<apex:column value="{!c.acct.Id}"/>
</apex:pageblockTable>

</apex:facet>
</apex:actionStatus>
</apex:pageblock>
</apex:form>
</apex:component>

Controller:

public class resellerAccountSearch {

public String searchValue {get; set;}
public List<Account> results {get; set;}
public List<cAccount> cAccounts {get; set;}
public Boolean selectAllCheckbox {get; set;}

//Constructor
public resellerAccountSearch(){
results = new List<Account>();
cAccounts = new List<cAccount>();
}

public PageReference search(){
results.clear();
cAccounts.clear();

if(searchValue.length() > 1){
searchValue = '*'+searchValue+'*';

List<List<SObject>> searchList = [FIND :searchValue IN NAME FIELDS RETURNING Account(Id, Name, BillingCity, CreatedDate, LastModifiedDate order by LastModifiedDate desc )];
results = ((List<Account>)searchList[0]);

for(Account acct: results){
cAccounts.add(new cAccount(acct));
}
}
return null;
}

public PageReference selectAll(){
system.debug(selectAllCheckbox);
for(cAccount acct : cAccounts){
system.debug(acct.selected);

if(selectAllCheckbox == true){
acct.selected = true;
}else{
acct.selected = false;
}
}
return null;
}

public class cAccount{
public Account acct {get; set;}
public Boolean selected {get; set;}

public cAccount(Account a){
this.acct = a;
selected = false;
}
}

}


Thanks,
Jason

Message Edited by TehNrd on 01-22-2009 03:21 PM
CarcajouCarcajou
Hi Jason,

I just did the same yesterday and in fact i had to introduce a small javascript function on the "onclick" event of the "select all check box".
The first thing i did is to add an Id to the InputCheckBox :

Code:
<apex:inputCheckBox value="{!c.selected}" Id="selectLine"/>

Then i created this small javascript function :
Code:
function checkAll(cb){
    var inputElem = document.getElementsByTagName("input");
    for(var i=0; i<inputElem.length; i++){
        if(inputElem[i].id.indexOf("selectLine")!=-1){
            inputElem[i].checked = cb.checked;
        }
    }
}

 Finally i plugged this function to the onclick event of the select all check box :
Code:
<apex:inputCheckBox value="{!selectAllCheckbox}" onclick="checkAll(this)"> 

It works quite good.
Hope this can help you.
Catherin.


 

TehNrdTehNrd
Thanks for confirming this behavior. This appears to be a bug so I have opened a case (02308648).

I try to stay away from Javascript and use apex as much as possible but thanks for the workaround. I must admit it is quicker though.


Message Edited by TehNrd on 12-16-2008 02:46 PM
TehNrdTehNrd
Confirmed as bug. Work around above is the recommended solution.