简体   繁体   English

无法过滤以在PrimeFaces DataTable上工作

[英]Can't get filtering to work on PrimeFaces DataTable

I am trying to use filtering on my datatable. 我正在尝试对数据表使用过滤。 Whenever the table first loads, it looks like this: 每当表第一次加载时,它都将如下所示:

在此处输入图片说明

If I enter text into the filter of user name, the table looks like this: 如果我在用户名过滤器中输入文本,则该表如下所示:

在此处输入图片说明

I would expect it to only show dangreen87 since mike.smith does not contain a "d". 我希望它只显示dangreen87,因为mike.smith不包含“ d”。 It however just displays no user names. 但是,它仅不显示用户名。 Im not sure what this behaviour is? 我不确定这种行为是什么?

I have a datatable like so: 我有一个像这样的数据表:

<h:body>
    <ui:composition>
        <h:panelGroup layout="block" styleClass="messagesPanel" rendered="#{socialAdvertiserManagedBean.displaySearch}" >
            <p:dataTable 
                resizableColumns="true"
                var="account" 
                value="#{searchManagedBean.accountsToDisplay}" 
                scrollable="true"
                paginator="true"
                rows="10"
                rowKey="#{account.id_value}"
                emptyMessage="No accounts found for the given criteria"
                widgetVar="searchTable"
                filteredValue="#{searchManagedBean.filteredAccounts}">
                <f:facet name="header">
                    #{searchManagedBean.isCompany ? 'Company' : 'Social Advertisers'}
                </f:facet>
                <p:column headerText="Image">
                    <p:graphicImage value="/dbimages/#{accountManagedBean.getImageId(account)}" width="25" height="25"/>
                </p:column>

                <c:if test="#{searchManagedBean.isCompany}" >
                    <p:column headerText="Company Name">
                        <h:outputLabel value="#{accountManagedBean.getCompany(account).name}" />
                    </p:column>
                </c:if>

                <c:if test="#{not searchManagedBean.isCompany}" >

                    <p:column id="userNameColumn" filterBy="#{account.userName}" filterMatchMode="contains">
                        <f:facet name="header">
                            <h:outputLabel value="User Name"/>
                        </f:facet>
                        <h:outputLabel value="#{account.userName}" />
                    </p:column>

                </c:if>

            </p:dataTable>

My Backing bean looks like so: 我的Backing bean看起来像这样:

@ManagedBean
@ViewScoped
public class SearchManagedBean implements Serializable
{

    private boolean isCompany;
    private Account selectedAccount;


    @EJB
    private AccountDao accountDao;

    @EJB
    private SocialAdvertiserDao socialAdvertiserDao;

    @EJB
    private CompanyDao companyDao;

    private List<Account> filteredAccounts;

    @PostConstruct
    public void init()
    {
        isCompany = true;
    }

    public List<Account> getAccountsToDisplay()
    {
        List temp;
        if(isCompany)
        {
            temp = companyDao.findAll();
        }
        else
        {
            temp = socialAdvertiserDao.findAll();
        }
        return temp;
    }

    public List<Account> getFilteredAccounts() {
        return filteredAccounts;
    }

    public void setFilteredAccounts(List<Account> filteredAccounts) {
        this.filteredAccounts = filteredAccounts;
    }


    public boolean getIsCompany() {
        return isCompany;
    }

    public void setIsCompany(boolean isCompany) {
        this.isCompany = isCompany;
    }

    ....

Those JSTL <c:if> tags bound to a view scoped bean property is the culprit. 绑定到视图作用域bean属性的那些JSTL <c:if>标记是罪魁祸首。

<c:if test="#{not searchManagedBean.isCompany}" >
    <p:column id="userNameColumn" filterBy="#{account.userName}" filterMatchMode="contains">
        ...
    </p:column>
</c:if>

Long story short, carefully read @ViewScoped fails in taghandlers and JSTL in JSF2 Facelets... makes sense? 长话短说,仔细阅读@ViewScoped 在JSF2 Facelets 中的标记处理程序JSTL中 失败了 吗? In a nutshell, it causes the view scoped bean to be recreated on every single HTTP request and therefore a complete reset of the bean's state across the filtering and sorting ajax requests. 简而言之,它会导致在每个HTTP请求上重新创建视图作用域的bean,从而在过滤和排序ajax请求中完全重置bean的状态。

This @ViewScoped +taghandler issue is solved since Mojarra 2.1.18. 自Mojarra 2.1.18起,此@ViewScoped + taghandler问题已解决。 Basically, you'd need to upgrade to at least Mojarra 2.1.18 (it's currently already at 2.1.25). 基本上,您需要至少升级到Mojarra 2.1.18(当前已经是2.1.25)。 However, this is after all not the canonical approach. 但是,这毕竟不是规范方法。 You should just use the rendered attribute of <p:column> for that. 您应该只使用<p:column>rendered属性。

<p:column id="userNameColumn" filterBy="#{account.userName}" filterMatchMode="contains" rendered="#{not searchManagedBean.isCompany}">
    ...
</p:column>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM