WiXXeY
WiXXeY

Reputation: 1001

Filter Datatable through date-range in JSF

I am using JSF 2.0 and primefaces 3.5, i have generated a datatable, it has a date column in it, i want to have filter the datatable by using a date range means two calendar one of which will be starting date and second will represent the ending date, my code is given below;

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

My java classes are

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

    {getter and setter for Name and dob}
} 

my bean class is

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

kindly guide me how can i solve this issue. your guidance will be highly appreciated

Upvotes: 0

Views: 2204

Answers (1)

danRod
danRod

Reputation: 113

Updated (according with comments):

<!-- 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>

You need to add those properties in your 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

If you need to keep your logic in getList() then delete the listeners from p:calendar and keep the date's comparison.

Upvotes: 1

Related Questions