DataTable clearFilter() not working properly - jsf-2

I have a complicate JSF that contain dataTable with filter in each one of the columns.
In order to make sure that the generate button will fetch all the data first I need to clear all the filters when the user press the button.
I try to do use onclick but then I couldn’t see the blockUI I also try on complete (ajax) but again it was not working properly with all the other items (blockUI ,message).
I decided to try to clear the filters via server side but only dataTable.reset() is working.
I have no more ideas how to clean the filters ???
Is this API working ?
Appreciate your help
Thanks
<h:panelGrid columns="1" style="width: 100%">
<p:panel id="vt-panel">
<h:panelGrid columns="5" cellpadding="2" >
<h:outputText value="Start Date" />
<p:calendar id="vt-start" value="#{vtRepBean.startDate}" binding="#{startDateComponent}" maxlength="9" size="9" pattern="dd-MMM-yy" title="dd-MMM-yy" required="true" maxdate="#{vtRepBean.endDate}">
<p:ajax event="dateSelect" listener="#{vtRepBean.handleStartDateSelect}" update=":mainForm:vt-end"/>
</p:calendar>
<h:outputText value="End Date" />
<p:calendar id="vt-end" value="#{vtRepBean.endDate}" maxlength="9" size="9" pattern="dd-MMM-yy" title="dd-MMM-yy" required="true" mindate="#{vtRepBean.startDate}">
<p:ajax event="dateSelect" listener="#{vtRepBean.handleEndDateSelect}" update=":mainForm:vt-start"/>
</p:calendar>
<p:commandButton
id="genVtBtn"
value="Generate"
actionListener="#{vtRepBean.handleVTGenerateButton}"
update=":mainForm:vt-panel,:mainForm:vt-panel-table">
</p:commandButton>
</h:panelGrid>
</p:panel>
</h:panelGrid>
<p:growl id="vt_message" showDetail="true" autoUpdate="true"/>
<h:panelGroup id="vt-panel-table">
<p:dataTable id="vtDataTable"
widgetVar="vtWidget"
var="reportObject"
value="#{vtRepBean.reportObjectsList}"
rendered="#{vtRepBean.renderVTReport}"
filteredValue="#{vtRepBean.filteredVTList}"
paginator="true"
paginatorPosition="bottom"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="50,100,200"
rows="50"
style="width: 100%">
<p:columnGroup type="header">
<p:row>
<p:column colspan="5" headerText="VT request"/>
<p:column colspan="1" headerText="Dis" />
</p:row>
<p:row>
<p:column headerText="CREATE DATE" sortBy="#{reportObject.log.createDate}" filterBy="#{reportObject.log.createDate}" filterMatchMode="contains"/>
<p:column headerText="IP" sortBy="#{reportObject.log.ip}" filterBy="#{reportObject.log.ip}" filterMatchMode="contains"/>
</p:row>
</p:columnGroup>
<p:column >
<h:outputText value="#{reportObject.log.createDate}"/>
</p:column>
<p:column >
<h:outputText value="#{reportObject.log.ip}"/>
</p:column>
</p:dataTable>
<p:commandLink rendered="#{vtRepBean.renderVTReport}" ajax="false" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)">
<p:graphicImage value="resources/images/excel.png" title="excel" style="border-color: white"/>
<p:dataExporter id="xlsReport"
type="xls"
target="vtDataTable"
fileName="VTReport"
postProcessor="#{vtRepBean.postProcessXLS}"/>
</p:commandLink>
</h:panelGroup>
<p:blockUI widgetVar="blockVTPanel" trigger="genvtBtn" block="vt-panel">
<div class="disable-scroll">
<p:graphicImage value="resources/images/ajax-loader.gif"/>
</div>
</p:blockUI>
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("mainForm:vtDecomDataTable");
if (!dataTable.getFilters().isEmpty()) {
logger.info("dataTable.getFilters().isEmpty() :" + dataTable.getFilters().isEmpty());
dataTable.getFilters().clear();// not working
dataTable.getFilteredValue().clear();// not working
dataTable.setFilteredValue(null);// not working
dataTable.setFilters(null);// not working
dataTable.setFilterMetadata(null);// not working
dataTable.reset();// working
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.update("mainForm:vtDecomDataTable");
}

To clear all the inputs of the filters you can do it by javascript:
<p:commandButton onclick="PF('vtWidget').clearFilters()" />
vtWidget is the widgetVar of the datatable.
Basically clearFilters() will clear the fields for you and call filter(), and the filter function would update your datatable, which in turns will empty the filtered list.
Note: This would work only if the filters were inputText. If you have custom components then you should implement your own clear based on the components that you have.
Sometimes if you have custom components, you need to empty the filtered list manually, as you did in the comments!

This is how i solved my problem.
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.execute("PF('widget_orderDataTable').clearFilters()");
Hope its help.

For clearing custom filters you can use primefaces resetInput, along with clearFilters() discussed in other answers, and a custom actionListener method. See code snippets below:
<p:dataTable id="dataTable" widgetVar="dataTable"
value="#{bean.listOfObjects}" var="object">
<p:commandButton value="Clear All Filters"
onclick="PF('dataTable').clearFilters()"
actionListener="#{controller.clearAllFilters}"
update="dataTable">
<p:resetInput target="dataTable" />
</p:commandButton>
Controller.java
public void clearAllFilters() {
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:dataTable");
if (!dataTable.getFilters().isEmpty()) {
dataTable.reset();
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.update("form:dataTable");
}
}
I hope this helps anyone looking to clear custom filters.

For Primefaces 7 and above, use the following:
public void clearAllFilters() {
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:dataTable");
if (!dataTable.getFilters().isEmpty()) {
dataTable.reset();
PrimeFaces.current().ajax().update("form:dataTable");
}
}

Related

Pass dataTable value of the row to new JSF using managed bean or?

How do I pass the value in my dataTable to another jsf page? I managed to pass the value by using f:param but I have no clue how to get the data in dataTable and pass the value.
<p:outputPanel id="panelSysMenuList">
<p:dataTable id="dtSysMenu" paginator="true" paginatorPosition="bottom" paginatorAlwaysVisible="true"
var="menu" styleClass="panel-list" filteredValue="#{menuController.filteredList}"
value="#{menuController.resultList}" emptyMessage="#{msg.noRecordsFound}"
rows="#{menuController.datatableMaxResult}" selection="#{menuController.selectMenu}"
widgetVar="widVarTable" rowKey="#{menu}" selectionMode="single">
<p:column sortBy="#{menu.admenuPK.parentNode}" >
#{menu.admenuPK.parentNode}
</p:column>
<p:column sortBy="#{menu.admenuPK.node}" >
#{menu.admenuPK.node}
</p:column>
<p:column sortBy="#{menu.nodeDesc}" >
#{menu.nodeDesc}
</p:column>
<p:column sortBy="#{menu.className}" >
#{menu.className}
</p:column>
<p:column sortBy="#{menu.nodeRemark}" >
#{menu.nodeRemark}
</p:column>
</p:dataTable>
</p:outputPanel>
<p:button id="btnAdd2" value="#{msg.sysMenuAdd}" icon="ui-icon-plus" styleClass="action-buttons" outcome="MenuMaint">
<f:param name="parentNode" value="#{menuController.parentNode}" />
<f:param name="node" value="#{menuController.selectMenu}" />
</p:button>
My MenuMaint
<f:metadata>
<f:viewParam name="parentNode" value="#{menuMaintController.parentNode}" />
<f:viewParam name="node" value="#{menuMaintController.node}" />
</f:metadata>
Or is that anyway that I can do?
BTW, I am using ViewScoped
Thanks in advance!
I have solved my question by adding the param at the end of my URL
MenuMaint?parentNode=parentNode&node=selected
at the managed bean of MenuMaint, I will get the param using
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("parentNode")
this enable for me to get the value in another way than f:param

Primefaces dataTable instant Row Selection not working

I am working with Primefaces 3.5, Spring 3.2, JSF 2.2 and Apache is my server.
My problem is that whenever i click on the row, the row select event is never triggered in my backing bean and my store details panel is never updated.
My backing bean is in request scope and contains onRowSelect method.
However when i change my backing bean to sessions scope(it requires a few changes in backing bean), instant row selection works.
For the simplicity of my question i have modified my code to include only relevant part. In my param, for the sake of testing i just put existing customer with ID 35 in my test page.
Here is my dialog.xhtml
<h:form id="customerListingsForm">
<p:dialog id="manageStoresDialog"
header="Manage #{manageStoreBean.store.owner.name}'s Stores"
widgetVar="manageStoresDlg"
modal="true"
resizable="false">
<p:panelGrid id="storePanel" rendered="#{manageStoreBean.owner != null}">
<f:facet name="header">
<p:row>
<p:column>Store Details</p:column>
<p:column>Customer Store List</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:panelGrid id="storeDetails">
<p:row>
<p:column>Name: </p:column>
<p:column>
<h:inputText value="#{manageStoreBean.store.name}" />
</p:column>
</p:row>
<p:row>
<p:column>Description: </p:column>
<p:column>
<h:inputText value="#{manageStoreBean.store.description}" />
</p:column>
</p:row>
<p:row>
<p:column>Short Code: </p:column>
<p:column>
<h:inputText value="#{manageStoreBean.store.shortCode}" />
</p:column>
</p:row>
<p:row>
<p:column>Owner name:</p:column>
<p:column>
<h:inputText value="#{manageStoreBean.store.owner.name}" readonly="true" />
</p:column>
</p:row>
<p:row>
<p:column>Store Type: </p:column>
<p:column>
<p:selectOneMenu
id="storeTypeSelected"
value="#{manageStoreBean.placeType}"
var="storeType"
style="height:25px"
converter="#{placeTypeConverter}">
<f:selectItems
value="#{manageStoreBean.typeList}"
var="storeTypeItem"
itemLabel="#{storeTypeItem.code}"
itemValue="#{storeTypeItem}"/>
<p:column>#{storeType.description}</p:column>
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>Country: </p:column>
<p:column>
<p:selectOneMenu
id="storeCountrySelected"
value="#{manageStoreBean.country}"
var="country"
style="height:25px"
converter="#{countryConverter}">
<f:selectItems
value="#{manageStoreBean.countryList}"
var="countryItem"
itemLabel="#{countryItem.isoNumericCode}"
itemValue="#{countryItem}"/>
<p:column>#{country.isoNumericCode}</p:column>
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>Localisation: </p:column>
<p:column>
<p:selectOneMenu
id="storeLocalisationSelected"
value="#{manageStoreBean.localisationData}"
var="storeLocal"
style="height:25px"
converter="#{localisationConverter}">
<f:selectItems
value="#{manageStoreBean.localisationList}"
var="local"
itemLabel="#{local.regionCode}"
itemValue="#{local}"/>
<p:column>#{storeLocal.id} - #{storeLocal.regionCode}</p:column>
</p:selectOneMenu>
</p:column>
</p:row>
</p:panelGrid>
</p:column>
<p:column style="width:250px">
<p:dataTable id="stores"
var="store"
value="#{manageStoreBean.storeList}"
rowKey="#{store.id}"
scrollHeight="240"
scrollable="true"
selectionMode="single"
selection="#{manageStoreBean.selectedStore}"
lazy="true">
<p:ajax event="rowSelect" immediate="true"
listener="#{manageStoreBean.onRowSelect}"
update=":customerListingsForm:storeDetails"/>
<p:column headerText="Salon Name">
<h:outputText value="#{store.name}" />
</p:column>
<p:column headerText="ID">
<h:outputText value="#{store.id}" />
</p:column>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
<p:commandButton
value="Save"
action="#{manageStoreBean.saveStore}"
oncomplete="manageStoresDlg.hide()">
<f:param name="storeCustomerId" value="#{manageStoreBean.owner.id}" />
</p:commandButton>
<h:outputText value=" " />
<p:commandButton
value="Close"
oncomplete="manageStoresDlg.hide()"/>
</p:dialog>
<p:commandButton value="Add Store"
oncomplete="manageStoresDlg.show()"
update=":customerListingsForm:manageStoresDialog"
process="#this">
<f:param name="storeCustomerId" value="35" />
</p:commandButton>
</h:form>
Also i'm new to primefaces and web dev, but did a lot of learning myself before i started this project.
I spent 2 days try to understand what's wrong and searched all web but could not find any solution. May be i'm missing something or doing wrong not sure.
Any help is much appreciated.
Try making your bean view scoped.
If you are making it request scoped, then you are telling JSF to throw out all of your existing data every time the user is done talking to your server. So it makes no sense to have JSF run a listener on an existing page. In particular, PrimeFaces has a tendency to malfunction when it encounters an odd set of scopes.
See also Difference between View and Request scope in managed beans
By searching a lot of stuff on the internet i came to conclusion that datatable does not work very well with backing bean in request scope when table is loaded using dynamic argument. That is very shame. I do not see any other option but to implement viewscope in spring for my beans.

How to fetch validation error messages using FacesContext?

My xhtml contains input fields, where user can input numeric or alphabet values. Using an ajax call I am validating the input value and throwing a validation error on the . But even after entering invalid characters and error message, user may still hit the 'Save' button. Once the user hits the Save button it triggers a method in the Controller. Is there a way I can check for validation errors on p:messages inside the controller using the FacesContext.getCurrentInstance().
xhtml code
<!-- Messages -->
<p:messages id="errorMessages" globalOnly="false" showDetail="true" showSummary="false" closable="true" />
<!-- Column input field-->
<p:column>
<f:facet name="header">
<h:outputText value="Input Amount" escape="false" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.amount}" escape="false">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
</h:outputText>
</f:facet>
<f:facet name="input">
<p:inputText id="input" value="#{row.amount}" maxlength="10"
size="10">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
<f:validator validatorId="hourlyValueValidator" for="input" />
<p:ajax event="change" partialSubmit="true" process="input"
update=":#{p:component('errorMessages')}" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<!-- Save Button -->
<p:commandButton id="btnSubmit" value="Save"
update=":#{p:component('tblScrollPnl')} :#{p:component('errorMessages')}"
action="#{controller.saveValues()}" ajax="true" />
Controller in ViewScope
public void saveValues() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
List<FacesMessage> errorMsgList = context.getMessageList("globalMessages");
......
......
......
}
You don't need to get messages from the bean side for validation, as you experienced, view side validation occur but doesn't block your action.
There is couple of thing wrongs in your view, to fix your problem you need to change the process attribute of your p:commandButton, ortherwise only the button is processed (by default process="#this", so the validation is skipped.
<p:commandButton id="btnSubmit" value="Save" process="#form" update=":tblScrollPnl :errorMessages" action="#{controller.saveValues()}" />
Also note that I've removed ajax="true" since it is by default and fixed major problems in update="".
You should also rearrange your validators :
<p:inputText id="input" value="#{row.amount}" maxlength="10" size="10" validator="hourlyValueValidator">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3" maxIntegerDigits="5" />
</p:inputText>

PrimeFaces second dialog window not opening from first dialog

I have a issue trying to open a second dialog from the first dialog. The first dialog opens fine but the second dialog does not seem to open at all. I have tried with the form tag inside and outside of the dialog but get neither seems to open the dialog. On clicking the OPen dialog button from the first nothing seems to happen. I am using PrimeFaces 3.5.
Code for dialog is as below
<p:dialog id="dialog" header="New Submission" widgetVar="dlg" resizable="false" modal="true" closable="true">
<h:form id="createDialogForm">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="projectDropDown" value="Project:" />
<h:selectOneMenu id="projectDropDown" value="#{newSubmissionBean.submission.project}" required="true">
<f:selectItem itemLabel="Choose project" noSelectionOption="true" />
<f:selectItems value="#{newSubmissionBean.projectEntities}" var="project" itemLabel="#{project.name}" itemValue="#{project}" />
</h:selectOneMenu>
<f:facet name="footer">
<p:commandButton id="createButton" value="Create" update=":newSubmissionForm :createDialogForm"
actionListener="#{newSubmissionBean.createSubmission}"
oncomplete="handleRequest(xhr, status, args)"
/>
<p:commandButton id="chooseBatchButton" value="OPen dialog" update=":batchChooserForm"
actionListener="#{newSubmissionBean.fetchAvailability}" />
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
<p:dialog id="batchDialog" header="Batch Chooser" widgetVar="bdlg" resizable="false" modal="true" closable="true">
<h:form id="batchChooserForm">
<p:fieldset legend="#{messages['label.legend.sample.available']}">
<p:dataTable id="availableSamples" var="sample" value="#{newSubmissionBean.availableSamples}">
<p:column style="width:20px">
<h:outputText id="dragIcon"
styleClass="ui-icon ui-icon-arrow-4" />
<p:draggable for="dragIcon" revert="true" />
</p:column>
<p:column headerText="#{messages['label.sample.batch']}">
<h:outputText value="#{sample.sampleId}" />
</p:column>
</p:dataTable>
</p:fieldset>
<p:fieldset id="selectedSamples" legend="#{messages['label.legend.sample.selected']}" style="margin-top:20px">
<p:outputPanel id="dropArea">
<h:outputText value="#{messages['label.drop.text']}"
rendered="#{empty newSubmissionBean.selectedSamples}"
style="font-size:24px;" />
<p:dataTable var="sample" value="#{newSubmissionBean.selectedSamples}"
rendered="#{not empty newSubmissionBean.selectedSamples}">
<p:column headerText="#{messages['label.sample.batch']}">
<h:outputText value="#{sample.sampleId}" />
</p:column>
<!-- p:column style="width:32px">
<p:commandButton update=":carForm:display"
oncomplete="carDialog.show()"
icon="ui-icon-search">
<f:setPropertyActionListener value="#{car}"
target="#{tableBean.selectedCar}" />
</p:commandButton>
</p:column-->
</p:dataTable>
</p:outputPanel>
</p:fieldset>
<p:commandButton id="confirmBatch" value="Confirm Selection" update=":newSubmissionForm :createDialogForm"
actionListener="#{newSubmissionBean.confirmSelection}"
oncomplete="handleRequest(xhr, status, args)"/>
<p:droppable for="selectedSamples" tolerance="touch" activeStyleClass="ui-state-highlight" datasource="availableSamples" onDrop="handleDrop">
<p:ajax listener="#{newSubmissionBean.onSampleDrop}" update="dropArea availableSamples" />
</p:droppable>
</h:form>
</p:dialog>
the javascript function for on complete is
<script type="text/javascript">
function handleRequest(xhr, status, args) {
if(args.validationFailed) {
dlg.show();
}
else {
dlg.hide();
}
}
</script>
And the code in the action listener where I try to open the second dialog. This method does get called as I have break pointed on it.
public void fetchAvailability(ActionEvent actionEvent) {
RequestContext.getCurrentInstance().execute("batchDialog.show()");
}
Can anyone advise what I have done wrong? Thanks in advance
You're using the id of the dialog in your javascript, you need to use the widgetVar as such:
public void fetchAvailability(ActionEvent actionEvent) {
RequestContext.getCurrentInstance().execute("bdlg.show()");
}
For future debuggning, try executing the javascript in your web browser console. In this case it should say "batchDialog is not defined" or something similar, giving you a hint.
Ok figured out what the issue was just in case anybody has the same thing. The Primefaces showcase example code has not defined the handledrop javascript function thus object undefined error is thrown. So I added the following to my code and now my pop up shows up.
function handleDrop(event, ui) {
var selectedSample = ui.draggable;
selectedSample.fadeOut('fast');
}
Thanks

complete action then update table jsf

What I want is commandButton should complete the action first and then update dataTable, but it is actually updating dataTable before the action is performed. Can someone please suggest how to do this?. Thanks.
<p:dataTable id="files" var="file" value="#{tableBean.lstFiles}" rowKey="#{file.fname}" selection="#
{tableBean.selectedFile}" selectionMode="single" >
<p:ajax event="rowSelect" listener="#{tableBean.onRowSelect}" />
<p:ajax event="rowUnselect" listener="#{tableBean.onRowUnselect}"/>
<p:column headerText="Date"> #{file.fname}</p:column>
<p:column headerText="Type"> #{file.ftype}</p:column>
<p:column headerText="Date"> #{file.fdate}</p:column>
<p:column headerText="View File" >
<p:commandButton id="folder" action="#{folderName.name}" value="Open" update=":form2:files" icon="ui-icon-arrowthichk-s" >
<f:setPropertyActionListener target="#{folderName.foldername}" value="#{file.fname}" />
</p:commandButton>
</p:column>
</p:dataTable>

Resources