How to make one SelectManyMenu depends on other in JSF? [duplicate] - jsf

This question already has answers here:
Make multiple dependent / cascading selection components in JSF
(4 answers)
Closed 1 year ago.
I have two selectManyMenu in PrimeFaces:
<p:selectManyMenu id="vehicleMakes"
value="#{analyticsController.selectedVehiclesMake}"
filter="true"
filterMatchMode="startsWith"
multiple="true"
scrollHeight="250"
showCheckbox="true"
styleClass="customInput manymenu-advanced"
label="#{msg['ANALYTICS-vehicle-brand-select']}">
<f:selectItems var="make" itemLabel="#{make}"
value="#{analyticsController.vehicleMakeList}"
itemValue="#{make}"
/>
<p:ajax event="change" listener="#{analyticsController.restoreFilters()}">
</p:ajax>
</p:selectManyMenu>
An other:
<p:selectManyMenu id="vehicleModels"
value="#{analyticsController.selectedVehiclesModel}"
filter="true"
filterMatchMode="startsWith"
multiple="true"
scrollHeight="250"
onchange="familyComponents(this, 'gfVehicleModelList', 'selectManyMenu')"
showCheckbox="true"
styleClass="customInput manymenu-advanced gfVehicleModelList"
label="#{msg['ANALYTICS-vehicle-model-select']}">
<f:selectItems
var="model"
itemLabel="#{model}"
value="#{analyticsController.vehicleModelList}"
itemValue="#{model}"
/>
</p:selectManyMenu>
But I need that when an element is selected in select number one the content is loaded depending on a FK in select number two, for this I create this function:
public void filterModelBySelectedMakes(){
if (!CollectionUtils.isEmpty(selectedVehiclesMake) && !CollectionUtils.isEmpty(makes)){
List<Integer> makeIds = makes.stream().filter(make -> selectedVehiclesMake.contains(make.getName())).map(Make::getId).sorted().collect(Collectors.toList());
vehicleModelList = models.stream().filter(model -> makeIds.contains(model.getMakeId())).map(Model::getName).filter(StringUtils::isNotBlank).sorted().collect(Collectors.toList());
}
}
Using directly the List that I already have loaded, but I don't know when to execute this function? Or if there is another easier method, could you help me please?
Thanks in advance

You want to look at this example: https://www.primefaces.org/showcase-v8/ui/ajax/dropdown.xhtml
Its exactly what you want to do, change the contents of a second component based on the Selection in the first component.

Related

Two filters in one selectCheckbox in Primefaces

Let's see if someone can help me with this.
I have a selectCheckboxMenu made with primefaces.
Is there a possibility to add another filter apart from the one I already have to select all the nodes that are editable?
Here is the code:
<p:selectCheckboxMenu id="selectNodo"
label="Nodos"
value="#{simulacionHandsontableBean.selectedNodos}"
widgetVar="selectNodoWidget"
styleClass="my-selectcheckboxmenu-style"
converter="dtoConverter"
filter="true" filterText="Filter" filterMatchMode="contains"
style="width:200px !important"
panelStyleClass="ui-multicheckbox"
onchange="refreshSelectCheckboxMenuStyle('selectNodoWidget', 'width: 200px !important'); showLoading();">
<f:selectItems value="#{simulacionHandsontableBean.nodos}" var="nodo" itemLabel="#{nodo.descripcion}, #{nodo.id}, #{request.contextPath}#{nodo.iconPath}" itemValue="#{nodo}"/>
<p:ajax process="#this" listener="#{simulacionHandsontableBean.onNodoChange}" oncomplete="clearHandsonTable();onCompleteChangeSelectNodo();" update=":accordionFiltros:formFiltros:selectMarcasPackLitro :accordionFiltros:formFiltros:selectMarcasPackLata :accordionFiltros:formFiltros:selectNodoAComparar :accordionFiltros:formFiltros:selectSegmento :accordionFiltros:formFiltros:btnAceptar"/>
<p:ajax event="toggleSelect" onstart="showLoading();" listener="#{simulacionHandsontableBean.onToggleSelectCheckBoxMenuNodos}" oncomplete="hideLoading();" update=":accordionFiltros:formFiltros:selectSegmento"/>
</p:selectCheckboxMenu>
The view
The idea of ​​how I want it to be
Any idea?
Thanks in advance!

How enable/disable <p:tab> without update form?

I have a big <h:form>, with a <p:tabView> with 3 <p:tab>
I want to enable/disable one <p:tab> based on <p:selectOneRadio> click.
How can I achieve this without updating the entire form?
<p:selectOneRadio id = "pessoa" required = "true"
value="#{myBean.typeChecked}" valueChangeListener="#{myBean.checkTypeSelec}">
<f:selectItem itemLabel="item 1" itemValue="PF" />
<f:selectItem itemLabel="item 2" itemValue="PJ" />
<p:ajax event="click" update="???" execute="#this"/>
</p:selectOneRadio>
<p:tab title="History" id="historyTab" disabled="#{myBean.historyDisabled}">...
I dont wanna update entire form because I dont wanna loose other inputs data

Filter p:datatable by boolean column (checkbox)

I am trying to filter my primefaces datatable by a boolean column using a checkbox filter but unfortunately filtering in primefaces datatable seems it does not work with any type other than String, but there should be a workaround for this case.
datatable column
<p:column headerText="A_boolean_column" filterBy="#{myBean.myBoolean}" filterMatchMode="exact">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter" onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{myBean.myBoolean}"/>
</p:column>
where possibleAnswers variable is a list that has been initialized on init method of myBean with true && false values
#PostConstruct
public void init(){
this.possibleAnswers= new ArrayList<>();
possibleAnswers.add(true);
possibleAnswers.add(false);
}
I have similar working examples in my datatable with text values and are working perfectly. Of course I could do a workaround to fix my issue by converting the values from boolean ( true / false) into String ("true" / "false") ( or even write a custom function to check for equality)but I don't really like this solution and I would prefer any other out of the box solution ( perhaps a different filterMatchMode ? ).
I am using primefaces 7.0
Normally an input component has a 'value' attribute that is bound to a field (getter/setter) in a backing bean. The type of this field can be used to automatically convert the technical string of the http request to the correct java type. For a datatable filter this cannot be automatically done since there is no value attribute. Giving all components knowledge about all possible containers they can be used in is bad design. So the only and corrrect solution is to use an explicit converter.
Have a Look at the Implementation of the Status-Column in the PrimeFaces datatable filter showcase, as far as I see it it is exactly what you need
for Reference:
<p:column filterBy="#{myBean.myBoolean}" filterMatchMode="in">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter"
onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:converter converterId="javax.faces.Boolean" />
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
</p:column>

Primefaces dataTable filter selectOneMenu not working

I am using PrimeFaces 5.1, In my project dataTable to filter used.In text filter is work fine but dropdown filter is not working properly (i.e) In dropdown I show department,First time I choose any value from dropdown is work fine anothertime I choose dropdown It not return any value show in dataTable.I choose select one first value from dropdown also throw null pointer exception.
<p:dataTable id="datalist" widgetVar="datalist" var="user" value=#{beanList.userList}>
<p:column headerText="Department" filterBy="#{user.deptname}"
filterMatchMode="exact" >
<f:facet name="filter">
<p:selectOneMenu onchange="PF('datalist').filter()">
<f:selectItem itemLabel="ALL" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{datalist.deptList}" />
</p:selectOneMenu>
</f:facet>
<h:outputText value="#{user.depatname}" />
</p:column>
</p:dataTable>
My doubt is default value ALL click and second time select any value return null or no data show in dataTable.
Since I don't know the scope of your Managed Bean: Try a scope longer than request (see PrimeFaces 5.1 User Documentation), and provide a value "filteredValue" for your table, like this:
<p:dataTable id="datalist" widgetVar="datalist" var="user"
value="#{beanList.userList}" filteredValue="#{beanList.filteredUserList}">
With that, you make sure to keep your filtered table/list in a field in your managed bean and the contents won't get lost.
Also, make sure your Managed Bean class is serializable (See this stackoverflow post)

Check box and Radio Button selection gets cleared on h:selectOneMenu value change

I am working on a form which has two datatables. In the first datatable, I am displaying the radio buttons and checkboxes, whereas the second datatable contains
selectOneMenu(s). Value change listener has been implemented on the selectOneMenu and on value change, the subsequent fields gets populated based on the selection.
Everything works fine except that when I select the value from the dropdown, the checkbox and the radio button values are cleared.
f:ajax event="change" has been used for the change event on selectOneMenu. Looks like the whole page gets refreshed and the values are cleared.
I also tried using f:ajax by updating the id of the datatable in which the selectOneMenu is present.
<p:dataTable styleClass="borderless" id="inResultTable" var="result" value="#{RequestBean.fooFields}" rendered="#{not empty RequestBean.fooFields}">
<p:column style="width:150px;">
<f:facet name="header">
<h:outputText value=" " />
</f:facet>
<h:outputText value="#{msg[result.fldLabel]}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<ui:repeat value="#{RequestBean.fooFields}"
var="itm">
<h:selectOneMenu value="#{itm.indFieldValue}"
rendered="#{result.lvlid==itm.lvlid}"
style="width:250px;padding-right:150px;text-align: left;">
<f:selectItems value="#{RequestBean[itm.indField]}" />
</h:selectOneMenu>
<h:selectOneRadio value="#{itm.indFieldValue}" rendered="#{result.lvlid==itm.lvlid and result.fldType=='radio'}"
style="width:250px;padding-right:150px;text-align: left;">
<f:selectItems value="#{RequestBean[itm.indField]}" />
</h:selectOneRadio>
<h:selectManyCheckbox value="#{itm.indFieldCheckBox}" rendered="#{result.lvlid==itm.lvlid and result.fldType=='selectbox'}">
<f:selectItems value="#{RequestBean[itm.indField]}" />
</h:selectManyCheckbox>
</ui:repeat>
</p:column>
</p:dataTable>
<p:dataTable styleClass="borderless"
id="resultTable" var="result"
value="#{RequestBean.depFields}">
<h:selectOneMenu value="#{RequestBean.field4Value}"
valueChangeListener="#{RequestBean.processValueChange4}"
rendered="#{result.lvlid=='5' and result.fldType=='selectbox'}"
style="width:250px;padding-right:150px;text-align: left;">
<f:selectItem itemLabel="--Please Select--" itemValue="#{null}" />
<f:selectItems value="#{RequestBean[result.field]}" />
<f:ajax event="change" render="#form" />
</h:selectOneMenu>
...
...
</p:dataTable>
N.B: The requirement is such that the datatable with checkbox and radio buttons has to be placed at the top. IDs cannot be used for the selectOneMenu in the second table since I am
populating the selecOneMenu(s) dynamically based upon the entries in the database.
I know that this might be a simple issue but didn't have any luck with the info posted on most websites.
Any help would be appreciated.
Here's your <f:ajax>.
<f:ajax event="change" render="#form" />
You didn't specify the process attribute. So it defaults to #this, which means that only the current input component is processed during submit. However, you specified a render of the whole form. So the whole form will be refreshed. However, the other input components aren't been processed during submit. So their model values won't be updated, which means that during render response the initial values will be redisplayed instead of the submitted values, which are in your case apparently blank/null.
You've basically 2 options:
Process the whole form during submit.
<f:ajax process="#form" render="#form" />
Or, update only those components which really needs to be updated.
<f:ajax render="clientId1 clientId2 clientId3" />
In your particular case I think option 2 isn't viable, so option 1 is the best one. Please note that I omitted the event attribute. It defaults to change already.
Unrelated to the concrete problem, I'm not sure what you're all doing in your value change listener method, but I only want to note that most starters abuse it for the purpose of manipulating the model values and that it don't always work quite as expected (changed model values are not reflected at all). If this is true in your case, then you should actually be using <f:ajax listener> instead.
See also:
When to use valueChangeListener or f:ajax listener?
The main reason for the values of Check boxes & Radio buttons getting cleared after a value change listener could be that the model values are not updated.
Value change listeners are called before updating the model.
For these kind of issues, it is very important to have an understanding of the JSF lifecycle.
Please try following code in your value change listener and see.
public void valueChange(ValueChangeEvent event){
PhaseId phase = event.getPhaseId();
if (phase.equals(PhaseId.ANY_PHASE))
{
event.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
event.queue();
return;
}
if(!phase.equals(PhaseId.UPDATE_MODEL_VALUES))
{
return;
}
// Your code
}
I didn't try this code with your example. Please let me know with complete example source code if it is not working.
Thanks.

Resources