1

When creating a datatable with Primefaces, I want to create a button on both the header and footer facets, but without duplicating code.

Think of having the following header facet on a <p:dataTable/> element:

<f:facet name="header">
    <p:commandButton value="Preview" icon="pi pi-search" update="@form:dPreview"
                     oncomplete="PF('wvdPreview').show()"
    />
</f:facet>

How could I add the same code in another facet (let's say, name="footer") without duplicating the code?

I searched some options on the Internet, but only found the option to duplicate the header's code or to create a brand new component, and I'm not expecting to apply any of those approaches unless there's no other way.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • You can use something like `ui:include` to reduce duplicate code. https://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets – Jasper de Vries Nov 03 '22 at 13:41
  • That's one of the approaches I thought of, the thing is that I must create a new file for doing so, but that's better than nothing! Thank you for your reply! Appreciated! – Nakarukatoshi Uzumaki Nov 03 '22 at 13:44

1 Answers1

7

You can actually put your button in a facet which will be called by your paginatorTemplate of your dataTable and set paginatorPosition to both (which is Default). Something like the following code

<p:dataTable 
     paginatorAlwaysVisible="true"
     paginatorTemplate="{yourCustomTempl}"
     paginatorPosition="both">
        <f:facet name="{yourCustomTempl}">
            <p:commandButton icon="pi pi-download"/>
        </f:facet>
</p:dataTable>
CW_
  • 157
  • 1
  • 10