-2

I can update available to sold and sold to available on any button click but problem is whole column changed .. i want when i click on change status only that row status should change. rest of rows status remain same.

output is shown in this image

Apex Controller

public class BookController 
{
    public list books {get;set;} 
    public ID CurrentStatusID{get;set;}
    public BookController()
    {
        books=[select id, name, status__c from book__c];   
   }
    public void UpdateStatus()
    {   
        try
        { 
            for(book__c b : books)
            {
                if(b.Status__c=='Available')
                {
                    b.Status__c='Sold';                
                }
                else if(b.Status__c=='sold')
                {
                    b.Status__c='Available';              
                }
            }           
            update books; 
        }
        catch(Exception ex)
        {
            system.debug('Error: ' + ex);
        }
    }
}

 <apex:page Controller="BookController">
<apex:form >
    <apex:pageBlock id="pb">
        <apex:pageBlockTable value="{!books}" var="b">
            <apex:column headerValue="Name" value="{!b.name}"/>
            <apex:column headerValue="Status" value="{!b.Status__c}"/>
            <apex:column>
                <apex:commandButton value="Change Status" action="{!UpdateStatus}" Rerender="pb">
                    <apex:param assignTo="{!CurrentStatusID}" id="p1" value="{!b.ID}"/>
                 </apex:commandbutton>
             </apex:column>
        </apex:pageBlockTable>  
     </apex:pageBlock>
</apex:form>    
  </apex:page>   

  • 2
    Please correct your title. The UpdateStatus method loops over every book, so it updates each one of them. I downvoted your question as it is a bit to trivial. – Bart Juriewicz Feb 24 '15 at 13:32
0
for(book__c b : books)
{
    if(CurrentStatusID == b.Id){
           if(b.Status__c=='Available')
                {
                    b.Status__c='Sold';                
                }
                else if(b.Status__c=='sold')
                {
                    b.Status__c='Available';              
               }
            }
            }           


  update books; 

Here you will get the only one record and update that, no need to updated all books records it may helpful for you

0

You have to use a wrapper class, where u need to bind each each record with change status button.

Assign a boolean value to the button and onclick change the value of that button and call the update method only on the record with true boolean value, at the end of update methode change the boolean again to false.

Search more about wrapper methods for better understanding

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.