0

I have a datatable in primefaces with jsf and I need to check when the datatable is empty with jquery/js, but I have just found tags for "normal" datatables that don't work on PF.

Bean

private ArrayList<Curso> curs = null;
private ArrayList<Curso> listado_filtrado;
private DefaultStreamedContent informe_cursos;

Html

<p:outputPanel id="opTabla" >
  <p:dataTable id="tabla_elements"   
                 value="#{Cursos.curs}"
                 var="element"
                 filteredValue="#{Cursos.listado_filtrado}"
                 emptyMessage="No se encontraron elementos"
               paginator="true" rows="20"   
      currentPageReportTemplate="{startRecord} a {endRecord} de {totalRecords}"
         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"  
               paginatorPosition="bottom">
                 
                <p:ajax event="filter" update="exportar"/>
                <f:facet name="header">
                 <div class="header-field-col">
                <p:commandButton id="anadir_curso" value="Añadir curso nuevo" icon="ui-icon-plus" />
              </div>
              <div class="header-field-col"> 
                 
               <p:commandButton id="exportar" value="Exportar" ajax="false" disabled="#{empty Cursos.curs}"  
                   icon="ui-icon-arrowreturnthick-1-s">
                <p:fileDownload value="#{Cursos.informeCursos}" />                
               </p:commandButton>
               
       </div>
                </f:facet> 
  • If you want to merely display a message, whenever a datatable is empty - contains no records, then an attribute named `emptyMessage` can be used. – Tiny Jun 13 '17 at 15:03
  • no, i need it to disable a export commandbutton when the table is empty – Abel Ballesteros Jun 13 '17 at 15:05
  • Then add a disabled attribute to the commandButton that takes the same 'list' as is used in the value of the dataTable but add an empty check there... – Kukeltje Jun 13 '17 at 15:39
  • it didnt work either – Abel Ballesteros Jun 14 '17 at 08:16
  • Read https://stackoverflow.com/questions/8634156/how-to-find-out-client-id-of-component-for-ajax-update-render-cannot-find-compo and see if the 'header' facet is a namingcontainer, or something else... – Kukeltje Jun 14 '17 at 19:33

1 Answers1

1

Your p:dataTable most likely references a list. Add an ajax event handler to it for e.g. the filter (all the ones that can make a page empty)

<p:dataTable value="#{myBean.myList}"...>
    <p:ajax event="filter" update="exportButton" ... >
    ...
</p:dataTable>

use a p:commandButton with a disabled attribute like this:

<p:commandButton id="exportButton" value="export" disabled="#{empty myBean.myList}" ... />

It disables the commandButton client and serverside if the list is empty. So users cannot hack it client-side either.

The 'update' element in the commandButton makes sure the button state is, well,... updated on the relevant events. This is all rather basic ajax stuff, maybe read some tutorial on that

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • i have tried this but, the button is disabled just when i update the page manually not – Abel Ballesteros Jun 14 '17 at 06:19
  • Then you need ajax events in the datatable, mainly on the filter, where you 'update' the commandButton – Kukeltje Jun 14 '17 at 06:38
  • i have tried with an ajax event listen but it does the same – Abel Ballesteros Jun 14 '17 at 07:12
  • Then you have a different problem... This works for me in all the case I use it – Kukeltje Jun 14 '17 at 08:48
  • Read this: https://stackoverflow.com/questions/8634156/how-to-find-out-client-id-of-component-for-ajax-update-render-cannot-find-compo it might help – Kukeltje Jun 14 '17 at 21:12
  • I have tried this and this and the button didnt disabled it, i have seen the name in html of the button is "tabla_elements:exportar" – Abel Ballesteros Jun 15 '17 at 06:50
  • We cannot see if that is good or not unless there is a [mcve] in the question. And you can use backticks to 'mark' code in comments – Kukeltje Jun 15 '17 at 07:05
  • is there any other way to avoid the export when the table is empty? if i will not be able to make this way works – Abel Ballesteros Jun 15 '17 at 14:25
  • Sure, tell the user to **not** press the button. – Kukeltje Jun 21 '17 at 13:35
  • i mean can i trigger a warning when the table is empty and the button pressed – Abel Ballesteros Jun 22 '17 at 07:32
  • Why is this not sufficient? It is the safe solution. Works client AND server side. Any other possible solution either works just client-side (and might thus be 'unsafe/untrusted') or becomes rather complex (as would a client-side solution). Others think it isa good solution (see the upvotes) – Kukeltje Jun 22 '17 at 07:36