title: filter p:dataTable by date range with pagination
body: <p>I have made a PrimeFaces data table, which can be filtered (see below).  It works fine, but now I have attempted to create the ability to filter by start and end date, selecting dates via the calendar (it should find dates between those two dates; notice start date/end date and filter dates button at the top).  I need it to filter by start and end date just like it filters by everything else.  </p>
  
  <p>It is invoked via a custom method, but it doesn't return the correct values, and most importantly if I select another page on the paginator it defaults back to the unfiltered list.  Which is something of a disaster.  I'd like help to make it so that the user can filter by start/end date as part of the whole filtering system.  Right now start/end date fields are part of the header. </p>
  
  <p>I thought maybe if the filter dates fields modify the list (bean property) used to pull the data for the table (tableFiltered), this will ensure the data pulled is always limited to the correct dates which have been filtered.  But it didn't work like that.  I don't understand.  </p>
  
  <p><a href="https://i.stack.imgur.com/pZ6Pf.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pZ6Pf.jpg" alt="enter image description here"></a></p>
  
  <p>Inside home.xhtml</p>
  
  <pre><code>&lt;p:outputPanel id="tableContainer"&gt;
  &lt;p:remoteCommand name="filterByDate" action="#{homeController.FilterByDate()}" update="tableContainer" /&gt;
  &lt;p:dataTable var="hbel" id="hbelList" value="#{homeController.tableFiltered}" rows="10"
      paginator="true"
      paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
      rowsPerPageTemplate="10,100,1000"
      widgetVar="widgetTable"
      tableStyle="width:auto"&gt;
  
  &lt;f:facet name="header"&gt;
      &lt;p:outputPanel&gt;
      &lt;p:calendar id="calStartDate" value="#{homeController.startDate}" maxdate="#{homeController.endDate}" navigator="true" pattern="dd-MMM-yy" placeholder=" start date"&gt;
          &lt;p:ajax process="calStartDate" partialSubmit="true" event="change"/&gt; 
      &lt;/p:calendar&gt;
  
      &lt;p:calendar id="calEndDate" value="#{homeController.endDate}" maxdate="#{homeController.endDate}" navigator="true" pattern="dd-MMM-yy" placeholder=" end date"&gt;
          &lt;p:ajax process="calEndDate" partialSubmit="true" event="change"/&gt; 
      &lt;/p:calendar&gt;
  
      &lt;p:commandButton id="btnFilter" value="Filter Dates" ajax="true"&gt;
          &lt;p:ajax oncomplete="filterByDate()" /&gt;
      &lt;/p:commandButton&gt;
  
      &lt;/p:outputPanel&gt;
  &lt;/f:facet&gt;
  
      &lt;p:column filterStyle="width: 48px;" filterBy="#{hbel.batchId}" headerText="BatchID" filterMatchMode="exact"&gt;
          &lt;h:outputText value="#{hbel.batchId}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterStyle="width: 48px;" filterBy="#{hbel.recordId}" headerText="RecordID" filterMatchMode="exact"&gt;
          &lt;h:outputText value="#{hbel.recordId}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterBy="#{hbel.unitName}" headerText="UnitName" filterMatchMode="in"&gt;
          &lt;f:facet name="filter"&gt;
              &lt;p:selectCheckboxMenu label="Unit" onchange="PF('widgetTable').filter()" panelStyle="width:125px" scrollHeight="150"&gt;
                  &lt;f:selectItem itemLabel="Location" itemValue="LOCATION"/&gt;
                  &lt;f:selectItem itemLabel="Unit" itemValue="UNIT" /&gt;
                  &lt;f:selectItem itemLabel="Customer" itemValue="DIRECT_CUSTOMER" /&gt;
                  &lt;f:selectItem itemLabel="Occupancy" itemValue="OCCUPANCY" /&gt;
                  &lt;f:selectItem itemLabel="Bank Account" itemValue="BANK_ACCOUNT" /&gt;
              &lt;/p:selectCheckboxMenu&gt;
          &lt;/f:facet&gt;
          &lt;h:outputText value="#{hbel.unitName}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterBy="#{hbel.logDate}" headerText="LogDate" filterMatchMode="exact"&gt;
          &lt;h:outputText value="#{hbel.logDate}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterBy="#{hbel.logFlag}" headerText="LogFlag" filterMatchMode="in"&gt;
          &lt;f:facet name="filter"&gt;
              &lt;p:selectCheckboxMenu label="Flag" onchange="PF('widgetTable').filter()" panelStyle="width:125px" scrollHeight="150"&gt;
                  &lt;f:selectItem itemLabel="Business" itemValue="B"/&gt;
                  &lt;f:selectItem itemLabel="Reconciliation" itemValue="R" /&gt;
                  &lt;f:selectItem itemLabel="Technical" itemValue="T" /&gt;
                  &lt;f:selectItem itemLabel="Warning" itemValue="W" /&gt;
               &lt;/p:selectCheckboxMenu&gt;
          &lt;/f:facet&gt;
          &lt;h:outputText value="#{hbel.logFlag}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterBy="#{hbel.logFields}" headerText="LogFields" filterMatchMode="contains"&gt;
          &lt;h:outputText value="#{hbel.logFields}" /&gt;
      &lt;/p:column&gt;
  
      &lt;p:column filterBy="#{hbel.logReason}" headerText="LogReason" filterMatchMode="contains"&gt;
          &lt;h:outputText value="#{hbel.logReason}" /&gt;
      &lt;/p:column&gt;
  
  &lt;/p:dataTable&gt;
  &lt;/p:outputPanel&gt;
  </code></pre>
  
  <p>Inside homeController.java bean (request scoped)</p>
  
  <pre><code>    // custom filtering
      public void FilterByDate()
      {
          if (startDate != null &amp;&amp;
              endDate != null)
          {
              // new dates (add start and end date to range)
              Calendar c = Calendar.getInstance(); 
              c.setTime(startDate); 
              c.add(Calendar.DATE, -1);
              Date startDateMod = c.getTime();
              c.setTime(endDate); 
              c.add(Calendar.DATE, 1);
              Date endDateMod = c.getTime();
  
              // reset table first
              tableFiltered.clear();
              // add legal items from tableFull
              for (HubToBauExceptionLog hbel : tableFull)
              {
                  if (hbel != null &amp;&amp;
                      hbel.getLogDate().after(startDateMod) &amp;&amp; 
                      hbel.getLogDate().before(endDateMod))
                  {
                      tableFiltered.add(hbel);
                  }
              }
          }
  }
  </code></pre>