Search code examples
jsfprimefacesprimefaces-datatable

Datatable filter not working


I am using Primefaces for creating a filterable table.

<p:dataTable id="tasksTable" value="#{taskView.currentTasks}" var="task" rowIndexVar="index" filteredValue="#{taskView.filteredTasks}">
                <p:column headerText="Name">
                    <h:outputText value="#{task.name}"></h:outputText>
                </p:column>
                <p:column headerText="Type" filterBy="type"   
                            filterMatchMode="containing">
                    <h:outputText value="#{task.type}"></h:outputText>
                </p:column>
                <p:column headerText="Date Started">
                    <h:outputText value="#{task.startTime}"></h:outputText>
                </p:column>
                <p:column headerText="Details">
                    <h:outputText value="#{task.details}"></h:outputText>
                </p:column>
</p:dataTable>

And this is my view:

@Component("taskView")
@Scope("session")
public class TaskView{
private List<TaskDTO> currentTasks;

private List<TaskDTO> filteredTasks;
//getters and setters

}

The problem is that the filtering is not working. Apparently it makes an ajax call which receives no answer an just keeps waiting. On the backend the getter for the currentTasks list is called many times.

It seems to me that Primefaces makes an ajax call and for some reason on the server side it calls the getter for the table results a lot of times(maybe hoping for some different result?). I do not know why that happens.

What am I doing here wrong?


Solution

  • I have found the solution. The official documentation for Primefaces provides a non-working example.

    So this is WRONG. The correct approach is here.

    More exactly, the filterBy field should not contain only the field of the object upon which it is filtered, but the whole field, like #{myVar.field}. So, in my case, this would mean:

    <p:column headerText="Type" filterBy="#{task.type}"   
                            filterMatchMode="contains">
                    <h:outputText value="#{task.type}"></h:outputText>
                </p:column>
    

    I hope other people will find this useful.