Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces datatable filter doesn't work

I must do something fundamentally wrong, I stripped down the code to the bare minimum with a data table and enabling one column filter and a globe filter.

The funny thing is that the example code from Primefaces works. The only difference to my code should be that it gathers data from a DB rather than generating it in the bean.

I have no more clues why my example doesn't do anything when I type something in the filter would be appreciate any ideas here.

My xhtml:

    <!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="layout.xhtml">

    <ui:define name="title">All Projects</ui:define>    

    <ui:define name="content">


        <p:dataTable var="project" value="#{projectController.allProjects}" widgetVar="projectTable" filteredValue="#{projectController.filteredProjects}">

            <f:facet name="header">
                <p:outputPanel>  
                    <h:outputText value="Search all fields:" />  
                    <p:inputText id="globalFilter" onkeyup="PF('projectTable').filter()" style="width:150px" />  
                </p:outputPanel>
            </f:facet>

            <p:column headerText="Name" filterBy="#{project.name}">
                <h:outputText value="#{project.name}" />
            </p:column>

            <p:column headerText="Priority">
                <h:outputText value="#{project.priority}" />
            </p:column>

            <p:column headerText="Exit">
                <h:outputText value="#{project.exitCriteria}" />
            </p:column>

        </p:dataTable>      

    </ui:define>
</ui:composition>

My Bean:

    package com.apa.projectd.common;
    import java.io.Serializable;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;
    import javax.inject.Inject;
    import com.habony.common.Loggable;
    import com.habony.projectd.ejbs.ProjectEJB;
    import com.habony.projectd.enteties.Project;

    @ManagedBean(name="projectController")
    @SessionScoped
    @Loggable
    public class ProjectController implements Serializable{

private static final long serialVersionUID = 8345760187637787728L;

@Inject
private ProjectEJB projectEJB;

private List<Project> filteredProjects;
private List<Project> allProjects;

@PostConstruct
public void loadAllProjects(){
    allProjects =  projectEJB.getAllProjects();
}

//
// Getters and Setters
//
public List<Project> getFilteredProjects() {
    return filteredProjects;
}

public void setFilteredProjects(List<Project> filteredProjects) {
    this.filteredProjects = filteredProjects;
}

public void setAllProjects(List<Project> allProjects) {
    this.allProjects = allProjects;
}

public List<Project> getAllProjects(){
    return allProjects;
}

}

like image 817
Ferenc Avatar asked Jun 18 '14 18:06

Ferenc


1 Answers

The filters features of p:dataTable need to be wrapped in <h:form> tags for work fine. The code xhtml modified would:

<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="layout.xhtml">

    <ui:define name="title">All Projects</ui:define>    

    <ui:define name="content">
       <h:form>
           <p:dataTable var="project" value="#{projectController.allProjects}" widgetVar="projectTable" filteredValue="#{projectController.filteredProjects}">

              <f:facet name="header">
                <p:outputPanel>  
                    <h:outputText value="Search all fields:" />  
                    <p:inputText id="globalFilter" onkeyup="PF('projectTable').filter()" style="width:150px" />  
                 </p:outputPanel>
              </f:facet>

              <p:column headerText="Name" filterBy="#{project.name}">
                 <h:outputText value="#{project.name}" />
              </p:column>

              <p:column headerText="Priority">
                 <h:outputText value="#{project.priority}" />
              </p:column>

             <p:column headerText="Exit">
                 <h:outputText value="#{project.exitCriteria}" />
             </p:column>

          </p:dataTable>      
      </h:form>
   </ui:define>
</ui:composition>
like image 192
Deoxyseia Avatar answered Nov 23 '22 19:11

Deoxyseia