0

I am trying to use filtering on my datatable. Whenever the table first loads, it looks like this:

enter image description here

If I enter text into the filter of user name, the table looks like this:

enter image description here

I would expect it to only show dangreen87 since mike.smith does not contain a "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:

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

    ....
user489041
  • 27,916
  • 55
  • 135
  • 204
  • What PrimeFaces version? Have you manually bundled a jQuery JS file? – BalusC Aug 28 '13 at 19:57
  • It is PrimeFaces version 3.5. I have not included a jQuery JS file either. Found out the consequences of that earlier :) – user489041 Aug 28 '13 at 19:59
  • I am not sure if it is related, but I am also having issues with sorting. – user489041 Aug 28 '13 at 20:00
  • Ho, I'm now seeing those `` around ``. What Mojarra version? Why not just ``? – BalusC Aug 28 '13 at 20:02
  • Interesting... I am using Mojarra 2.1.6. You think those tags could be the source of the issue? Ill try changing those to use the rendered attribute of column – user489041 Aug 28 '13 at 20:03
  • Yep, that was it. Didn't even think that those could be the source of the issue. I can write this up as an answer or if you would like to, I can accept it. – user489041 Aug 28 '13 at 20:05

1 Answers1

4

Those JSTL <c:if> tags bound to a view scoped bean property is the culprit.

<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? 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.

This @ViewScoped+taghandler issue is solved since Mojarra 2.1.18. Basically, you'd need to upgrade to at least Mojarra 2.1.18 (it's currently already at 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 id="userNameColumn" filterBy="#{account.userName}" filterMatchMode="contains" rendered="#{not searchManagedBean.isCompany}">
    ...
</p:column>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555