我不确定如何重置primefaces数据表,然后将数据重新加载到其中。
有什么想法吗?
当我点击“老师”或“众议院领导团队”时,我发送一个ajax调用,然后我想完全重置数据表,然后重新加载数据。
以下是与两个小组有关的部分:
#{msgs.typeOfLeaderPunishment}
<f:ajax event="valueChange" listener="#{detentionForm.updateData()}" execute="typeOfLeader" render="typeOfPunishment studentTable">
<p:selectOneMenu id="typeOfLeader" value="#{detentionForm.typeOfLeaderSelectedID}" style="width:400px" panelStyle="width:150px" effect="fade">
<f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" />
</p:selectOneMenu>
</f:ajax>
这部分涉及选择“众议院领导小组”或“老师”,然后触发更新数据表的ajax调用。
<p:dataTable id="studentTable" var="student" value="#{detentionForm.students}" paginator="true" rows="10" selection="#{detentionForm.studentSelected}" filterDelay="200" filteredValue="#{detentionForm.filteredStudents}" binding="#{detentionForm.studentTable}">
<f:facet name="header">
Students:
</f:facet>
<p:column id="prefName" headerText="Preferred Name" sortBy="=#{student.prefName}" filterBy="#{student.prefName}" filterMatchMode="contains">
#{student.prefName}
</p:column>
<p:column id="lastName" headerText="Last Name" sortBy="#{student.lastName}" filterBy="#{student.lastName}" filterMatchMode="contains">
#{student.lastName}
</p:column>
<p:column id="house" headerText="House" sortBy="#{student.house}">
#{student.house}
</p:column>
<p:column id="code" headerText="Student Code" sortBy="#{student.studentCode}" >
#{student.studentCode}
</p:column>
<p:column id="gender" headerText="Gender" sortBy="#{student.gender}">
#{student.gender}
</p:column>
<p:column id="formName" headerText="Form" sortBy="#{student.form}">
#{student.form}
</p:column>
<p:column id="yearLevel" headerText="Year Level" sortBy="#{student.yearLevel}">
#{student.yearLevel}
</p:column>
</p:dataTable>
这部分是数据表。
//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 -> 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. >>>>>>>>>>>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;
}
这部分是后端bean的一部分,显示了调用的ajax方法以及使用该方法完成的操作。 我不知道将什么放入ajax方法来重置数据库,然后将数据加载回数据表。
答案 0 :(得分:7)
问题在于,虽然您正在调用托管bean方法,但您并没有告诉jsf数据表“更新”其内容。
这应该有帮助
<p:selectOneMenu id="typeOfLeader" value="#{detentionForm.typeOfLeaderSelectedID}">
<f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" />
<p:ajax event="change" update="studentTable" listener="#{detentionForm.updateData()}" />
</p:selectOneMenu>
这里的重要部分是 update =“studentTable”,它告诉我们在完成ajax请求后,用id studentTable更新jsf组件。
PS1:这假设您的selectOneMenu和数据表采用相同的形式;如果不是,你应该用正确的路径替换update =“studentTable”。 PS2:我建议您阅读DAO图层,以便从托管bean中删除findStudents方法。
答案 1 :(得分:0)
无论何时使用ajax更改选择菜单,都要调用updateData方法。 尝试重建您在updateData方法上显示的“学生”列表。
答案 2 :(得分:0)
我对primefaces 4数据表有同样的问题。
感谢Mehul Kaklotar我在创建/编辑/删除数据库并刷新数据表后重建/调用getList()。
注意:在update=":yourform:yourdatatable"
<p:commandbutton>
答案 3 :(得分:0)
通过使用PrimeFaces remoteCommand调用dataTable的AJAX / jQuery filter()函数,我获得了成功:
<p:selectOneMenu id="typeOfLeader" value="#{detentionForm.typeOfLeaderSelectedID}"
style="width:400px" panelStyle="width:150px" effect="fade"
onchange="typeOfLeaderUpdate();">
<f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" />
<p:ajax event="change" oncomplete="typeOfLeaderUpdate();"/>
<p:remoteCommand id="typeOfLeaderRemoteCommandId" name="typeOfLeaderUpdate"
actionListener="#{detentionForm.updateData()}"
update="studentTable"
oncomplete="student.filter();"/>