How to align button horizontally in primefaces column?

15,747

Wrap them with a <h:panelGrid/> or a <p:panelGroup/>. This will render a table with two table cells in the same table row (so they must be appear in the same row):

With h:panelGrid:

<h:panelGrid columns="2">
    <p:commandButton value="Edit" icon="ui-icon-scissors" />
    <p:commandButton value="Delete" icon="ui-icon-trash" />
</h:panelGrid>

With h:panelGroup:

If you use h:panelGroup without style, styleClass or id attributes the layout attribute has no effect and no additional html will be rendered around your buttons. Anyway, adding the style:

<h:panelGroup style="white-space: nowrap">
    ....
</h:panelGroup>

makes them horizontally aligned, no matter which layout value you are using.

Share:
15,747
Mark Estrada
Author by

Mark Estrada

Updated on June 27, 2022

Comments

  • Mark Estrada
    Mark Estrada almost 2 years

    I am having a layout problem in one of my primefaces UI. I basically have a table wrapped in a DIV with the code below. The last column should contain buttons that I wanted to arrange horizontally so I enclosed them in a panelGroup

    <div style="overflow: auto; height: 300px;">
        <p:dataTable >
            <p:column headerText="Column 1">
            </p:column>
            .
            .
            <p:column headerText="Actions">
                <h:panelGroup layout="span">
                    <p:commandButton value="Edit" icon="ui-icon-scissors" />
                    <p:commandButton value="Delete" icon="ui-icon-trash" />
                </h:panelGroup>
            </p:column>
        </p:dataTable>
    </div>
    

    However, when they are rendered the buttons are arrange vertically. Viewing the HTML code I notice that the buttons are both wrapped in a div so thats why there is a newline between them.

    How to fix this problem?

    Thanks

    • Akos K
      Akos K over 11 years
      what is your PrimeFaces version? on 3.4.1, with your code they are wrapped into the same div.
    • Akos K
      Akos K over 11 years
      I updated my answer with an alternative h:panelGroup solution.
  • Daniel Díaz Astudillo
    Daniel Díaz Astudillo almost 3 years
    I think the last panelGroup it's not correctly closed. Should be... </h:panelGroup>
  • Akos K
    Akos K almost 3 years
    Thanks, fixed after 9 years. :)
  • Daniel Díaz Astudillo
    Daniel Díaz Astudillo almost 3 years
    It's never too late when it comes to Java