I have asked this question several times in this and various other forums, but still unable to implement it in my code.
I am doing this example , and i need to add a listbox (like in the column MANUFACTURE).
i am unable to display the Listbox or populate it with values from my Java class.
My java code looks like this;
private List<Hotel> listHotel;
public List<Hotel> ListAllHotels() {
return dml.displayAllHotels(); //dml.displayAllHotels() returns a List<Hotel>
}
Normally i create a listbox and populate it with values using the following JFS code;
<h:selectOneMenu value="#{HotelDataForm.stationedHotel}" id="globalFilter" onchange="carsTable.filter()" >
<f:selectItems value="#{HotelDataForm.ListAllHotels}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" />
<h:outputText value="#{carsTable[1]}" />
</h:selectOneMenu>
And this works, but i am unable to add this code to the Manufacturer column in the link i posted. In the example they make use of SelectItem[] object to populate the listbox. I am clueless as in how to add and populate values to the manufacturer column in my program.
This is from the example on Page 131 of the PrimeFaces 2.2 Guide
If you’d like to use a
dropdown instead of an inputtext to only allow predefined filter values use filterOptions attribute
and a collection/array of selectitems as value. In addition, filterMatchMode defines the built-in
matcher which is startsWith by default. Following is an advanced filtering datatable with these
options demonstrated.
<p:column
filterBy="#{car.manufacturer}"
headerText="Manufacturer"
filterOptions="#{carBean.manufacturerOptions}"
filterMatchMode="exact">
<h:outputText value="#{car.manufacturer}" />
</p:column>
So in this example, the carBean should have a method getManufacturerOptions() that returns either SelectItem[] or List<SelectItem> containing all the values that should be in the filter dropdown list.
REFERENCE: Javadoc for SelectItem
Related
I'm stuck on this current Problem, maybe somebody can help me out on this:
I try to build a dynamic Ruleseditor based on JSF 2.2 and the latest primefaces version (5.0).
Therefor I construct a p:dataTable which display rule values in seperate columns. To edit the values the Editor generates a pickable Valuelist from the used database table.
Everything works fine but if I try to select another value in the pickable value list and click the save Button the old Value is submitted to the bean and the h:selectOneMenu display the last used value.
I tried many solutions but this is the current on JSF Side:
<h:form id="ruleTableForm">
<p:dataTable value="#{table.rules}" var="rule">
<p:columns var="column" value="#{table.columns}" headerText="#{column.header}" width="250">
<h:selectOneMenu id="columnSelectMenu" value="#{rule.ruleColumnToValueMap[column].value}">
<f:selectItems value="#{column.pickList.entrySet()}"
var="pickItem" itemLabel="#{pickItem.value}"
itemValue="#{pickItem.key}" />
</h:selectOneMenu>
</p:columns>
</p:dataTable>
<div class="button_group">
<h:commandLink styleClass="add_button" action="#{regelEditorViewController.saveRuleset()}">Save</h:commandLink>
</div>
</h:form>
The parts used in the xhtml-File from my ViewController look like this:
#ManagedBean
#ViewScoped
public class RegelEditorViewController implements Serializable {
private RegelEditorViewData viewData;
#Produces
public RegelEditorViewData getViewData() {
if (this.viewData == null) {
this.viewData = this.viewDataService.produceViewData(0L,20L);
}
return this.viewData;
}
public void saveRuleset() {
RulesetViewData rulesetViewData = this.viewData.getCurrentRuleset();
this.viewDataService.saveRuleset(rulesetViewData);
this.refreshViewData();
}
}
The ruleColumnToValueMap is a Map:
private Map<RuleTableColumnViewData, Value> ruleColumnToValueMap = new LinkedHashMap<RuleTableColumnViewData, Value>();
...and the "pickList" in the "ruleTableColumnViewData"-object is also a Map with Strings:
private Map<String, String> pickList = new LinkedHashMap<String, String>();
The solutions I tried and but do not worked:
work with an ajax setter, which select the value (but the value is null in the setter method)
<f:ajax listener="#{regelEditorViewController.selectedRuleColumnValueChanged(table, rule, column, rule.ruleColumnToValueMap[column].value)}" render="#form" />
work with an immediate ajax setter, which sends always the last setted value to the bean:
<f:ajax listener="#{regelEditorViewController.selectedRuleColumnValueChanged(table, rule, column, rule.ruleColumnToValueMap[column].value)}" immediate="true" render="#form" />
tried to work with p:selectOneMenu but it seems to be very buggy: it is not possible to change the selected item, the component do not show a menu
tried to work with "SelectItem" in a "pickList" List and a "SelectItem" for the column in the "ruleColumnToValueMap" Map
Maybe someone have a possible solution that can help me?
The example above this answer is working correctly, the failure was that the controller do not save the data to the database!
I built a Relatively simple DataTable and tried to use the filter feature with also using the pagination feature.
in reference to the primefaces showcase i created a column for each field in my Customer class.
this is my "Controller" Bean:
#SessionScoped
#Named
public class CustomerListController implements Serializable{
public static final long serialVersionUID = //UID;
private List<Customer> filteredCustomers;
private List<Customer> allCustomers;
public CustomerListController(){
//some Class that generates a list of sufficiently many
//dummy customers on instantiation
this.allCustomers = new CustomerListProducer().getCustomers();
}
public List<Customer> getFilteredCustomers{
return this.filteredCustomers;
}
public void setFilteredCustomers(List<Customers> list){
this.filteredCustomers = list;
}
public List<Customer> getAllCustomers(){
return this.allCustomers;
}
}
i use following dataTable to render this:
<p:dataTable paginator="true" rows="18" scrollRows="15" scrollable="true"
scrollHeight="500" var="customer" value="#{customerListController.allCustomers}"
scrollable="true" id="customerTable"
filteredValue="#{customerListController.filteredCustomers}" widgetVar="table">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<h:inputText id="globalFilter" onkeyup="table.filter()" />
</p:outputPanel>
</f:facet>
<p:Column id="nameColumn" filterBy="name" sortBy="name"
headerText="Customer" filterMatchMode="contains">
<h:outputText value="#{customer.name}" />
</p:Column>
<!-- Some more columns in the exactly same
manner as this changes only in Customer attribute-->
</p:dataTable>
When i press any Key in any given filter field the Table loses all rows and even upon clearing the fields does not display any.
When refreshing the page i get the expected amount of rows & pages.
I will try to provide amendments as requested.
EDIT:
I am using Primefaces version 4.0.0 as installed with maven.
I have been digging into console under FF and found the following:
The response XML is empty save the node Entry for updated table. There are no JavaScript errors thrown and the viewstate id sent with the "table data" changes with every keystroke.
Your filterBy and sortBy must contain a deferred EL expression.
<p:Column id="nameColumn" filterBy="#{customer.name}" sortBy="#{customer.name}"
headerText="Customer" filterMatchMode="contains">
<h:outputText value="#{customer.name}" />
</p:Column>
UPDATE:
Since I could confirm that both EL and non EL approach works in PF V4.0, here is another possible answer to your problem:
Please check your SessionScoped import.
I could not make it work using the javax.faces.bean.SessionScoped.
Using javax.enterprise.context.SessionScoped it worked.
Personally I avoid as much as possible the usage of SessionScoped scope, try to use the ConversationScoped instead. It will make a better usage of your server resources.
Replace your code with
public CustomerListController(){
//some Class that generates a list of sufficiently many
//dummy customers on instantiation
this.allCustomers = new CustomerListProducer().getCustomers();
this.setFilteredCustomers(this.getAllCustomers());
}
No value is provide to the filterAttribute therefore you see a blank dataTable when you do filter.
Hope this help's
This is my second day working with JSF. Have no previous background in Java , Have Been working with Flex and C++ for quite some time. Some history so that everybody knows where im coming from . For a "rush" project i am running into an issue
<h:panelGroup id="txeTab" layout="block" class="txeTab">
<h1>TXE</h1>
<h:form id="txeForm">
<h:panelGrid columns="3">
<c:forEach items="${txeConfBean.getListTable()}" var="property">
<h:outputLabel id="key" value="${property.key}"/>
<h:inputText id="value" value="${property.value}" />
<h:commandButton value="Change" action='${txeConfBean.setProperty('key','value')}'/>
</c:forEach>
</h:panelGrid>
</h:form>
</h:panelGroup>
and The Bean is as follows
public HashMap <String,String> getListTable ()
{
String[] keys = new String[super.keyData.size()];
HashMap <String,String> retKeys = new HashMap <String, String>();
super.keyData.toArray(keys);
for (int i=0;i<keys.length;i++)
{
if(!keys[i].isEmpty())
{
retKeys.put(keys[i],getProperty(keys[i]));
}
}
return retKeys;
}
im able to display the Key,value pairs recursively. But i want to update a specific key with new value once someone updated the h:inputText id="value" value="${property.value}" /> and press the command button the new value is written to. Need help in this regard . Googling it make me feel there are too many ways to do it. Need help. I am just unable to figure out what to pass to ${txeConfBean.setProperty('key','value')} How can i pass the value of both InputText and OutPutText to setProperty ?
The ${}, which is inherited from legacy JSP, can't do a "set" operation on a property. It can only do a "get" operation on the property.
If you want to support both "get" and "set" on a property, you need the #{}. Even more, in general in JSF, you should not use ${} anymore. Further, in order to get/set a map value by an input component, you have to reference the map value by its key using the brace notation as #{bean.map[key]}.
<h:form id="txeForm">
<h:panelGrid columns="3">
<c:forEach items="#{txeConfBean.listTable}" var="property">
<h:outputLabel id="key" value="#{property.key}"/>
<h:inputText id="value" value="#{txeConfBean.listTable[property.key]}" />
<h:commandButton value="Change" />
</c:forEach>
</h:panelGrid>
</h:form>
Note that the command button action is omitted. JSF/EL will "automagically" already call the map's put() method when the model value is about to be updated. It's unnecessary in this particular construct. Also note that in this construct, the entire form is submitted. You might want either to put the command button outside the table, or to use <f:ajax> to submit the current "row" only.
See also:
Difference between JSP EL, JSF EL and Unified EL
Unrelated to the concrete problem: you're doing the business job in a getter method. This is extremely inefficient in case the property is referenced in iterating components. Do the business job in (post)constructor instead. It'll be invoked only once. This way you can make the getter really a fullworthy getter.
public HashMap<String,String> getListTable() {
return listTable;
}
See also:
JSF calling setter & getter multiple times.
I am trying this example in PrimeFaces. I understand only the first few lines of the code.
<p:dataTable var="car" value="#{tableBean.carsSmall}"
emptyMessage="No cars found with given criteria">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="carsTable.filter()" style="width:150px" />
</p:outputPanel>
</f:facet>
It could display a search box here. The reaming lines of code would be to add the column and populate the columns with data. I don't understand what
<p:column filterBy="#{car.model}"
headerText="Model" footerText="contains"
filterMatchMode="contains">
<h:outputText value="#{car.model}" />
</p:column>`
What is #{car.model} ? it doesn't specify anything call model in the java class. How do I alter my java class to make a column display?
The expression variable car is declared to be the var attribute of the dataTable. This means that each unique row in the dataTable component can be referenced in expression language by the variable car.
The model property of car is a Bean property of the Serializable POJO Car. It is assumed that the Car class has a property model meaning a getter getModel() and a setter setModel().
The filterBy attribute of <p:column> specifies that this column header will have its own unique filter text field and that it will filter the rows on car.model property.
The attribute filterMatchMode specifies that the match criteria is contains which means any textual occurence of what is typed into the column filter field will equate as a matched record. See the Primefaces Guide for a complete list of filterMatchMode options.
private List<Car> carsSmall;
carsSmall is a list that contains Car objects. Car is imported here:
import org.primefaces.examples.domain.Car;
Car.java Source
Car is the backing bean, it has an attribute model that contains the car's model as a String.
In car #{car.model} is defined here:
<p:dataTable var="car"...>
The dataTable iterates over every element in the list carsSmall and you can access the current element using the name given in the var attribute (here: car). So #{car.model} calls the getModel() method of the current Car object.
hi my dropdown has some 3 values
1)Apple
2)mango
3)grape
when my page loads my dropdown is defaulted to Apple(first value)
how can i make dropdown defaulted to empty and when i clicked on dropdown it should show the values
in java jsf (i am getting dropdown values as List)
I would just add an empty option, and validate as needs be.
I generally add an empty field with a null ID and a value that says something like "Select Fruit". I believe you can have both selectItem and selectItems. So something like this should work inside your JSF selectOneMenu component:
<f:selectItem itemValue="" itemLabel="Select Fruit" />
<f:selectItems value="#{fruitList}" />