+ Start a Discussion
dsh210@lehigh.edudsh210@lehigh.edu 

Rendering elements with javascript methods

Hey, quick question...

 

I am attempting to implement a filtering system where I render certain elements based on their type. I am trying to use a javascript method to control the rendering, but it is always returning false, does anyone know what the issue might be?

 

 

<apex:inputText value="{!def.myDef.Name}" rendered="getFilter()" />
 <script>
  function getFilter(){
   var myBool = true;
   return myBool;
  }
 </script>

 This is simply not rendering the element, which leads me to believe the javascript is not being evaluated, but I am not sure why that would be the case.

 

Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Hi,

 

 

    It is not possible to render/hide the compoents using javascript boolean variables. Try the apex class variables of type boolean instead.

 

    If you really want to control the visibility of components using javascripts, you need to refer that component and change the style attribute "display" to none. Check this site. for more details.

 

Cheers..

All Answers

SargeSarge

Hi,

 

 

    It is not possible to render/hide the compoents using javascript boolean variables. Try the apex class variables of type boolean instead.

 

    If you really want to control the visibility of components using javascripts, you need to refer that component and change the style attribute "display" to none. Check this site. for more details.

 

Cheers..

This was selected as the best answer
dsh210@lehigh.edudsh210@lehigh.edu

Thanks so much for the help, I was hoping to use Javascript booleans because it would be a lot easier, but I am okay using Apex. However, I am wondering how I can set an Apex variable from my visualforce page since I cannot pass parameters or get access to set methods.

 

For example, when the user chooses to filter for only number types, I would like to have that global variable set to 'num', so that when my series of get statements runs, the getIsFilterNumber() returns true.

 

How would I go about working with that global Apex variable?

SargeSarge

Hi

 

   To answer your requirement I would say use the  visualforce tags that supports Ajax.

Few of them are :

<apex: actionSupport/>

<apex:actionFunction/>

 

So if you are choosing the "Number" type  as a filter and you choose that in a picklist,  the "actionSupport" tag can call the apex method of the corresponding controller on change of the selected value, and upon completion of the execution of that method, the "actionSupport" compoenent refreshes  entire page or the specific portion of the page (as mentioned in attributes of "actionsupport" tag). This behaviour is similar to ajax partial page refresh.

So when a part of page refreshes, you can return with the boolean value false and as a result hides the refreshed component.

Check out this resource for an example code

 

Hope this helps.

 

Cheers..