Pass value of p:selectOneMenu item in parameter function - jsf

I want to refresh my table when I select a value in the dropdown, and in the table I want to call a function with the selected value as a parameter.
The value I select has to come in the function calculateAveragePrice as a parameter.
This is my dropdown with all the different regions.
<p:selectOneMenu id="list" value="#{regionController.region}">
<f:selectItems value="#{regionController.regions}" var="region" itemLabel="#{region.name}"/>
<f:ajax execute="list" render="myTable" />
</p:selectOneMenu>
This is the table I want to refresh:
<table id="myTable" class="table table-responsive">
<tr>
<td>#{msg.averagePrice}:</td>
<td>#{flightController.calculateAveragePrice(regionController.region)}</td>
</tr>
</table>
RegionController bean:
#Named
#RequestScoped
public class RegionController {
#Inject
RegionService regionService;
private Region region;
private Collection<Region> regions;
#PostConstruct
public void initialize() {
regions = regionService.findAll();
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public void setRegions(Collection<Region> regions) {
this.regions = regions;
}
public Collection<Region> getRegions() {
return regions;
}
}

If i got it right, it's just a design issue.
You could just create a property called region inside flightController.
Then, you would have:
<p:selectOneMenu id="list" value="#{FLIGHTCONTROLLER.region}">
<f:selectItems value="#{regionController.regions}" var="region" itemLabel="#{region.name}"/>
<f:ajax execute="list" render="myTable" />
</p:selectOneMenu>
Now, you don't have to pass the regions as parameter, because FlightController already have that property setted via ajax.
The way you did, you're setting regionController.Region for no reason.

Related

Get the value for multiple <p:rating> [duplicate]

I have a bean with a List<T>:
#Named
#ViewScoped
public class Bean {
private List<Item> items;
private String value;
#Inject
private ItemService itemService;
#PostConstruct
public void init() {
items = itemService.list();
}
public void submit() {
System.out.println("Submitted value: " + value);
}
public List<Item> getItems() {
return items;
}
}
And I'd like to edit the value property of every item:
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{bean.value}" />
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
With this code the value doesn't contain all the submitted values, only the latest submitted value. I also tried <c:forEach> and <h:dataTable>, but it didn't made any difference.
What should I do for collecting all the submitted values?
Your problem is caused because you're basically collecting all submitted values into one and same bean property. You need to move the value property into the bean behind var="item".
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{item.value}" /> <!-- instead of #{bean.value} -->
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
In the bean action method, simply iterate over items in order to get all submitted values via item.getValue().
public void submit() {
for (Item item : items) {
System.out.println("Submitted value: " + item.getValue());
}
}

selectBooleanCheckbox is not set after remoteCommand

I have a remmoteCommand that updates a List, after the items are loaded a repeat render selectBooleanCheckbox. However, the values are not correctly set at the checkbox, all of them are unchecked beside there are some items with selected property (which is Boolean) set to true.
If I just set an Item in the backing bean after the list is loaded it works only for that Item. What could be the problem?
index.xhtml
<h:form>
<h:panelGroup id="itemsHolder">
<h:panelGrid rendered="#{backingBean.items != null}">
<h:selectBooleanCheckbox value="#{backingBean.item.selected}" disabled="#{backingBean.rendered}" /> #{backingBean.item.name}
<ui:repeat value="#{backingBean.items.toArray()}" var="option" >
<h:selectBooleanCheckbox value="#{option.selected}" disabled="#{backingBean.rendered}" />
#{option.name}
<br />
</ui:repeat>
</h:panelGrid>
</h:panelGroup>
<p:remoteCommand rendered="#{backingBean.rendered}"
name="initItems"
actionListener="#{backingBean.initItems()}"
process="#this"
update="itemsHolder"/>
</h:form>
<ui:fragment rendered="#{backingBean.rendered}">
<script>
$(document).ready(function () {
setTimeout(function () {
initItems();
}, 3000);
});
</script>
</ui:fragment>
BackingBean.java
#Component
#ManagedBean
#Scope("view")
public class BackingBean {
private boolean rendered = true;
private Set<Item> items;
private Item item;
public void initItems() {
items = new LinkedHashSet<>();
items.add(new Item("item 1", true, "1"));
items.add(new Item("item 2", false, "2"));
items.add(new Item("item 3", true, "3"));
item = new Item("lonely item", true, "4");
}
//Getters and setters
}
UPDATE:
I created a copy of the real code in a SandBox, I am still not able to reproduce, I think I will have to add more dependencies from real code.
I've recreated your case and it is working just fine with following simple managed bean
#ManagedBean(name = "bean")
#ViewScoped
public class YourBean implements Serializable {
List<YourItem> items=new ArrayList<>();
YourItem anItem=new YourItem();
#PostConstruct
public void init() {
System.out.println("Bean is created");
//initialize items
anItem.setSelected(false);
//add 5 items to list
for (int i=0;i<5;i++){
YourItem item=new YourItem();
item.setSelected(false);
items.add(item);
}
}
public void onUpdateItems(){
System.out.println("Items will be updated...");
anItem.setSelected(true);
//modify 1st and 3rd item
items.get(0).setSelected(true);
items.get(2).setSelected(true);
//remove 4th item
items.remove(3);
}
public List<YourItem> getItems() {
return items;
}
public void setItems(List<YourItem> items) {
this.items = items;
}
public YourItem getAnItem() {
return anItem;
}
public void setAnItem(YourItem anItem) {
this.anItem = anItem;
}
}
p:command should be implemented on following way
<h:form id="form">
<h:panelGroup id="itemHolder">
<h:panelGrid rendered="#{bean.items != null}">
<h:selectBooleanCheckbox value="#{bean.anItem.selected}" />
<ui:repeat value="#{bean.items}" var="item">
<h:selectBooleanCheckbox value="#{item.selected}" />
#{item.selected}
<br />
</ui:repeat>
</h:panelGrid>
</h:panelGroup>
<p:remoteCommand name="startUpdate"
actionListener="#{bean.onUpdateItems()}"
update=":form:itemHolder"/>
<p:commandButton value="Update items" onclick="startUpdate();"/>
</h:form>
Compare your code with this example. Very probably there is problem in your managed bean method for updating items List or with the way how you are updating h:panelGroup.

Can't bind SelectItem list to <f:selectItem

I am using IceFaces components and I am trying to fill up a select with some values that correspond to a MangedBean property.
<h:form>
<ice:selectOneMenu size="1" style="width: 180px">
<f:selectItem value="#{stockManagedBean.listeCategoriesItem}"></f:selectItem>
</ice:selectOneMenu>
</h:form>
listeCategoriesItem is a property of StockManagedBean and is an ArrayList of SelectItem.
#ManagedBean
public class StockManagedBean {
CategorieDAO categorieDAO;
List<SelectItem> listeCategoriesItem;
public StockManagedBean() {
categorieDAO = new CategorieDAO();
listeCategoriesItem = new ArrayList<SelectItem>();
List<Categorie> listeCategories = categorieDAO.selectAllCat();
for(Categorie categorie: listeCategories) {
listeCategoriesItem.add(new SelectItem(categorie.getCatId(), categorie.getCatNom()));
}
}
public List<SelectItem> getListeCategoriesItem() {
return listeCategoriesItem;
}
public void setListeCategoriesItem(List<SelectItem> listeCategoriesItem) {
this.listeCategoriesItem = listeCategoriesItem;
}
}
I tested the values that come from my DAO and they are all correct. I also tested the values of the list in the getter and they are also correct, but when I load my html page, nothing is in the select list...
Use <f:selectItems> instead of <f:selectItem>. Note the s at the end of the former component.
<ice:selectOneMenu size="1" style="width: 180px">
<f:selectItems value="#{stockManagedBean.listeCategoriesItem}" />
</ice:selectOneMenu>
Also, it would be good to also have a field in your bean that will handle the value of the selected item in your selectOneMenu.
<ice:selectOneMenu size="1" style="width: 180px"
value="#{stockManagedBean.selectedCategory}">
<f:selectItems value="#{stockManagedBean.listeCategoriesItem}" />
</ice:selectOneMenu>
And in your managed bean:
#ManagedBean
public class StockManagedBean {
private String selectedCategory;
//rest of your code
//getters and setters...
}

How to get dynamically rendered <h:inputText> value in back end in JSF

I am able to render dynamic but don't know how to get those dynamically created values in back end.
test.xhtml
<h:form>
Insert Your Desire Number : <h:inputText value="#{bean.number}">
<f:ajax listener="#{bean.submitAjax}" execute="#form" render="#form" event="keyup" />
</h:inputText>
<br></br>
<h:outputText value="#{bean.text}" />
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{item.value}" />
</h:column>
</h:dataTable>
<h:commandButton value="Submit" action="#{item.submit}" />
</h:form>
If I render 3 input boxes, and when I submit the button i get the last value only, Can someone guide me how can i
Bean.java
#ManagedBean(name="bean")
#SessionScoped
public class Bean {
private int number;
private List<Item> items;
private Item item;
//getter and setter are omitted
public void submitAjax(AjaxBehaviorEvent event)
{
items = new ArrayList<Item>();
for (int i = 0; i < number; i++) {
items.add(new Item());
}
}
}
Item.java
private String value;
//getter and setter are omitted
public void submit() {
System.out.println("Form Value : "+value);
}
Your submit() method is in the wrong place. You should put it on the managed bean, not on the entity.
Thus so,
<h:commandButton value="Submit" action="#{bean.submit}" />
with
public void submit() {
for (Item item : items) {
System.out.println(item.getValue());
}
}
or more formally,
#EJB
private ItemService service;
public void submit() {
service.save(items);
}

How to collect submitted values of a List<T> in JSF?

I have a bean with a List<T>:
#Named
#ViewScoped
public class Bean {
private List<Item> items;
private String value;
#Inject
private ItemService itemService;
#PostConstruct
public void init() {
items = itemService.list();
}
public void submit() {
System.out.println("Submitted value: " + value);
}
public List<Item> getItems() {
return items;
}
}
And I'd like to edit the value property of every item:
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{bean.value}" />
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
With this code the value doesn't contain all the submitted values, only the latest submitted value. I also tried <c:forEach> and <h:dataTable>, but it didn't made any difference.
What should I do for collecting all the submitted values?
Your problem is caused because you're basically collecting all submitted values into one and same bean property. You need to move the value property into the bean behind var="item".
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{item.value}" /> <!-- instead of #{bean.value} -->
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
In the bean action method, simply iterate over items in order to get all submitted values via item.getValue().
public void submit() {
for (Item item : items) {
System.out.println("Submitted value: " + item.getValue());
}
}

Resources