0

I have a search VF page where I show the list of records based on search. On select it will perform some action.

So the issue is when I select the checkbox it is returning False instead of true(checked in Debugs).

I am not able to find the cause for this. Can anyone please help me with this?

Here is part of my code :

public class searchpageclass {

public string searchstring{get; set;}
public List<cusobj__c> list123{get;set;}
String IdString;

List<cusobj__c> listCb; 
public String searchname{ get ; set;}


 public List<Wrapclass> cblist {get; set;}

    public PageReference PerformSearch() {

       if(searchname.length()==0){
          errorMessage = Label.Error;
         }



      listCb  =[select id,Name,Contact__c,Amount__c,Type__c from cusobj__c where type__c =: 'Code' and Name=:searchname];


       if(listCb.size()>1)
       {

       list123 = listCb;

       }

    public PageReference SelectedList() {

       if(searchname.length()==0){
          errorMessage = Label.Error1;
         }

    List<cusobj__c> selectedcb = new List<cusobj__c>();


    for(Wrapclass ac: getwrap()) {
    System.debug('Debug!!' +ac.select);// it is returning false when chekced on VF page
            if(ac.select == true) { 
                   selectedcb.add(ac.accidentifier);
                 }
        ac.select = false;
    }


    for(cusobj__c cbrec: selectedcb) {
                IdString = cbrec.id;
    }      

      listCb  =[select id,Name,Contact__c,Amount__c,Type__c from cusobj__c where Id=:IdString];

       if(listCb.size()>0)
       {
       //Will perform some action

          }
          else
          {
                return null;
   }

public List<Wrapclass> getwrap() {

       cblist.clear();

        for(cusobj__c p: list123) {
            cblist.add(new Wrapclass(p));

           }

    return cblist;
}

  public class Wrapclass {
    public cusobj__c cbrecs {get; set;}
    public Boolean select {get; set;}

    public Wrapclass (cusobj__c ai) {
        cbrecs = ai;
        select = false;
    }
  }

}

Page :

<apex:commandButton action="{!PerformSearch}" value="Search"  />

       <apex:outputPanel rendered="{!list.size > 1}" >
       <apex:pageBlock>
                  <br/><br/>

            <apex:pageblockTable value="{!wrap}" var="w" >
             <apex:column value="{!w.cbrecs.Name}"/>
             <apex:column value="{!w.cbrecs.Amount__c}"/>
             <apex:Column headerValue="Contact" >
             {!w.cbrecs.Contact__r.name}
             </apex:column>
            <apex:inputCheckbox value="{!w.select}" /> 

             <apex:actionSupport event="onclick" 
                                action="{!SelectedList }" />


             </apex:column>

         </apex:pageblockTable>


      </apex:pageBlock>
      </apex:outputPanel>
  • 1
    So the statement here System.debug('Debug!!' +ac.selected); Is it a typo to use select instead of selected ? – Mohith Shrivastava Oct 13 '16 at 17:12
  • @MohithShrivastava my bad in real code i added select. I corrected my code here – Learner Oct 13 '16 at 17:19
  • @MohithShrivastava Also If I add select=true in wrapper class and If I uncheck that in Vf page its working. So the thing is its working in reverse order. – Learner Oct 13 '16 at 17:22
  • Can you change event from onclick to onchange and see how it goes – Mohith Shrivastava Oct 13 '16 at 17:54
  • Onchange is not working. I mean its not even calling the action – Learner Oct 13 '16 at 18:07
1

This line is the issue here

for(Wrapclass ac: getwrap()) {

Instead of using updated list you are filling the list with fresh set of data that why you are getting false in checkbox. So update this for loop

for(Wrapclass ac: wrap) {

Also instead of getter method you should make a list variable and use that and rerender the table. Because if you rerender the table on ui you will lose all your change you made it on table.

You can use cblist in your for loop and on PBT this will give you correct result.

| improve this answer | |
  • Thanks for your response. If I use wrap instead of getwrap,it is giving compile error "method doesn't exists" – Learner Oct 14 '16 at 14:32
  • 1
    @Learner I suggest you to use cblist in for loop and with PBT. – Tushar Sharma Oct 14 '16 at 16:13
1

Can you try, by changing the outputpanel code to

   <apex:outputPanel rendered="{!if(list.size > 1,true,false)}" >
| improve this answer | |
  • Thanks for your response. I tried your code but no change. Its again giving false when checked – Learner Oct 13 '16 at 19:01
  • You can always replace IF(condition, true, false) with simply condition... – Adrian Larson Oct 13 '16 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.