jsf standard converter along with primefaces select one menu - primefaces

When the "select" option of the select one menu is selected, the default value is saved in DB as 0. Can I change this to null?
The value of select one menu is bound to a bean property. To achieve this, I have used the converter attribute of the select one menu component. But it does not seem to convert empty string to null. What am I missing?
<p:panelGrid columns="2" >
<p:outputLabel value="District" />
<p:selectOneMenu value="#{cJData.cJ.dC}" converter="javax.faces.Long">
<p:ajax listener="#{cJActionHandler.selectDC}"/>
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItems value="#{cJData.DCs}" var="coun"
itemLabel="#{coun.name}" itemValue="#{coun.id}" />
</p:selectOneMenu>
</p:panelGrid>

Use the following
<f:selectItem itemLabel="Select" itemValue="#{null}" />
As the docs of LongConverter#getAsObject say - this is the method which is called when you submit th value - the Long converter will convert only null to null.

Related

Uncheck all p:selectManyCheckbox checkboxes from a p:selectOneRadio item

How could I Uncheck all checkboxes from selectManyCheckbox when choosing "No" from selectOneRadio
<p:selectOneRadio id="radio" value="#{myView.myObject.myBoolean}">
<f:selectItem itemLabel="Si" itemValue="#{true}"/>
<f:selectItem itemLabel="No" itemValue="#{false}"/>
<p:ajax process="radio" event="valueChange" update="#widgetVar(displayPanel)"/>
<p:ajax update="#this"/>
</p:selectOneRadio>
<p:panel widgetVar="displayPanel">
<p:outputPanel rendered="#{myView.myObject.myBoolean}">
<p:selectManyCheckbox widgetVar="mySelections" value="#{myView.myObject.myObjectList}" layout="grid" columns="8" styleClass="grid-checkbox">
<p:ajax update="#this"/>
<f:selectItems value="#{myView.things}" var="thing" itemLabel="#{thing.idThing}" itemValue="#{thing.thing}"/>
</p:selectManyCheckbox>
</p:outputPanel>
</p:panel>
As seen, update="#widgetVar(displayPanel)" will show the outputPanel when myBoolean is true and viceversa.
What I need to achieve is to uncheck all the already selected checkboxes (mySelections) when myBoolean is false (by selecting "No" from selectOneRadio above.
Possible? If so, Could you please show me how, I'm just starting on PrimeFaces.
Normally, the easiest way would be using the client side API uncheckAll. However, there is a bug. But, I've fixed it. So you can wait for PrimeFaces 12.0.0-RC3 or create a Monkey Patch.
A simple demo would be:
<p:selectOneRadio id="radio" value="#{testView.bool}"
onchange="if(this.value==='false'){PF('mySelections').uncheckAll()}">
<f:selectItem itemLabel="Si" itemValue="true"/>
<f:selectItem itemLabel="No" itemValue="false"/>
</p:selectOneRadio>
<p:selectManyCheckbox widgetVar="mySelections" value="#{testView.strings}">
<f:selectItems value="#{['a','b','c']}"/>
</p:selectManyCheckbox>
This eliminates the need to use Ajax.

How to detect row selection in Primefaces Datatable?

I use Prime Faces 6.2 to construct checkboxed column. Have this sample:
<p:dataTable id="List"
value="#{tickets}"
lazy="true"
paginator="true"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
currentPageReportTemplate="{startRecord}-{endRecord} из {totalRecords}"
rows="20"
rowKey="#{ticket.id}"
selection="#{ticketForm.abstractMTSBUExportTickets}"
var="ticket"
emptyMessage="Записи в данной категории отсутствуют">
<p:column selectionMode="multiple" style="width:40px; text-align:center">
<!-- <p:ajax event="click" listener="#{form.abstractMTSBUExportTickets}" /> --> // first try
<!-- <p:commandLink id="select" action="doSome" /> --> // second try
</p:column>
The purpose to use ajax is for making some button on page visible when at least one checkbox selected.
Firstly, I tried to insert p:ajax tag into column tag but got exception:
<p:ajax> Unable to attach behavior to non-ClientBehaviorHolder parent
More obviouse way was to insert command link into but it simply did nothing.
So, is there any practice to do this? Thanks for answers in advance.
Yes you are close but what you what is Ajax on the Datatable and not on the checkboxes. PF provides these two Ajax events on the datatable for being notified or boxes being checked or unchecked...
<p:datatable>
<p:ajax event="rowSelect" listener="#{dtSelectionView.onRowSelect}" update=":form:mybutton" />
<p:ajax event="rowUnselect" listener="#{dtSelectionView.onRowUnselect}" update=":form:mybutton" />
...
</p:datatable>
See the Select Events example from the Showcase for more info: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml
You can simply do something like in the Select and Unselect methods will trigger updating your selection and you can enable the button using EL expression like...
<p:commandButton id="mybutton" disabled="#{ticketForm.abstractMTSBUExportTickets.size > 0}" />

Primefaces datatable filter specific column in header

In Primefaces datatable, it can filter a specific column by the following code:
<p:column filterBy="#{car.id}" headerText="Id" filterMatchMode="contains">
<h:outputText value="#{car.id}" />
</p:column>
The code above will generate a filter on the header of the column. What I want is create a filter on the header of datatable, not column. From the ShowCase, there is a code:
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="PF('carsTable').filter()"/>
</p:outputPanel>
</f:facet>
This will put a filter in header of datatable, but it will filter the all fields of the datatable, which is what I don't want, I want it to filter specific column only, is it possible?
You could by using a few hacks. First add a widgetVar attribute to your p:dataTable, for example widgetVar="myTable". Then, add an id attribute to you p:column:
<p:column filterBy="#{car.id}"
headerText="Id"
filterMatchMode="contains"
id="clm">
<h:outputText value="#{car.id}" />
</p:column>
You can use this id to select the column filter field. In the global filter, you want to remove the id attribute, and add an onkeyup listener which sets the input's value in the column's filter and triggers filtering on the table using the widgetVar.
<f:facet name="header">
<p:inputText onkeyup="document.getElementById('frm:tbl:clm:filter').value=this.value; PF('myTable').filter();"/>
</f:facet>
Here I assume your form has the ID frm and your data table has the ID tbl. They probably have different IDs, so change the code accordingly.
If you don't want the filter to be visible in the column, you could hide it with a CSS rule:
.ui-datatable .ui-column-filter[id='frm:tbl:clm:filter'] {
display: none;
}
If you are using lazy loading you could also check out Primefaces lazy datatable: put my own filter instead of GlobalFilter

PrimeFaces: selectOneButton is cleared on validation error

I have a wizard tab and a few fields in it. Some of them are required.
There is also a selectOneButton. The problem is when I have some required fields empty and some value selected in selectOneButton - when the validation fails on form submit, it gets "unclicked", i.e. nothing is selected. I suspect that the whole form gets updated but other components still hold the visible value.
What am I doing wrong or maybe someone have had the same problem?
I use PrimeFaces-5.0.RC2
Here is the code:
<p:tab title="Service params" id="firstTab">
<h:panelGrid columns="2">
Name *
<p:inputText value="#{serviceMB.service.name}" label="Name" required="true" maxlength="150"/>
Unit
<p:selectOneButton value="#{serviceMB.service.unit}" converter="serviceUnitConverter"
filter="true" filterMatchMode="contains" label="Unit"
id="unitMenu"
required="true">
<f:selectItems value="#{serviceMB.allServiceUnits}" var="unit"
itemLabel="#{unit.name}"
itemValue="#{unit.code}"/>
<p:ajax event="change"
update=":addServiceForm:priceListPanel"
process="#this"/>
</p:selectOneButton>
</h:panelGrid>
<p:commandButton value="Next" icon="ui-icon-triangle-1-e" iconPos="right" type="button" onclick="PF('serviceWizard').next();"/>
So Unit is the component which value is cleared. I really have no idea why, so I moved to selectOneMenu instead which is less preferable in my case.

<p:selectOneMenu>, filter and converter

I have a menu with filter on. The value and label are both String. In this case, does the "converter" property have to be set? The reason I'm asking is that the filter function is not working if the "converter" is not provided. E.g.,
<p:selectOneMenu value="#{menuBean.selectedCountry}" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="Select One" itemValue=""/>
<f:selectItem itemLabel="US" itemValue="1"/>
<f:selectItem itemLabel="Spain" itemValue="2"/>
</p:selectOneMenu>
Thank you for your points and help!
You didn't specify your primefaces version but this is a bug in 3.4. You can set the height attribute of p:selectOneMenu or you can apply the following fix from here. SelectOneMenu with filter not working with less than 10 elements.