title: Rendering JSF/PrimeFaces components based on the number of rows returned by the PrimeFaces LazyDataModel<T>
body: <p>I need to render some components based on the number of rows returned by the PrimeFaces <code>LazyDataModel&lt;T&gt;</code>.</p>
  
  <p>For example, a <code>&lt;p:panelGrid&gt;</code> should be rendered only when there is at least one row is returned by the associated lazy data model.</p>
  
  <pre><code>&lt;p:panelGrid rendered=#{bean.rowCount gt 0}&gt;
  
      ...
      ...
      ...
  
  &lt;/p:panelGrid&gt;
  </code></pre>
  
  <p>Given below a <code>&lt;p:dataList&gt;</code> which is lazily loaded.</p>
  
  <pre><code>&lt;p:dataList var="row"
              value="#{bean}"
              first="0"
              rows="10"
              paginator="true"
              lazy="true"&gt;
  
              ...
              ...
              ...
  
  &lt;p:dataList&gt;
  </code></pre>
  
  <p>The bean is quite usual :</p>
  
  <pre><code>@Named
  @ViewScoped
  public class Bean extends LazyDataModel&lt;Entity&gt; implements Serializable
  {
      @Inject
      private BeanService service;
      private static final long serialVersionUID = 1L;
  
      @Override
      public List&lt;Entity&gt; load(int first, int pageSize, String sortField, SortOrder sortOrder, Map&lt;String, Object&gt; filters)
      {
          setRowCount(service.rowCount());
          return service.getList(first, pageSize);
      }    
  }
  </code></pre>
  
  <p>The given <code>&lt;p:panelGrid&gt;</code> will not be rendered anymore because the lazy data model is unavailable, when the <code>rendered</code> attribute of <code>&lt;p:panelGrid&gt;</code> is processed.</p>
  
  <p>The correct thing can happen, if the order of both <code>&lt;p:panelGrid&gt;</code> and <code>&lt;p:dataList&gt;</code> is interchanged but it is a design issue and cannot be thought of.</p>
  
  <p>The given <code>&lt;p:dataList&gt;</code> holds a complex list of reporting data for preview (in combination with <code>&lt;p:dataTable&gt;</code>, <code>&lt;p:panelGrid&gt;</code> and some others) which is then exported to an MS-Excel workbook and the file is downloaded.</p>
  
  <p>The list of reporting data (held by <code>&lt;p:dataList&gt;</code>) depends upon several (custom) filters composed of <code>&lt;p:inputText&gt;</code>, <code>&lt;p:selectOneMenu&gt;</code> etc. The list is AJAX updated based on the filter values/events.</p>
  
  <p>The list can be empty at anytime depending upon the values of filters. In any case, if the <code>&lt;p:dataList&gt;</code> is empty, the <code>&lt;p:panelGrid&gt;</code> must not be rendered (<code>&lt;p:panelGrid&gt;</code> is just a simple example. There are other components too).</p>
  
  <p>How can such components be rendered based on the number of rows returned by <code>LazyDataModel&lt;T&gt;</code> (ultimately from the associated database) without any extra overhead of executing extra JPA queries to count the number of rows which could be returned based on the values of filters (when there are already very complex/expensive reporting JPA criteria queries are being executed)?</p>