无法过滤以在PrimeFaces DataTable上工作

时间:2013-08-28 19:55:32

标签: jsf primefaces

我正在尝试对我的数据表使用过滤。每当表首次加载时,它看起来像这样:

enter image description here

如果我在用户名的过滤器中输入文本,则表格如下所示:

enter image description here

我希望它只显示dangreen87,因为mike.smith不包含“d”。但它只显示没有用户名。我不确定这种行为是什么?

我有一个像这样的数据表:

<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看起来像这样:

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

    ....

1 个答案:

答案 0 :(得分:4)

绑定到视图范围bean属性的那些JSTL <c:if>标记是罪魁祸首。

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

长话短说,仔细阅读@ViewScoped fails in taghandlersJSTL in JSF2 Facelets... makes sense?简而言之,它会导致在每个HTTP请求上重新创建视图范围bean,从而在整个过滤过程中完全重置bean的状态并排序ajax请求。

自Mojarra 2.1.18以来,此@ViewScoped +标签处理程序问题已得到解决。基本上,你需要升级到至少Mojarra 2.1.18(它目前已经是2.1.25)。然而,这毕竟不是规范的方法。您应该使用rendered的{​​{1}}属性。

<p:column>