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
Eager-2-LearnEager-2-Learn 

Updating a row in pageblocktable

Hi,

I have a subclass wrapper that I use to populate a pageblocktable on the VF page.  When I take action on the row I am able to pass the row id to the controller and do some work that I need to do. 

The problem is I know want to update the row on the table with data.  I think I need to get the wrapper class row and update it and my action refreshes the pageblocktable so I think it should refresh and show the change.  The problem is I am having trouble figuring out how to use my wrapper class list and get to the specific related row so I can update the field level data in the wrapper class record.  

Since it is a list I am not certain I can do this.  Any idea on this would be helpful.
Best Answer chosen by Eager-2-Learn
Boris BachovskiBoris Bachovski

As far as I understood you want to be able to find the row that you clicked on and change fields on the car record? If that's the case then your controller methods should look something like this:

public void StartCar()
{
for (wprCar car : wprCarList)
{
  if (car.CarId == currentId)
  {
   car.car.Name = 'New Name for started car';
   car.car.Color__c = 'New Color for started car';
   // this is how you reference the actual car record that you're listing on the table
   break;
  }
}
}

public void StopCar()
{
// Same thing for Stop Car
for (wprCar car : wprCarList)
{
  if (car.CarId == currentId)
  {
   car.car.Name = 'New Name for stopped car';
   car.car.Color__c = 'New Color for stopped car';
   // this is how you reference the actual car record that you're listing on the table
   break;
  }
}
}

All Answers

Boris BachovskiBoris Bachovski
If you can identify the record from your wrapper class in your controller and change the data, by re-rendering the pageblocktable, the updates should flow through automatically. Not sure if we're talking about the same issue, though some snippets from your actual code would be very helpful here.

Also, if the list is an issue and you can't identify the record, you can also use a map and apex repeat to build the table.
Eager-2-LearnEager-2-Learn

So below is the mark up.  It uses the standard Contact object and a custom object called Car__c.  

My wrapper class encapsulates the car__c object and expans on that with a few objects like status and two booleans for holding the button's value when clicked.  

 

So when one of the buttons are clicked I call a method and I am passing the method to the table row id of thet car__c id.  I can't seem to figure out how I can get the other details for that matching id in the wprCar list and update the fields.  

Is there a way I can pass the entire table row data?  If I could do that then I think what I have access to in my method that gets called on the button click would allow me to update the record.





<apex:page title="Cars" tabStyle="Contact" standardController="Car__c" Extensions="car_Controller" recordSetVar="car">
    <apex:sectionHeader title="Car Picker" subtitle="{!Contact.Name}"/>
    <apex:form >
    <apex:pageBlock >
        <apex:pageMessages /> 
.....
.....
.....
        <apex:pageblockSection title="Cars" collapsible="false" Columns="1" id="pbs3">
            <apex:pageblockTable value="{!wprCarList}" var="CarWpr" id="pbt1">
                <apex:column headerValue="Action" width="150">
                    <apex:commandButton action="{!StartCar}" value="Start" disabled="{!carWpr.Started}" reRender="pbs1,pbs3">                   
                        <apex:param assignTo="{!currentId}" name="carId" value="{!CarWpr.carId}"/>
                    </apex:commandButton>
                    <apex:commandButton action="{!StopCar}" value="Stop" disabled="{!carWpr.Stopped}"  reRender="pbs1,pbs3">
                        <apex:param assignTo="{!currentId}" name="carId" value="{!carWpr.carId}"/>                  
                    </apex:commandButton>
                </apex:column>
                <apex:column value="{!carWpr.Status}" headerValue="Status" width="100"/>
                <apex:column value="{!carWpr.car.Name}" width="400"/>
                <apex:column value="{!carWpr.car.color__c}"/>
            </apex:pageblockTable>                                
        </apex:pageblockSection>
....
....
....




//CONTROLLER
public with sharing class car_Controller {
    public List<wprCar> wprCarList {get; set;}
....
....
....

    // Wrapper class for car table
    public class wprCar {
        public Car__c Car {get; set;}
        public Id CarId {get; set;}
        public Boolean started {get; set;}
        public Boolean stopped {get; set;}
        public String  color {get; set;}

       
        public wprCar( Car__c d ) {
            CarId = d.Id;
            Car = d;           
            started = false;
            stopped = false;
            color = '';    
        }
    }
}

Boris BachovskiBoris Bachovski

As far as I understood you want to be able to find the row that you clicked on and change fields on the car record? If that's the case then your controller methods should look something like this:

public void StartCar()
{
for (wprCar car : wprCarList)
{
  if (car.CarId == currentId)
  {
   car.car.Name = 'New Name for started car';
   car.car.Color__c = 'New Color for started car';
   // this is how you reference the actual car record that you're listing on the table
   break;
  }
}
}

public void StopCar()
{
// Same thing for Stop Car
for (wprCar car : wprCarList)
{
  if (car.CarId == currentId)
  {
   car.car.Name = 'New Name for stopped car';
   car.car.Color__c = 'New Color for stopped car';
   // this is how you reference the actual car record that you're listing on the table
   break;
  }
}
}

This was selected as the best answer
Eager-2-LearnEager-2-Learn
Thank you for getting me over this hurtle!  I feel so silly!  I should have known to do that as I have done FOR loops many times in other apex coding efforts just not much VF experience.  On another note -- how do you get the line number with the green bar and alternating light color tone in the code snippet?  What editor is that?
Boris BachovskiBoris Bachovski
When writing the question/answer, in your WYSIWYG editor there is an icon that looks like this '< >' with title 'Add a code sample'. Paste the code there and it will format it automatically.