1

Hello I am a new user sales force, My view takes a variable from the controller and displays it onto the view, however, the page seems to refresh back to the initial state(without going back into the constructor). Do you happen to see in my code below the reason why this would be happening? Thanks for your help.

View :

<!-- CSS STYLE TO NOT USE WORD WRAP -->
<style type="text/css">
    .pbTitle {
        white-space: nowrap
    }      
</style> 

<!-- TOP HEADER BLOCK OF THE PAGE TITLE -->
<apex:pageBlock title="Assign Voucher to Project" id="mainBlock" >

<script type="text/javascript">

function enable_test()
{
    console.debug("Begin enable_test : " + '{!showFieldServer}');
    enabledviewField();
    console.debug("Begin disable_test : " + "{!showFieldServer}");
}   

function disable_test()
{
    console.debug("Begin disable_test : " + '{!showFieldServer}');
    disabledviewField();
    console.debug("end : " + "{!showFieldServer}");
}
 </script>
<apex:form >       
    <!--BEGIN:ALL APEX ACTION FUNCTIONS -->
     <apex:actionFunction name="enabledviewField" action="{!enabledviewField_controller}" reRender="inputServerName ,result">
        <apex:param name="enabledviewField" value="" /> 
    </apex:actionFunction>
    <apex:actionFunction name="disabledviewField" action="{!disabledviewField_controller}" reRender="inputServerName ,result">
        <apex:param name="disabledviewField" value="" />    
    </apex:actionFunction>

    <!-- MAIN PANEL TO DISPLAY SEARCH PARAMETER ON THE LEFT AND VOUCHER TABLE ON THE RIGHT OF THE SCREEN -->
    <apex:panelGrid columns="2" style="width:75%;pbTitle;">

        <!-- SEARCH PARAMETERS BLOCK -->
        <apex:pageBlock mode="edit" id="criteria" title="Register Voucher">
            <apex:outputPanel layout="block" id="inputParameters" style="height:300px">
                <table> 
                    <div  align="Left" margin-right="auto">
                        <tr >
                        <td><apex:outputLabel id="l_ServerName" value="Server Name:"   /></td>
                        <td><apex:inputText id="inputServerName"    value="{!inputServerName}" style="{!showFieldServer}" /></td>
                        </tr>  
                    </div>
                </table>
                <apex:outPutPanel layout="block" id="result" >
                <apex:outPutText value="{!showFieldServer}"  />
                </apex:outPutPanel>                      
                <button  onclick="enable_test();" >enable_test</button>
                <button  onclick="disable_test();" >disable_test</button>
            </apex:outputPanel>
        </apex:pageBlock>

    </apex:panelGrid>              
    </apex:form>
    <apex:form>

    </apex:form>
</apex:pageBlock>

Controller :

public  class Controller_OlympusLR_addSingleVoucher{

    public String dynamicFieldId {get; set;}    
    public string showFieldServer  {get; set;}
    public string inputServerName {get; set;}
    public PageReference enabledviewField_controller()
    {
        showFieldServer = 'visibility: visible';
        return null;        
    }

    public PageReference disabledviewField_controller()
    {
        showFieldServer = 'visibility: hidden';
        return null;        
    }     
    //CONSTRUCTOR
    public Controller_OlympusLR_addSingleVoucher(ApexPages.StandardSetController stdcontroller)
    {
        this.dynamicFieldId = 'page:mainBlock:j_id14';
        this.showFieldServer ='visibility: hidden';        
    }
}
  • Please be clear on what is not working. And anyway, you are re rendering an inputfield. That wont work. Re render the container of that field. In your case the PageBlock or the outputpanel. And also to hide a section, why dont you simply use the rendered attribute? – AslamK Nov 25 '14 at 14:25
  • My view takes a variable from the controller and displays it onto the view, however, the page seems to refresh back to the initial state(without going back into the constructor). Do you happen to see in my code below the reason why this would be happening? Thanks for your help – muzin Nov 25 '14 at 14:36
  • 1
    why would it go back to the constructor? also did you understand the part about rerendering the outer sections rather than the inputfield itself? – AslamK Nov 25 '14 at 14:39
  • I don’t understand what I can change in the code. Please, show me the line where I can change to do it working. Thanks – muzin Nov 25 '14 at 15:20
1

you are re rendering an inputfield. That wont work. Re render the container of that field. In your case the PageBlock or the outputpanel.

<apex:actionFunction name="enabledviewField" action="{!enabledviewField_controller}" reRender="criteria">

Also maybe its the button that is causing your page to refresh. Instead of <button> try to replace with <apex:commandButton>

<apex:commandButton action="{!enabledviewField_controller}" rerender="criteria" value="enable_test"/>'

With this you don't even need the actionfunction for your requirement.

Based on what I understand of your problem statement, you want to show or hide an input field on press of a button. How I would do that is shown below

Page

<apex:pageBlock>
<apex:form>
    <apex:pageBlock id="criteria">
        <apex:outputLabel>Enter Value</apex:outputLabel>
        <apex:inputText value="{!inputServerName}" rendered="{!show}" />

        <apex:commandButton value="Enable" action="{!enable}" rerender="criteria" rendered="{!!show}"/>
        <apex:commandButton value="Disable" action="{!disable}" rerender="criteria" rendered="{!show}"/>
    </apex:pageBlock>
</apex:form>
</apex:pageBlock>
</apex:page>

Controller

public class somecontroller{

public Boolean show{get; set;}
public String inputServerName{get; set;}

    public somecontroller(){
        show = true;
    }
    public void enable(){
        show = true;        
    }
    public void disable(){
        show = false;
    }

}
  • I make the change but the problems steel her. The input field display in the view less than one second and hidden after. – muzin Nov 25 '14 at 15:51
  • your controller methods, make the return type void. Since you are not redirecting anywhere, there is no use of it returning PageReference. Lets see if that helps. – AslamK Nov 25 '14 at 15:59
  • I make the change but the problems also steel her – muzin Nov 25 '14 at 16:04
  • 2
    Added some more changes to answer. But really, your code is needlessly going around in circles. For what you are trying to achieve ( i believe simply hiding an inputfield ) you dont need Javascript at all. You can very easily achieve this with simple Visualforce. Better you try to re-write it ensuring good MVC practices. – AslamK Nov 25 '14 at 16:33

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.