1

I have a datatable that is showing results from a query. I wanted to, throught a rerender (or something similar), that the datatable refreshs with the parameters that I'm passing to controller. I have come to this:

VF PAGE

    <apex:page>
    <apex:pageBlock>
    //another forms here, cuted to reduce the code size
    </apex:pageBlock>
    <apex:pageBlock id="pb">
  <apex:form >
      <apex:outputLabel value="FROM:  " style="font-weight: bold"/>
      <apex:inputField value="{!record.Data_Hora_Levantamento_Viatura__c}" required="true"/>
      <p/>
      <apex:outputLabel value="TO:  " style="font-weight: bold"/>
      <apex:inputField value="{!record.Data_Hora_Entrega_Viatura__c}" required="true"/>
      <p/>
      <apex:commandButton value="Filter">
          <apex:actionSupport event="onclick" rerender="PageBlock"/>
      </apex:commandbutton>
  </apex:form>
  <p></p>      
    <apex:outputPanel layout="block" style="max-height:250px; overflow:scroll" id="outP">
              <apex:dataTable id="dataTable" var="e" value="{!Veiculos}" border="1" frame="hsides"
                cellspacing="0" cellpadding="5" width="100%">
              <apex:column >
                  <apex:facet name="header">Titular</apex:facet>
                  <apex:outputText value="{!e.Owner.Name}"/>
              </apex:column>
              <apex:column >
                  <apex:facet name="header">Matricula</apex:facet>
                  <apex:outputText value="{!e.Matriculas_Viaturas__c}"/>
              </apex:column>
              <apex:column >
                  <apex:facet name="header">Data/Hora Levantamento</apex:facet>
                  <apex:outputText value="{0,date,dd-MM-yyyy hh:MM}">
                      <apex:param value="{!e.Data_Hora_Levantamento_Viatura__c}"/>
                  </apex:outputText>  
              </apex:column>
              <apex:column >
                  <apex:facet name="header">Duracao Prevista</apex:facet>
                  <apex:outputText value="{!e.Dura_o_Prevista_horas__c}"/> 
              </apex:column>
              <apex:column >
                  <apex:facet name="header">Data/Hora Entrega Prevista </apex:facet>
                  <apex:outputText value="{0,date,dd-MM-yyyy hh:MM}">
                      <apex:param value="{!e.Data_Entrega_prevista__c}"/>
                  </apex:outputText>   
              </apex:column>
              <apex:column >
                  <apex:facet name="header">Estado</apex:facet>
                  <apex:outputText value="{!e.Estado__c}"/>   
              </apex:column>
              </apex:dataTable>
              </apex:outputPanel>
              </apex:pageBlock> </apex:page>

CONTROLLER

public with sharing class OrbC_VeiculosExtension 
{

    public Veiculos__c record {get;set;}
    public Id currentId {get;set;}
    public List<Veiculos__c> Veiculos {
    get {
        if (Schema.sObjectType.Veiculos__c.isAccessible())
        {
            system.debug(record.Data_Hora_Levantamento_Viatura__c);
            system.debug(record.Data_Hora_Entrega_Viatura__c);
            if (record.Data_Hora_Levantamento_Viatura__c==null && record.Data_Hora_Entrega_Viatura__c==null)
            {
                Veiculos = [SELECT Matriculas_Viaturas__c,Data_Hora_Levantamento_Viatura__c,Estado__c,Dura_o_Prevista_horas__c,Data_Entrega_prevista__c,Data_Hora_Entrega_Viatura__c, Owner.Name FROM Veiculos__c WHERE Data_Hora_Levantamento_Viatura__c >= TODAY ORDER BY Data_Hora_Levantamento_Viatura__c];
            }
            else if (record.Data_Hora_Levantamento_Viatura__c!=null && record.Data_Hora_Entrega_Viatura__c==null)
            {
                Veiculos = [SELECT Matriculas_Viaturas__c,Data_Hora_Levantamento_Viatura__c,Estado__c,Dura_o_Prevista_horas__c,Data_Entrega_prevista__c,Data_Hora_Entrega_Viatura__c, Owner.Name FROM Veiculos__c WHERE Data_Hora_Levantamento_Viatura__c >= :record.Data_Hora_Levantamento_Viatura__c ORDER BY Data_Hora_Levantamento_Viatura__c];
            }
            else if (record.Data_Hora_Levantamento_Viatura__c==null && record.Data_Hora_Entrega_Viatura__c!=null)
            {
                Veiculos = [SELECT Matriculas_Viaturas__c,Data_Hora_Levantamento_Viatura__c,Estado__c,Dura_o_Prevista_horas__c,Data_Entrega_prevista__c,Data_Hora_Entrega_Viatura__c, Owner.Name FROM Veiculos__c WHERE Data_Entrega_prevista__c <= :record.Data_Entrega_prevista__c ORDER BY Data_Hora_Levantamento_Viatura__c];
            }
            else if (record.Data_Hora_Levantamento_Viatura__c!=null && record.Data_Hora_Entrega_Viatura__c!=null)
            {
                Veiculos = [SELECT Matriculas_Viaturas__c,Data_Hora_Levantamento_Viatura__c,Estado__c,Dura_o_Prevista_horas__c,Data_Entrega_prevista__c,Data_Hora_Entrega_Viatura__c, Owner.Name FROM Veiculos__c WHERE Data_Hora_Levantamento_Viatura__c >= :record.Data_Hora_Levantamento_Viatura__c AND Data_Entrega_prevista__c <= :record.Data_Entrega_prevista__c ORDER BY Data_Hora_Levantamento_Viatura__c];
            }

    }
    return Veiculos;
    }
    set;}
    public datetime inicio {get;set;}
    public datetime fim {get;set;}
    public Boolean isRendered {get; set;}

    public OrbC_VeiculosExtension(ApexPages.StandardController controller) {
        this.record = (Veiculos__c)controller.getRecord();
        this.currentId = record.Id;
        isRendered = false;
        system.debug(currentId);
        system.debug(record);

    }

When I click the "Filter" button, the pages refreshes, and the inputfields (that I cut from the code to reduce it) get the values from the "FROM" and "TO" fields, but the table refreshs like they are null. I'm out of ideas! How can I get this to work?

  • You need to have a action in order for the button to set the value and serenader properly...... You can add a method public void doNothing() to the class and add that as an action to the button. No need to have the action support either, simply put the serenader on the button – Eric Jun 16 '15 at 17:28
  • Thanks for the response @Eric . But your response made me confuse. If I dont have a action support, how I will rerender the datatable? Can you show what you saying with an example? – danielpm Jun 16 '15 at 17:33
  • 1
    <apex:commandbutton action="{!donothing}" value="Filter" rerender="outP" /> – Prady Jun 16 '15 at 17:36
2

Rerender works when there is a post operation from within page. In your code provided above, I see some problems:-

<apex:commandButton value="Filter">
          <apex:actionSupport event="onclick" rerender="PageBlock"/>
      </apex:commandbutton>

Secondly, rerender needs to have the id of the pageblock to be refreshed.

As pointed out of Eric, it should be binded to an action in controller. So, if you add a method

public void refreshDataTable(){
    //do nothing
}

and modify your VF Code (only the button part):-

<apex:commandButton value="Filter" action="refreshDataTable" rerender="pb">
</apex:commandbutton>

Please note:- the above code is not tested, so may need some tweaking.

  • Perfect example, with minor changes works like a charm! Thanks! – danielpm Jun 17 '15 at 8:35
1
  1. Change the line <apex:actionSupport event="onclick" rerender="PageBlock"/> to <apex:actionSupport event="onclick" rerender="outP"/> .

  2. add action attribute in your commandButton and point it to a method in controller which will refresh the values in Veiculos list.

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.