通过JSF中的日期范围过滤数据表

时间:2013-06-27 14:33:11

标签: jsf-2 jdbc primefaces

我正在使用JSF 2.0和primefaces 3.5,我已生成一个数据表,它有一个日期列,我想通过使用日期范围过滤数据表意味着两个日历其中一个将是开始日期和第二个将代表结束日期,我的代码如下;

<p:datatable id="dTable" value="#{bean.list}" var="row">
  <p:column header="Name">
       #{row.names}
  </column>
  <p:column header="Date of Birth">
       #{row.dob}
  </column>
</datatable>

我的java类是

public class stud{
    public String Names;
    public Date dob;

    {getter and setter for Name and dob}
} 

我的bean类是

public class bean{
    private List<stud> list;
    public List<stud> getList() {
    try {
        list = new ArrayList<stud>();
        Connection con = Dbase.connect();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("Select * from students");
        while (rs.next()) {
            stud st = new stud();
            st.setNames(rs.getString(1));
            st.setDob(rs.getDate(2));
            list.add(st);
        }
        return list;
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}

请指导我如何解决这个问题。您的指导将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

更新(根据评论):

<!-- outside from dtable -->
<p:calendar widgetVar="var1" id="dtFrom" value="#{bean.dateFrom}"
    pattern="yyyy-MM-dd" mode="popup" showOn="button">
    <p:ajax event="dateSelect" listener="#{bean.dateFilter}" update="dTable"/>
</p:calendar>

<p:calendar widgetVar="var2" id="dtTo" value="#{bean.dateTo}"
    pattern="yyyy-MM-dd" mode="popup" showOn="button" >
    <p:ajax event="dateSelect" listener="#{bean.dateFilter}" update="dTable"/>
</p:calendar>
<!-- datatable code -->
<!-- keep in your datatable this column according with your location -->    
<p:column id="dateColumn" headerText="Date of Birth" >
  <h:outputText value="#{row.dob}" >
    <f:convertDateTime pattern="yyyy-MM-dd HH:mm" locale="es_PE" timeZone="EST"/>
  </h:outputText>
</p:column>

您需要在managed-bean中添加这些属性:

private Date dateFrom;
private Date dateTo;
//getters and setters
//You shouldn't put logic in your getList(), just return the list. Add your logic
// in dateFilter method and compare dateFrom and dateTo against st.getDob

如果你需要将你的逻辑保存在getList()中,那么从p:calendar中删除监听器并保持日期的比较。