Intento refrescar el componente dataTabale de primefaces cuando elimino un registro de la tabla, sin embargo, cuando hago click en el boton de eliminar, a veces la tabla se actualiza y a veces no, entonces, tengo que manualmente refrescar la pagina completa para ver el cambio en el componente dataTable.
He notado algo curioso, cada vez que vuelvo a subir la aplicación al servidor(wildfly 8.2) y empiezo por el 4 registro(Hulk), el componente dataTable se actualiza inmediatamente, si empiezo por el primner o ultimo registro, tengo que presionar mcuhas veces el boton Delete, hasta que se refresque el componente.
Registro que se desea eliminar.
<h:form id="form">
<p:dataTable id="studentsDataTable"
value="#{studentsController.students}"
var="student"
rowKey="#{student.username}"
selection="#{studentsController.studentSelected}"
selectionMode="single">
<!--Header-->
<f:facet name="header">
Spanish Academy : Students' List
<p:button value="Add Student" outcome="new_student" />
</f:facet>
<p:column headerText="Username">
<h:outputText value="#{student.username}" />
</p:column>
<p:column headerText="Firstname">
<h:outputText value="#{student.firstname}" />
</p:column>
<p:column headerText="Lastname">
<h:outputText value="#{student.lastname}" />
</p:column>
<p:column headerText="Edit" style="width:50px">
</p:column>
<!--Footer-->
<f:facet name="footer">
<p:commandButton value="Delete" actionListener="#{studentsController.deleteStudent}" icon="ui-icon-close" process="@form" update=":form:studentsDataTable"/>
<p:spacer height="5px;"/>
</f:facet>
</p:dataTable>
</h:form>
StudentController(ManagedBean) - Capa Web/Presentacion
Named
@ApplicationScoped
public class StudentsController implements Serializable {
@EJB
private StudentService studentService;
private List<Student> students;
private Student studentSelected;
@PostConstruct
public void init() {
students = studentService.getAllStudents();
studentSelected = new Student();
}
public void deleteStudent() {
studentService.deleteStudent(this.studentSelected);
this.studentSelected = null;
//List update
this.init();
}
public StudentService getStudentService() { return studentService; }
public void setStudentService(StudentService studentService) {
this.studentService = studentService; }
}
EJB(servicios) - Capa de Negocios
@Stateless
public class StudentServiceImpl implements StudentService {
@Inject
private StudentDao studentDao;
...
@Override
public void deleteStudent(Student student) {
studentDao.remove(student); }
}
EJB(Dao) - Capa de Acceso de Datos
@Stateless
public class StudentDaoImpl implements StudentDao {
...
@Override
public void remove(Student student) {
for (Student studentInDB: StudentsDB.students) {
if(student.getUsername().equals(studentInDB.getUsername())) {
StudentsDB.students.remove(studentInDB);
}
}
}
}
Base de datos estatica
public class StudentsDB {
public static List<Student> students;
static {
Student student;
students = new ArrayList<>();
student = new Student("Snake", "John", "Smith");
students.add(student);
student = new Student("Cowboy", "George", "Lopez");
students.add(student);
student = new Student("Batman", "Luis", "Williams");
students.add(student);
student = new Student("Hulk", "Miguel", "Woods");
students.add(student);
student = new Student("Prime", "Jeniffer", "Fierro");
students.add(student);
}
}
Modelo Student(Java Bean Simple)
public class Student {
private String username;
private String firstname;
private String lastname;
private Date birthDate;
private String country;
private String city;
private String street;
private Integer zipCode;
private Integer phoneNumber;
private String email;
private boolean acceptTerms;
public Student() { }
public Student(String username, String firstname, String lastname) {
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
}
...
<!-- getters & setters -->
}
He intentado de muchas formas, solventarlo, sin embargo, no he podido hasta ahora, he leido, otros problemas similares al mio, sin embargo, no me han funcionado.
Problema al actualizar componente datatable de primefaces
Incluso habia hecho la pregunta en stackoverflow en ingles y me han dado algunos consejos, pero no me han funcionado.
Alguna idea?
Gracias por adelantado.