Sunday, October 5, 2008

Implementing custom javascript effects in IceFaces

Using icefaces you sometimes need some custom JavaScript functionality. A good way to do this is extending the effects machinery.

We are going to implement a scroll effect which will allow to scroll down a textarea on demand as an example.

First of all we are going to create the JavaScript functionality we are going to use:
<script type="text/javascript">
function scrollDown(id) {
ta=document.getElementById(id);
ta.scrollTop=ta.scrollHeight;
}
</script>
you may want to use a more elaborated name to avoid name clashing.
Now we are going to create the Java class that would allow us to control the effect from the Java side:

import com.icesoft.faces.context.effects.Effect;

public class ScrollDown extends Effect {

@Override //This annotation is optional but advisable
public String getFunctionName() {
return "scrollDown";
}
}
Now you can use this Effect as any other in your code. Just remember that the JavaScript function must be present for the effect to work.
Enjoy!

No comments: