I had a datatable built with primefaces 5.1 that was working perfectly until I added the filtering options. I've been through the forum and I've tried the suggestions but none of them have worked for me so far. This was the closest suggestion that I could find:
Each row of my datatable has an edit and remove button. Before I add the search filter code, I can click on edit and a pre-populated form will show up with the correct details, also delete works. Once I add the filtering code to the primefaces datatable, I get a blank form once I click on edit. The delete button also ceases working.
The datatable:
<p:growl id="messages" />
<h2>Company</h2>
<h:form id="form1">
<p:dataTable value="#{companyController.companies}" var="company" id="companyTable" widgetVar="filteredcompanyTable"
emptyMessage="No records found with given criteria" filteredValue="#{companyController.filteredCompanies}">
<p:column headerText="Name" sortBy="#{company.company_name}" filterBy="#{company.company_name}" filterMatchMode="contains">
#{company.company_name}
</p:column>
<p:column headerText="Description">
#{company.company_description}
</p:column>
<p:column headerText="Enabled">
#{companyController.checkEnabled(company.enabled)}
</p:column>
<p:column headerText="Updated" sortBy="#{company.date_modified}">
#{companyController.formatDate(company.date_modified)}
</p:column>
<p:column headerText="Registered" sortBy="#{company.date_registered}">
#{companyController.formatDate(company.date_registered)}
</p:column>
<p:column headerText="Options">
<h:form>
<p:commandButton value="edit" action="#{companyController.setCompany(company)}" update=":form1:companyForm"
oncomplete="PF('companyDialog').show(); " resetValues="true" />
<p:commandButton value="remove" action="#{companyController.remove(company)}" update=":form1:companyTable, :messages">
<p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
</p:commandButton>
</h:form>
</p:column>
</p:dataTable>
<h:form>
<p:commandButton value="add company" action="#{companyController.clear()}" update=":form1:companyForm"
oncomplete="PF('companyDialog').show()" resetValues="true" />
</h:form>
<p:dialog widgetVar="companyDialog" closeOnEscape="true" modal="true" header="company">
<h:form id="companyForm">
<p:focus />
<p:panelGrid columns="2">
name:
<p:inputText value="#{companyController.company.company_name}" />
description:
<p:inputText value="#{companyController.company.company_description}" />
enabled:
<h:selectBooleanCheckbox value="#{companyController.company.enabled}" />
</p:panelGrid>
<p:commandButton value="save" action="#{companyController.save()}" update="companyForm, :form1:companyTable, :messages"
oncomplete="handleDialogSubmit(args, 'companyDialog'); PF('filteredcompanyTable').filter();" />
</h:form>
</p:dialog>
</h:form>
<h:form>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade" closeOnEscape="true">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:form>
<script type="text/javascript">
function handleDialogSubmit(args, dialogName) {
if(!args.validationFailed) {
PF(dialogName).hide();
}
}
</script>
Below is the code for the underlying view:
public class CompanyController implements Serializable {
private static final long serialVersionUID = 1L;
@ManagedProperty("#{companyService}")
private CompanyService companyService;
private List<Company> companies;
private List<Company> filteredCompanies;
private Company company = new Company();
@PostConstruct
public void loadCompanies() {
companies = companyService.fetchAll();
}
public void save() {
companyService.save(company);
clear();
loadCompanies();
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Company saved!", null));
}
public void remove(Company company) {
companyService.delete(company);
loadCompanies();
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Company de-registered!", null));
}
public void clear() {
company = new Company();
}
public String formatDate(Date date) {
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
return formatter.format(date);
}
public String checkEnabled(boolean status) {
if (status==true) {return "YES";}
return "NO";
}
}
Kindly assist