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
HarryHHHarryHH 

How to select one Item of a pageblocktable

Hello,

 

I have a search function for Articles in order-positions realized in visualforce. I managed showing the search results and getting checkboxes for each result. But now I am struggling how to tell the controller which articles are selected. Her is the piece of the visualforce page and the getter and settermethods:

 

    <apex:pageBlockTable value="{!results}" var="l"
       rendered="{!NOT(ISNULL(results))}">
     <apex:column headerValue="Auswahl">
      <apex:selectCheckboxes value="{!selArt}">
       <apex:selectOptions value="{!selart}"/>
      </apex:selectcheckboxes><br/>
     </apex:column>
     <apex:column value="{!l.name}"/>
     <apex:column value="{!l.v_art_PZN__c}"/>
     <apex:column value="{!l.v_art_Hm_Positionsnr_1__c}"/>
    </apex:pageBlockTable>

 

And the contorller methods:

 

    public void setselArt (ID artID)
    {  
        system.debug ('selektierter Artikel ' + selArt );
        system.debug ('bekommener Artikel' + artID);
    }

    public Artikel__c getselArt()
    {
        system.debug ('selektierter Artikel ' + selArt );
        return selArt ;
    }

 

What can I do to get the selected article and send it to the calling page for orderpositions and setting it in the lookup-field article?

 

Thanks for your help!

 

Harry

Best Answer chosen by Admin (Salesforce Developers) 
ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Harry,

     I do not think it is necessary to include the '<apex:selectOptions value="{!selart}"/>' in your Visual Force page. The boolean value of the checkbox itself will pass on the result whether the row is selceted or not.

I don not know how you have used the checkbox with the search result. But if you have defined an inner class comprising of the SObject returned in your Search Result and the Boolean field that u have used for the checkbox, the above stated logic will work fine.

for example if your Result is searching for Accounts

 

public class innerClass

{

       public Account acc {get; set;}

       public Boolean isSelected {get; set;}

 

       public innerClass(Account acct, Boolean bool)

       {

           acc = acct;

           isSelected = bool;

       }

 

}

All Answers

bob_buzzardbob_buzzard

I don't think there's an easy way to determine which row has been selected.  In your example, each row in the table contains a checkbox backed by the same controller property, so selecting one selects all.

 

I'd suggest that you have the checkbox as a field on the sobject that is backing the table.  That way, once the page is submitted (presumably the user clicks a button to take some actions on the selections), the elements in your results list will be updated with the checkbox values.    Thus in the controller, you would simply iterate the results list and extract those with the checkbox field set to true.

 

I've also done something similar with a button on each row, which the user clicks to select the row.  However, this does mean that you can't select multiple rows in a single action.

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Harry,

     I do not think it is necessary to include the '<apex:selectOptions value="{!selart}"/>' in your Visual Force page. The boolean value of the checkbox itself will pass on the result whether the row is selceted or not.

I don not know how you have used the checkbox with the search result. But if you have defined an inner class comprising of the SObject returned in your Search Result and the Boolean field that u have used for the checkbox, the above stated logic will work fine.

for example if your Result is searching for Accounts

 

public class innerClass

{

       public Account acc {get; set;}

       public Boolean isSelected {get; set;}

 

       public innerClass(Account acct, Boolean bool)

       {

           acc = acct;

           isSelected = bool;

       }

 

}

This was selected as the best answer