Cagatay Civici’s Weblog

“Real power can’t be given, it must be taken”

Create a JSF Slider in 5 minutes

Posted by cagataycivici on March 19, 2007

There might be cases where it’d be cool to make users enter the values of an JSF inputtext using a slider. One possible way to do this is to create a custom JSF component and render the javascript but this is hard to create, test and maintain. On the otherhand thanks to the best jsf viewhandler Facelets, it’s sooo easy. Actually I’ve created this example to demonstrate how it’s easy to do stuff like this.

Here’s the slider.xhtml built on top of the cool scriptaculous slider widget;

<ui:composition>
    <t:inputText id=”#{id}” style=”width:100px” value=”#{value}” forceId=”true”></t:inputText>

    <div id=”#{id}_track_id” style=”width:105px;background-color:#aaa;height:5px;”>
        <div id=”#{id}_handle_id” style=”width:5px;height:10px;background-color:#f00;cursor:move;”></div>
    </div>

    <script type=”text/javascript”>
//some scriptaculous script here
         var slider_#{id} = new Control.Slider(’#{id}_handle_id’,'#{id}_track_id’, {range:$R(#{minimum},#{maximum})});              
        slider_#{id}.options.onSlide = function(value){                                 
          $(’#{id}’).value = (value + ”).split(”.”)[0];
        };
    </script>
</ui:composition>

After declaring slider tag in your facelets-taglib file, it’s all set and ready to use as;

<barca:slider id=”ageSlider” value=”#{CustomerCreate.customer.age}” minimum=”18″ maximum=”50″></barca:slider> 

How it looks

Scriptaculous slider’s lookandfeel can be changed using the css attributes so it’s possible to make it look cooler like a swing slider. But that’s not the point, the thing is with facelets, I don’t think there is so much need to write custom JSF components with java because it’s obvious that with facelets composition infrastructure creating composition components or fancy rich components like this slider is almost a no-brainer.

12 Responses to “Create a JSF Slider in 5 minutes”

  1. ziller Says:

    hello,

    your blog is great but i can’t find any rss feed
    does it exists?

    regads

  2. Cagatay Says:

    Hi, doesn’t this work? http://www.jroller.com/rss/cagataycivici

  3. Matthias Wessendorf Says:

    works,
    but I think he misses a *link* to the RSS

  4. Cagatay Says:

    I see, yes you’re right, I should’ve added one:)

  5. Matthias Wessendorf Says:

    Btw. I think the minimum=”18″ maximum=”50″ should be handled by (double/long) range validator :-)

  6. ziller Says:

    great, it works
    didn’t find it in the page…

    thanks

  7. Hazem Ahmed Saleh Says:

    Cagatay,
    I like that fast approach for customizing JSF components.
    Excellant work (y).

  8. Wesley Hales Says:

    Once again Facelets makes life much easier…Or you can use this component that uses the same scriptaculous library and works with JSP and Facelets.
    http://livedemo.exadel.com/richfaces-demo/richfaces/dataFilterSlider.jsf

  9. JC Says:

    Totally agree. Composite components are the way forward…

  10. sha Says:

    cant get the slider to work with events other than page load

  11. nsiw txozpquc Says:

    dkbmaq lmvdu rzyup cqgoh yqvlhci ldfiko penw

  12. sj Says:

    It would take same amount of time in php or jsp. Why is this a big deal?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>