gpt4 book ai didi

jsf - Primefaces 数据表重置和重新加载数据

转载 作者:行者123 更新时间:2023-12-04 23:56:27 29 4
gpt4 key购买 nike

我不确定如何重置 primefaces 数据表,然后将数据重新加载到其中。

有任何想法吗?

enter image description here

一旦我点击“老师”或“家庭领导团队”,我就会发送一个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>

这部分涉及选择“House Leadership Team”或“Teacher”,然后将触发 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;
}

这部分是 back bean 的一部分,显示了调用的 ajax 方法以及使用该方法完成的操作。 我不知道在ajax方法中放什么来重置数据库然后将数据加载回数据表。

最佳答案

问题是,尽管您正在调用托管 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>

这里的重要部分是 更新=“学生表”它告诉在完成 ajax 请求后,使用 id studentTable 更新 jsf 组件。

PS1:这里假设你的 selectOneMenu 和 datatable 是同一种形式;如果不是,您应该用正确的路径替换 update="studentTable"。
PS2:我建议您阅读有关 DAO 层的信息,以便您可以从托管 bean 中删除 findStudents 方法。

关于jsf - Primefaces 数据表重置和重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784400/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com