【问题标题】:Dynamically changing pagination rows value in jsf primefacesjsf primefaces中动态更改分页行值
【发布时间】:2018-12-25 12:53:27
【问题描述】:

我有一种情况,我需要在 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">

这样我就可以将“rows”值绑定到托管 bean 变量,我可以根据逻辑条件对其进行更改。是否也可以跟踪当前选择的“rowsPerPageTemplate”值。

非常感谢任何帮助。

【问题讨论】:

  • 您在 rows="dtPaginatorView.rows" 周围缺少大括号,它应该是 rows="#{dtPaginatorView.rows}" 。我在我所有的应用程序中都这样做并坚持它工作正常的行。
  • 对不起,我在打字时错过了。当我使用大括号时,值会持续存在,但分页不起作用。你能用 XHTML 文件和相关的托管 bean 分享你的例子吗

标签: jsf primefaces datatable paginator


【解决方案1】:

根据 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;
   }

}

用户界面提供者:

@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;
   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2012-05-17
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多