5

the layout of h:selectOneRadio can either go horizontal or vertical, so is there a way that I can do some custom layout. For example, instead of displaying 8 radio button, display them in 2 rows with 4 on each row? Please provide your answer beside PrimeFaces p:selectOneRadio solution, it use CSS3 causing IE8 to display the radio button in rectangle shape.

Thang Pham
  • 38,125
  • 75
  • 201
  • 285

1 Answers1

5

It's not exactly that, but you could use Tomahawk's <t:selectOneRadio> with the layout attribute set to "spread" to have a markupless rendering of radio buttons. You can then use <t:radio> to place the individual radio buttons in the markup the way you want, such as in a <h:panelGrid columns="4">.

E.g.

<t:selectOneRadio id="foo" value="#{bean.selectedItem}" layout="spread">
    <f:selectItems value="#{bean.availableItems}" />
</t:selectOneRadio>

<h:panelGrid columns="4">
    <t:radio for="foo" index="0" />
    <t:radio for="foo" index="1" />
    <t:radio for="foo" index="2" />
    <t:radio for="foo" index="3" />

    <t:radio for="foo" index="4" />
    <t:radio for="foo" index="5" />
    <t:radio for="foo" index="6" />
    <t:radio for="foo" index="7" />
</h:panelGrid>

or even when the amount of radio buttons is unspecified

<h:panelGrid columns="4">
    <c:forEach items="#{bean.availableItems}" varStatus="loop">
        <t:radio for="foo" index="#{loop.index}" />
    </c:forEach>
</h:panelGrid>

(note that <ui:repeat> is not suitable as it runs during view render time and thus end up as a single column of <h:panelGrid>, you'd need to use plain HTML <table> instead)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much :) Tomhawk works for now, but when I download Tomhawk via mvn, it downloaded so many extra jar files, which seems a bit overkill to me. I see you have a tutorial about styling selectOneMenu by extend MenuRenderer, I will take a look at it, maybe it will help me create a custom selectOneRadio component. Thank you very much – Thang Pham Jun 29 '12 at 17:17
  • Tomahawk is open source, you could also take a peek in it. Yes, it comes with a whole bunch of dependencies. For alone the `t:selectOneRadio`, just those Apache Commons dependencies are sufficient, all others are unnecessary. – BalusC Jun 29 '12 at 17:46
  • Yup, it does come with lots of dependencies. We add dependencies to our app via maven, and i do `org.apache.myfaces.tomahawk tomahawk20 1.1.13`, it comes with a whole lot, is there a way to avoid this, BalusC? – Thang Pham Jul 02 '12 at 16:05
  • Thank you, you have been very helpful, I will expose this to the maven community. – Thang Pham Jul 02 '12 at 17:39