title: Primefaces datatable Reset and Reload data
body: <p>I am not sure how to reset the primefaces datatable and then reload data into it.</p>
  
  <p>Any ideas?</p>
  
  <p><img src="https://i.stack.imgur.com/of2O0.png" alt="enter image description here"></p>
  
  <p>As soon as I click "Teacher" or "House Leadership Team", i send a ajax call and then I would like to completely reset the datatable and then reload data.</p>
  
  <p>Here are the parts relating to the two panels:</p>
  
  <pre><code>#{msgs.typeOfLeaderPunishment}
  &lt;f:ajax event="valueChange" listener="#{detentionForm.updateData()}" execute="typeOfLeader" render="typeOfPunishment studentTable"&gt;
      &lt;p:selectOneMenu id="typeOfLeader" value="#{detentionForm.typeOfLeaderSelectedID}" style="width:400px" panelStyle="width:150px" effect="fade"&gt;
         &lt;f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" /&gt;
      &lt;/p:selectOneMenu&gt;
  &lt;/f:ajax&gt;
  </code></pre>
  
  <p>This part relates to choosing "House Leadership Team" or "Teacher" which will then trigger a ajax call updating the datatable.</p>
  
  <pre><code>&lt;p:dataTable id="studentTable" var="student" value="#{detentionForm.students}" paginator="true" rows="10" selection="#{detentionForm.studentSelected}" filterDelay="200" filteredValue="#{detentionForm.filteredStudents}" binding="#{detentionForm.studentTable}"&gt;  
        &lt;f:facet name="header"&gt;  
             Students:   
        &lt;/f:facet&gt;  
                      &lt;p:column id="prefName" headerText="Preferred Name" sortBy="=#{student.prefName}" filterBy="#{student.prefName}" filterMatchMode="contains"&gt;  
                          #{student.prefName} 
  
                      &lt;/p:column&gt;  
                      &lt;p:column id="lastName" headerText="Last Name" sortBy="#{student.lastName}" filterBy="#{student.lastName}" filterMatchMode="contains"&gt;
                          #{student.lastName}
                      &lt;/p:column&gt;
                      &lt;p:column id="house" headerText="House" sortBy="#{student.house}"&gt;
                          #{student.house}
                      &lt;/p:column&gt;
                      &lt;p:column id="code" headerText="Student Code" sortBy="#{student.studentCode}" &gt;
                          #{student.studentCode}
                      &lt;/p:column&gt;
                      &lt;p:column id="gender" headerText="Gender" sortBy="#{student.gender}"&gt;
                          #{student.gender}
                      &lt;/p:column&gt;
                      &lt;p:column id="formName" headerText="Form" sortBy="#{student.form}"&gt;
                          #{student.form}
                      &lt;/p:column&gt;
                      &lt;p:column id="yearLevel" headerText="Year Level" sortBy="#{student.yearLevel}"&gt;
                          #{student.yearLevel}
                      &lt;/p:column&gt;
                  &lt;/p:dataTable&gt;  
  </code></pre>
  
  <p>This part is the datatable.</p>
  
  <pre><code>//ajax method called when user clicks on "House Leadership Team" or "Teacher" int the selectOneMenu tag
      public void updateData(){
          this.findStudents();
  
  
      }
  
  
  
      //populates the student list with student codes depending on what ledership was chosen (eg. HouseLeader -&gt; import House students only)
      private void findStudents() {
          this.students.removeAll(this.students);
          int houseID =  this.findTeacherHouseID();
          PreparedStatement ps;
          Connection con;
          String sqlStudentsTeacher = "SELECT a.LastName, a.PrefName, a.Code, a.Gender, b.FormName, b.YearLevel, c.HouseName FROM detentioncentredb.tbl_students a, detentioncentredb.tbl_forms b, detentioncentredb.tbl_houses c WHERE a.Form = b.Id AND a.House = c.Id";
          String sqlStudentsHouseLeader = "SELECT a.LastName, a.PrefName, a.Code, a.Gender, b.FormName, b.YearLevel, c.HouseName FROM detentioncentredb.tbl_students a, detentioncentredb.tbl_forms b, detentioncentredb.tbl_houses c WHERE a.Form = b.Id AND a.House = c.Id AND a.House = ?";
          ResultSet rs;
          try {
              con = ds.getConnection();
              if(this.typeOfLeaderSelectedID == 1){
                  ps = con.prepareStatement(sqlStudentsTeacher);
              }else{ //typeOfLeaderSelectedID must equal 2. &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Make sure that makeDetention xhtml page has a specific filter and there must be a validator when the user selects a leadership ty.pe
                  ps = con.prepareStatement(sqlStudentsHouseLeader);
                  ps.setInt(1,houseID);
              }
              rs = ps.executeQuery();
              //Puts a row into a Student object and chucks into the student arraylist
              while(rs.next()){
                  Student s = new Student();
                  s.setForm(rs.getString("FormName"));
                  s.setGender(rs.getString("Gender"));
                  s.setHouse(rs.getString("HouseName"));
                  s.setLastName(rs.getString("LastName"));
                  s.setPrefName(rs.getString("PrefName"));
                  s.setStudentCode(rs.getString("Code"));
                  s.setYearLevel(rs.getString("YearLevel"));
                  this.students.add(s);
              }
              rs.close();
              ps.close();
              con.close();
          } catch (SQLException ex) {
              Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  
  
      private int findTeacherHouseID(){
          PreparedStatement ps;
          Connection con;
          String sqlTeacherHouseID = "SELECT House FROM detentioncentredb.tbl_teachers WHERE RegNumber = ?";
          ResultSet rs;
          int id = 0;
          try {
              con = ds.getConnection();
              ps = con.prepareStatement(sqlTeacherHouseID);
              ps.setInt(1, this.details.getUserName());
              rs = ps.executeQuery();
              while(rs.next()){
                  id = rs.getInt("House");
              }
              rs.close();
              ps.close();
              con.close();
          } catch (SQLException ex) {
              Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          return id;
      }
  </code></pre>
  
  <p>This part is apart of the back bean showing the ajax method called and what is done with that method. <strong>I don't know what to put into the ajax method to reset the database and then load data back into the datatable.</strong></p>