+ Start a Discussion
Javier CastroJavier Castro 

Passing values from pageBlockTable to custom controller

Hi, I'm trying to pass values from a pageBlockTable to my custom controller, but something is not working properly and I don't what it is. I post my code below and I hope someone can help me.

Thanks in advance.

Visualforce code:
<div calss="modal-dialog" style="display:{!if(mostrarCitasPaciente,'block','none')};">
    <div class="tablemodal-container">
      <apex:form id="tabla">
        <apex:pageBlock >
          <apex:pageBlockTable value="{!lstCitas}" var="citas" styleClass="table table-hover">
            <apex:column value="{!citas.Name}"/>
            <apex:column value="{!citas.CloseDate}"/>
            <apex:column value="{!citas.Doctor__c}"/>
            <apex:column value="{!citas.Doctor__r.Especialidad__c}"/>
            <apex:column>
              <apex:commandButton value="Ver cita" action="{!mostrarCitaIndividualPaciente}">
                  <apex:param name="doctor" value="{!citas.Doctor__c}" assignTo="{!doctorCita}"/>
            	  <apex:param name="especialidad"  value="{!citas.Doctor__r.Especialidad__c}" assignTo="{!especialidadCita}"/>
                  <apex:param name="fecha" value="{!citas.CloseDate}" assignTo="{!fechaCita}"/>
              </apex:commandButton>
            </apex:column>
          </apex:pageBlockTable>
          <table class="table borderless" align="center">
            <tr>
              <td align="center">
                <apex:commandButton styleClass="btn btn-secondary" value="Primera pagina" reRender="tabla" action="{!primeraPagina}" disabled="{!Ant}"/>
                <apex:commandButton styleClass="btn btn-secondary" value="Pagina anterior" reRender="tabla" action="{!anterior}" disabled="{!Ant}"/>
                <apex:commandButton styleClass="btn btn-secondary" value="Pagina siguiente" reRender="tabla" action="{!siguiente}" disabled="{!Sig}"/>
              </td>
            </tr>
          </table>
        </apex:pageBlock>
      </apex:form>
    </div>
  </div>

<div class="modal-dialog" >
    <div class="tablemodal-container">
      <table align="center" id="RID">
        <tr>
          <td align="left"><apex:outputLabel value="Doctor"/></td>
          <td align="center"><apex:outputLabel value="Especialidad"/></td>
          <td align="right"><apex:outputLabel value="Fecha"/></td>
        </tr>
        <tr>
          <td>{!doctorC}</td>
          <td>{!especialidadC}</td>
          <td>{!fechaC}</td>
        </tr>
      </table>
    </div>
  </div>

Controller code:
 
public class controlador{

    public String doctorCita{ get; set; }
    public String especialidadCita{ get; set; }
    public String fechaCita{ get; set; }
    public String doctorC{ get; set; }
    public String especialidadC{ get; set; }
    public String fechaC{ get; set; }

public void obtenerCitaIndiviudalPaciente(){
        
        doctorC = this.doctorCita;
        especialidadC = this.especialidadCita;
        fechaC = this.fechaCita;

//My logic

 }

}

My second div is displayed but I always get null in my params. If I put a reRender attribute on the command button I get the correct values in my params but the second div is not displayed.
Best Answer chosen by Javier Castro
Alain CabonAlain Cabon
Could you try two panels like below?  reRender="panel1,panel2"

  <!-- div citas paciente-->
<apex:outputPanel id="panel1">
  <div calss="modal-dialog" style="display:{!if(mostrarCitasPaciente,'block','none')};">
    <div class="tablemodal-container">
      <apex:form id="tabla">
        <apex:pageBlock >
          <apex:pageBlockTable value="{!lstCitas}" var="citas" styleClass="table table-hover">
            <apex:column value="{!citas.Name}"/>
            <apex:column value="{!citas.CloseDate}"/>
            <apex:column value="{!citas.Doctor__c}"/>
            <apex:column value="{!citas.Doctor__r.Especialidad__c}"/>
            <apex:column >
              <apex:commandButton value="Ver cita" action="{!mostrarCitaIndividualPaciente}" reRender="panel1,panel2">
                  <apex:param name="doctor" value="{!citas.Doctor__c}" assignTo="{!doctorCita}"/>
            	  <apex:param name="especialidad"  value="{!citas.Doctor__r.Especialidad__c}" assignTo="{!especialidadCita}"/>
                  <apex:param name="fecha" value="{!citas.CloseDate}" assignTo="{!fechaCita}"/>
              </apex:commandButton>
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
      </apex:form>
    </div>
  </div>
</apex:outputPanel>

<!-- 2nd div group -->
<!--div cita individual paciente-->
<apex:outputPanel id="panel2">
  <div class="modal-dialog" style="display:{!if(mostrarCitaIndividualPaciente,'block','none')};">
    <div class="tablemodal-container">
      <table align="center">
        <tr>
          <td align="left"><apex:outputLabel value="Doctor"/></td>
          <td align="center"><apex:outputLabel value="Especialidad"/></td>
          <td align="right"><apex:outputLabel value="Fecha"/></td>
        </tr>
        <tr>
          <td>{!doctorCita}</td>
          <td>{!especialidadC}</td>
          <td>{!fechaC}</td>
        </tr>
      </table>  
    </div>
  </div>
 </apex:outputPanel>

All Answers

Alain CabonAlain Cabon
Hi;

There is a known problem with <apex:param> and <apex:commandButton> and you need a reRender Id (could be dummy, targetted an empty panel for instance)

Need to be add a DummyID for reRender.
https://salesforce.stackexchange.com/questions/187539/passing-parameters-commandbutton
 
<apex:outputPanel id="panel2">
</apex:outputPanel>
<apex:commandButton ... reRender="panel2">
  <apex:param ... />
</apex:commandButton>
Javier CastroJavier Castro
With the reRender attribute in the commandbutton and the dummyId on the outputPanel, the params get the correct values but my second "div group" is not displayed when I click the button and it should.

I'm going to write my new simplified code below.

Visualforce code:
 
<!-- dummy panel id -->	
  <apex:outputPanel id="panel2">
  </apex:outputPanel>

  <!-- div citas paciente-->
  <div calss="modal-dialog" style="display:{!if(mostrarCitasPaciente,'block','none')};">
    <div class="tablemodal-container">
      <apex:form id="tabla">
        <apex:pageBlock >
          <apex:pageBlockTable value="{!lstCitas}" var="citas" styleClass="table table-hover">
            <apex:column value="{!citas.Name}"/>
            <apex:column value="{!citas.CloseDate}"/>
            <apex:column value="{!citas.Doctor__c}"/>
            <apex:column value="{!citas.Doctor__r.Especialidad__c}"/>
            <apex:column >
              <apex:commandButton value="Ver cita" action="{!mostrarCitaIndividualPaciente}" reRender="panel2">
                  <apex:param name="doctor" value="{!citas.Doctor__c}" assignTo="{!doctorCita}"/>
            	  <apex:param name="especialidad"  value="{!citas.Doctor__r.Especialidad__c}" assignTo="{!especialidadCita}"/>
                  <apex:param name="fecha" value="{!citas.CloseDate}" assignTo="{!fechaCita}"/>
              </apex:commandButton>
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
      </apex:form>
    </div>
  </div>

<!-- 2nd div group -->
<!--div cita individual paciente-->
  <div class="modal-dialog" style="display:{!if(mostrarCitaIndividualPaciente,'block','none')};" reRender="panel2">
    <div class="tablemodal-container">
      <table align="center">
        <tr>
          <td align="left"><apex:outputLabel value="Doctor"/></td>
          <td align="center"><apex:outputLabel value="Especialidad"/></td>
          <td align="right"><apex:outputLabel value="Fecha"/></td>
        </tr>
        <tr>
          <td>{!doctorCita}</td>
          <td>{!especialidadC}</td>
          <td>{!fechaC}</td>
        </tr>
      </table>  
    </div>
  </div>

Controller code:
 
public class controlador{
    
    //Vars
    public Boolean mostrarCitasPaciente{ get; set; }
    public Boolean mostrarCitaIndividualPaciente{ get; set; }
    public String doctorCita{ get; set; }
    public String especialidadCita{ get; set; }
    public Date fechaCita{ get; set; }
    public String doctorC{ get; set; }
    public String especialidadC{ get; set; }
    public Date fechaC{ get; set; }

       /*More vars ....*/

   //Constructor
   public controlador() {
        mostrarCitasPaciente = true;
        mostrarCitaIndividualPaciente = false;
    }
  
  //Methods

  public void mostrarCitaIndividualPaciente(){
        obtenerCitaIndiviudalPaciente();
        mostrarCitasPaciente=false;
        mostrarCitaIndividualPaciente=true;
    }

   public void obtenerCitaIndiviudalPaciente(){
        doctorC = this.doctorCita;
        especialidadC = this.especialidadCita;
        fechaC = this.fechaCita;

    }

  /*More methods ...*/

}

What I want to do is when I click the button, the second "div group" should be displayed with the values corresponding to the row on the table, because the vars should have the value of the params.
I can see in the console logs that the params has the correct values but for some reason the second div group is not displayed, and this is what I want to solve now.

Thanks in advance.​


 
Alain CabonAlain Cabon
Could you try two panels like below?  reRender="panel1,panel2"

  <!-- div citas paciente-->
<apex:outputPanel id="panel1">
  <div calss="modal-dialog" style="display:{!if(mostrarCitasPaciente,'block','none')};">
    <div class="tablemodal-container">
      <apex:form id="tabla">
        <apex:pageBlock >
          <apex:pageBlockTable value="{!lstCitas}" var="citas" styleClass="table table-hover">
            <apex:column value="{!citas.Name}"/>
            <apex:column value="{!citas.CloseDate}"/>
            <apex:column value="{!citas.Doctor__c}"/>
            <apex:column value="{!citas.Doctor__r.Especialidad__c}"/>
            <apex:column >
              <apex:commandButton value="Ver cita" action="{!mostrarCitaIndividualPaciente}" reRender="panel1,panel2">
                  <apex:param name="doctor" value="{!citas.Doctor__c}" assignTo="{!doctorCita}"/>
            	  <apex:param name="especialidad"  value="{!citas.Doctor__r.Especialidad__c}" assignTo="{!especialidadCita}"/>
                  <apex:param name="fecha" value="{!citas.CloseDate}" assignTo="{!fechaCita}"/>
              </apex:commandButton>
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
      </apex:form>
    </div>
  </div>
</apex:outputPanel>

<!-- 2nd div group -->
<!--div cita individual paciente-->
<apex:outputPanel id="panel2">
  <div class="modal-dialog" style="display:{!if(mostrarCitaIndividualPaciente,'block','none')};">
    <div class="tablemodal-container">
      <table align="center">
        <tr>
          <td align="left"><apex:outputLabel value="Doctor"/></td>
          <td align="center"><apex:outputLabel value="Especialidad"/></td>
          <td align="right"><apex:outputLabel value="Fecha"/></td>
        </tr>
        <tr>
          <td>{!doctorCita}</td>
          <td>{!especialidadC}</td>
          <td>{!fechaC}</td>
        </tr>
      </table>  
    </div>
  </div>
 </apex:outputPanel>
This was selected as the best answer
Javier CastroJavier Castro
It works!!! You saved me. Thank you so much.