+ Start a Discussion
yvk431yvk431 

Issue with selectCheckbox

Hi Friends ,

 

I am working on customising the Opportunity line Items functionality,

intially i created a products search page and selecting the products and after clicking on a button (Select) I need to display the selects products similar to the multi line layout .

 

For selecting multple products i have used selectCheckbox in a pagablock table . 

 

 

 <apex:pageBlockTable id="SearchResult" value="{!prodRes}" var="PR"  width="100%" rendered="{!pids.size > 0}">
                    <apex:column headerValue="Select All">
                        <apex:facet name="header">
                              <apex:inputCheckbox id="chkSelectAll" onclick="checkAll(this),checkSelect(this);" title="Select All" />
                         </apex:facet>
                                               
                          <apex:selectCheckboxes value="{!Selected}" id="ids" onclick="checkSelect(this);">
                          <apex:selectOption itemvalue="{!PR.ID}"  />
                          </apex:selectCheckBoxes>
                    </apex:column>  
                    

                    <apex:column rendered="false" >
                          <apex:facet name="header">Product Code   </apex:facet>
                          <apex:outputText value="{!IF((PR.Product2.ProductCode<>''),PR.ProductCode,' - ')}" />
                    </apex:column>

To select all the checkboxes I am using the following script

 

 

function checkAll(cb)
{
//alert(cb.checked);
var inputElem = document.getElementsByTagName("input");
//alert(inputElem.length);
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("ids")!=-1)
inputElem[i].checked = cb.checked;
}
//alert(inputElem[0].checked);
}   

 

 

 

In Order to retrieve the selected products I am using array property variable 'Selected' .

 

 

 public id[] Selected = new  id[]{};
    public id[] getSelected()
    {
       return Selected;
    }
    public void setSelected(id[] slctd)
    {
       this.Selected.addAll(slctd);
    }

 

 

This seems working fine until yesterday the QA team reported with lots of bugs around it, the checkbox selections are not passed onto the variable i used each and every time.  I verified the browser settings enabled the browser script errors. Every thing seems to be fine but the selections kept on  missing and they wont get populated when i hit the Select button and a blank screen with 0 records prevails:

 

Please help me out, is this any issue with selectcheckbox  or my approach is wrong ?

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey,

 

There a few ways that bugs might be introduced in the approach above but the big one is that order in an array is not guaranteed. So even if you select the 4th checkbox SOMETIMES it might not be the 4th element in the array. The typical approach is to use a class to wrap your data records. The neat trick here is that you can also include a field in the "wrapper class" that tells you whether that record is selected. There's a nicely worked example here:

 

http://wiki.developerforce.com/index.php/Wrapper_Class

 

Wes

All Answers

WesNolte__cWesNolte__c

Hey,

 

There a few ways that bugs might be introduced in the approach above but the big one is that order in an array is not guaranteed. So even if you select the 4th checkbox SOMETIMES it might not be the 4th element in the array. The typical approach is to use a class to wrap your data records. The neat trick here is that you can also include a field in the "wrapper class" that tells you whether that record is selected. There's a nicely worked example here:

 

http://wiki.developerforce.com/index.php/Wrapper_Class

 

Wes

This was selected as the best answer
yvk431yvk431

Thank you weznolte,

 

I knew about this approach but i got lazy and thought it would take more time than the existing approach.

You really saved my time.

 

 

yvk431yvk431

Hi,

 

As suggested by you  I have modified my code to use the wrapper class. But still the issue prevails infact i found out that the issue is happening only for Browser settings of IE 8 , when we enable the script debugging.

 

the old code & New codes are working fine with mozilla and compatible view of IE8,

 

Let me know how to proceed with this.