动态更改jsf primefaces中的分页行值

时间:2018-07-17 18:10:22

标签: jsf primefaces datatable paginator

我遇到一种情况,我需要在Datatable中保留rows属性的值,并需要从Managed Bean中更改它。我目前正在使用jsf 3.4.1。

请考虑以下示例代码,

<p:dataTable var="car" value="#{dtPaginatorView.cars}" rows="10"
                     paginator="true"
                     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                     rowsPerPageTemplate="5,10,15">

我希望它像...

 <p:dataTable var="car" value="#{dtPaginatorView.cars}" rows="#{dtPaginatorView.rows}"
                     paginator="true"
                     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                     rowsPerPageTemplate="5,10,15">

,以便我可以将“行”值绑定到Managed bean变量,可以根据逻辑条件对其进行更改。是否还可以跟踪当前选择的“ rowsPerPageTemplate”值。

在此方面的任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据OP的要求,这就是我要做的。我存储每个用户的行,以便应用程序的每个用户可以设置自己的“行”值并将其保留。

XHTML:

<p:dataTable var="row" 
             rowKey="#{row.id}" filterEvent="enter"
             value="#{auditTrailDatatable.values}"
             filteredValue="#{auditTrailDatatable.filteredValues}" 
              paginatorTemplate="#{appmsg['datatable.paginator']}" 
              paginator="true" 
              rows="#{applicationUser.rowsPerPage}" 
              rowsPerPageTemplate="#{appmsg['datatable.rowsperpage']}">

JPA ENTITY(为简洁起见):

public class ApplicationUser  {

   /** How many rows per page to display in datatables */
   @Column(name = "ROWS_PER_PAGE", length = 19, nullable = false)
   @Min(value = 5)
   private Integer rowsPerPage;

   public final Integer getRowsPerPage() {
      return rowsPerPage;
   }

   public final void setRowsPerPage(final Integer rowsPerPage) {
      this.rowsPerPage = rowsPerPage;
   }

}

UI提供者:

@SessionScoped
public class ApplicationUserProvider implements Serializable {

   /** Logger for this class */
   private static final Logger LOG = LoggerFactory.getLogger(ApplicationUserProvider.class);
   private static final long serialVersionUID = 1L;

   /** Servlet request for this invocation */
   @Inject
   HttpServletRequest request;

   /** The DAO used to update the local application user entity */
   @Inject
   ApplicationUserRepository applicationUserRepository;

   /** State Variable: Cached copy of the current ApplicationUser record */
   private ApplicationUser applicationUser;

   /**
    * Called on user session initialization. Ensures that we have an Application
    * user record for the current user.
    */
   @PostConstruct
   public void init() {
      try {
         final Principal currentUser = request.getUserPrincipal();
         final String userName = StringUtils.upperCase(currentUser.getName());

         // attempt to locate our ApplicationUser record for the currently
         // authenticated User
         applicationUser = applicationUserRepository.find(userName);
         if (applicationUser == null) {
            applicationUser = ApplicationUser.createApplicationUser(userName);
            applicationUser.setLastLoginTime(DateTime.now().toDate());
            applicationUserRepository.persist(applicationUser);
         } else {
            applicationUser.setLastLoginTime(DateTime.now().toDate());
            applicationUser = applicationUserRepository.merge(applicationUser);
         }

         LOG.info("User '{}' logged in.", userName);
      } catch (final Exception ex) {
         LOG.error("Unexpected Exception with UserPrincipal: {}", ExceptionUtils.getRootCauseMessage(ex), ex);
         throw ex;
      }
   }

   /**
    * The currently logged in {@link ApplicationUser} being provided for
    * injection in other classes and exposed on the UI with @Named.
    *
    * @return the {@link ApplicationUser} object representing the currently
    *         logged in user.
    */
   @Produces
   @Named
   @LoggedIn
   public ApplicationUser getApplicationUser() {
      return applicationUser;
   }

}