This post is about using JavaScript in ADF Faces to change default properties , sometimes using JavaScript can make task easier and all scenarios covered in this post are based on very common requirement. One important point is - s
et clientComponent property of component to true when using JavaScript on that
Why this is important ? (Check what docs say)
whether a client-side component will be generated. A component may be
generated whether or not this flag is set, but if client Javascript
requires the component object, this must be set to true to guarantee the
component's presence. Client component objects that are generated
today by default may not be present in the future; setting this flag is
the only way to guarantee a component's presence, and clients cannot
rely on implicit behavior. However, there is a performance cost to
setting this flag, so clients should avoid turning on client components
unless absolutely necessary
Read more about clientComponent property -
Understanding ADF Faces clientComponent attribute
Set panel group layout properties-
Use this JavaScript function to set panel group layout's layout and other properties
<!--Function to set panelGroupLayout properties-->
function changeGroupLayout(evt) {
var pgl = AdfPage.PAGE.findComponent('pgl1');
pgl.setProperty("layout", "vertical");
pgl.setProperty("inlineStyle", "background-color:red");
}
I have called this function using client listener on a image that is inside my panel group layout
<af:panelGroupLayout id="pgl1" layout="horizontal" clientComponent="true">
<af:image source="#{resource['images:5-10.jpg']}" id="i1" inlineStyle="width:250px;height:200px;"/>
<af:image source="#{resource['images:13.jpg']}" id="i2" inlineStyle="width:250px;height:200px;">
<af:clientListener method="changeGroupLayout" type="dblClick"/>
</af:image>
<af:image source="#{resource['images:1.jpg']}" id="i3" inlineStyle="width:250px;height:200px;"/>
</af:panelGroupLayout>
Initially group layout is horizontal-
After executing JavaScript on double click on second image-
Set input component property (inlineStyle, contentStyle, value etc)-
This function is same as previous one , this function sets value in input text , changes it's contentStyle
<!--Function to set af:inputText properties-->
function changeInputText(evt) {
var iText = AdfPage.PAGE.findComponent('it1');
iText.setProperty("value", "Ashish Awasthi");
iText.setProperty("contentStyle", "background-color:red;color:white;font-weight:bold;");
}
Called this function on double click event in inputText-
<af:inputText label="Label 1" id="it1" clientComponent="true" unsecure="disabled">
<af:clientListener method="changeInputText" type="dblClick"/>
</af:inputText>
Output is like this-
on double click inside inputText
In same way we can set disabled property of component . It is a secure property of component , that should not be changed from a client side event normally but if this is a requirement then we have to set
disabled in
unsecure property of input component. Only disable property is supported as of now
Read more about this property -
<af:inputText>
Set panelSplitter width according to browser window width-
This JavaScript function divides af:panelSplitter in equal parts to fit in browser
<!--Function to set panel Splitter position-->
function changePanelSpliterPosition(evt) {
var width = window.innerWidth;
var ps = AdfPage.PAGE.findComponent('ps1');
ps.setProperty("splitterPosition", width / 2);
}
In same way try setting other properties of different components. Soon i will update this post with some more JavaScript functions and examples
Cheers :) Happy Learning