I have a JSF form, that contains some JavaScript. When a specific input changes to a value >= 10 a selectOneMenu needs to dynamically change its option (which is yes or no).
My jQuery:
if ($("input[id*='inputBox']").val() >= 10) {
$("select[id*='selectOneMenu']").val("No");
} else {
$("select[id*='selectOneMenu']").val("Yes");
}
When I debug, the value in the selectOneMenu is changed correctly, but the UI component doesn't change its option. How do I tell the selectOneMenu to update its rendered value?
PrimeFaces SelectOneMenu is not a trivial select tag, it is a collection of divs put together to make it look like a select tag, use widgetVar.selectValue(val);
select tag is hidden inside the visible UI parts so that's why it is not working for you.
Your best bet will be to call a p:remoteCommand once the value should toggle. You will need to have the remoteCommand update your selectOneMenu ID (or IDs).
<p:remoteCommand name="setNo" actionListener="#{myBean.setNo}"
update="selectOneMenu" />
<p:remoteCommand name="setYes" actionListener="#{myBean.setYes}"
update="selectOneMenu" />
<SCRIPT>
var val="No"; //I'm assuming your initial value is "No"
if ($("input[id*='inputBox']").val() >= 10) {
if (val==="Yes") {
setNo();
val="No";
}
} else {
if (val==="No") {
setYes();
val="Yes";
}
}
</SCRIPT>
Then in your backing bean:
Map<String, String> selectBoxValues = ...
public void setNo() {
selectBoxValues.put("No", index);
}
[ditto for yes]
Of course, I've made up a bunch of variable names; you will have to change the text to suit your real variables.
Related
So, here is the jsf component:
<h:selectBooleanCheckbox id="cb#{index}" value="backingBean.value" />
And here is a part of the backing bean java:
/**
* getValue is a method which checks if a checkbox is selected or not, using the checkbox ID
*/
public boolean getValue() {
//TODO: get the checkbox id
String checkboxID = ??
if (getCheckedIDs().contains(checkboxID)) {
return true;
}
return false;
}
When the page is loading the checkboxes, I want to check this way if the checkbox is selected or not.
So the question is, what to write instead of ?? to get the ID of the checkbox who called the method?
It's very important that I can use only JSF 1.1, so there are many solutions which won't work with this version.
EDIT: as #Kukeltje correctly notes, the main issue is that the value expression is incorrect. Once you change that, the below is applicable.
You don't need to "calculate" the value ("set" or "unset") of your checkbox. JSF will simply call backingbean.setValue(x) (with x being true or false) depending on whether the checkbox is on or off at that moment (i.e. when you submit the page).
This happens automatically because you said value="#{backingBean.value}".
So in setValue() you simply store the argument, in getValue you return the stored argument. The rest is done by JSF for you.
If you want the checkbox to be on by default, you set the stored value to true.
For example:
private boolean storedValue = true; // or false if you want it to be off by default
public boolean getValue() {
return storedValue;
}
public void setValue(boolean value) {
this.storedValue = value;
}
So, here is the jsf component:
<h:selectBooleanCheckbox id="cb#{index}" value="#{backingBean.value}" />
And here is a part of the backing bean java:
/**
* getValue is a method which checks if a checkbox is selected or not, using the checkbox ID
*/
public boolean getValue() {
//TODO: get the checkbox id
String checkboxID = ??
if (getCheckedIDs().contains(checkboxID)) {
return true;
}
return false;
}
When the page is loading the checkboxes, I want to check this way if the checkbox is selected or not. So the question is, what to write instead of ?? to get the ID of the checkbox who called the method? It's very important that I can use only JSF 1.1, so there are many solutions which won't work with this version.
Another very important thing is, that I cannot use the setter/getter in backing bean like here: https://stackoverflow.com/a/48006066/9158590, because I need to store the value of the checkbox immediately after it's checked or unchecked, not only after submit. I have already resolved the storing in backing bean right after checking, I only need to send back true or false when loading page.
This is because I use a page navigation, and for example, when I check a box in page 1, and go to another page, and then go back, the box isn't selected anymore (only in backing bean).
FacesContext context = FacesContext.getCurrentInstance();
UIComponent comp = context.getViewRoot().findComponent("Parent Element
id of HtmlSelectBooleanCheckbox ");
for(UIComponent c : comp.getChildren())
if(c instanceof HtmlSelectBooleanCheckbox)
{
// do something
}
Coming to your Question :
the value of the variable "#{backingBean.value}" is true then the checkbox will be selected
I have a lot of .xhtml views. Many of them have blockui elements, for example:
<p:blockUI block="tab" widgetVar="subscriberSelectBlocker">
</p:blockUI>
Sometimes there are more then 1 of them per view.
To hide the blockUI above I have a method in the according bean SubscriberFilterBean.java:
public void hideSubscriberSelectBlockUi() {
RequestContext.getCurrentInstance().execute("subscriberSelectBlocker.hide()");
}
Now the problem is that I need to hide all existing blockuis at once.
I could hide them individually like above, but that would mean that I would basically have the same code repeated over 15 times and the method to execute all these methods would be huge.
Is there a way to hide all elements of tag?
Something like
public void hideSubscriberSelectBlockUi() {
RequestContext.getCurrentInstance().execute("p:blockui.hide()");
}
When you are specifying widgetVar in a PrimeFaces element, JavaScript widget object is instantiated and assigned to a global variable with the specified name in the window scope. This means that this objects could be found and manipulated.
I suggest finding them by blockui ids. Widget object contains it's id, so after obtaining all the global objects and all the blockui ids from page, we can determine which of the global objects are blockui widgets. Blockui ids from page could be obtained using jQuery class selector, as they all have common css style: .ui-blockui.
Here is JavaScript example code that shows all blockui components on the page:
var keys = Object.getOwnPropertyNames( window );
var blocks = $('.ui-blockui');
var blockIds = [];
blocks.each (function (index,value) {
blockIds[index] = value.id;
});
$.each(keys, function (index, value) {
var obj = window[ value ];
if (obj != null) {
gObj = window[ value ];
if(gObj.blocker != undefined) {
if ($.inArray(gObj.blocker.attr('id'), blockIds) != -1) {
gObj.show();
}
}
}
});
I spent already more time as is good about some saving or updating issue in inputtext field, i go right to the point:
i have basic single input text with some basic attributtes
<h:inputText id="name" value="#{salesController.selectedSalesName}" />
here is a getter for inputText value
public String getSelectedSalesName(){
for(DealerListView dealer : dealerList){
if(dealer.getDealerId() == getSelectedDealerId()){
return dealer.getName();
}
}
return "";
}
nested in there i use ajax tag
<f:ajax event="change" render="name" listener="#{salesController.updateSelectedSalesName()}" />
a here is back bean method for updating a input text field
public void updateSelectedSalesName() {
DealerData dealDat = BeanFactory.getHotelDAOService(DealerData.class).findOne(selectedDealerId);
dealDat.setName(name);
BeanFactory.getHotelDAOService(DealerData.class).update(dealDat);
}
whole result of this is stack trace which say
value="#{salesController.selectedSalesName}": Property 'selectedSalesName' not writable on type sk.hotel.web.controller.SalesController
I know that something changes is need for that getter method but try some combinations without result which make corect update of value to database.
(I dont use any commands buttons for submit,update only response on pressing Enter in that inputText field.)
I want some guide how can be modified this save/update process whether on back-bean or jsf layout
or maybe someone solved similar situation already,and can share his solution.
Thanks all of you for advice posts.
Regards and nice day
First, add a field:
String selectedSalesName;
Add a setter and setter:
public String getSelectedSalesName() {
return selectedSalesName;
}
public void setSelectedSalesName(String selectedSalesName) {
this.selectedSalesName = selectedSalesName;
}
Add a ajaxListener(AjaxBehaviurEvent event) to create a new Dealer or Update current Dealer
public void ajaxListener(AjaxBehaviorEvent event) {
Dao dao = BeanFactory.getHotelDAOService(DealerData.class)
if (selectedDealerId == null) {
DealarData dealerData= new DealerData();
dealerDate.setName(getSelectedSalesName());
dao.add(dealerData);
setDealer(dealerData);
} else {
DealerData dealDat = dao.findOne(selectedDealerId);
dealDat.setName(name);
dao.update(dealDat);
}
}
A setter to the current dealer
int selectedDealerId;
public void setDealer(DealerData dealer) {
selectedDealerId = dealer.getId();
selectedSalesName = dealer.getName();
}
And the xhtml page:
<h:inputText value="#{salesController.selectedSalesName}" id="idSalesInput">
<a4j:ajax event="keyup" listener="#{salesController.ajaxListener}"
execute="idSalesInput"/>
</h:inputText>
Change "keyup" for the event you want to listen.
When you press a key, the listener is called, and the value of idSalesInput is submitted (the setSelectedSalesName() method is called here, for this reason you got the Property 'selectedSalesName' not writable exception),and the listener create or update a new DealerData.
Have a nice Day and sorry for my bad english!
Binding value in your inputText is two way, when it is rendered than getter is called to calculate value, when it is submited (like in your AJAX event) setter is called for that property to set value in your backing bean. So JSF tries to call setSelectedSalesName(String value). JSF can't write your property, which means can't call setter.
See also:
AJAX listener not being fired for inside
I mean this. I have a JSF page with a PrimeFaces Datatable, and I want to get a rowstyleClass name from bean. But bean method should check a record from table to return a String value of styleClass.
can I do something like this
#ManagedBean(name="styleController")
#SessionScoped
public class StyleController {
public StyleController() {
}
public String getContractStyle(ContrMain contrMain){ //ContrMain - entity class
if(contrMain.getCloseDate()!=null && contrMain.getClosedBy()!=null){
return "closed";
}else{
return "acting";
}
}
}
and in JSF page something like this
<p:dataTable value="#{contrMainController.items}"
var="item"
...
rowStyleClass="ui-datatable-#{styleController.contractStyle(item)}">
I know that it doesn't work. And is there some way to use an "item" variable?
I have found decision.
the answer is simple.
rowStyleClass="ui-datatable-#{styleController.getContractStyle(item)}"
instead of
rowStyleClass="ui-datatable-#{styleController.contractStyle(item)}"
works fine