Dynamically set itemValue of selectOneMenu in BootsFaces not working - jsf

I am Developing such application in which there is Hibernate Many to One Relationship between Employee and Designation. While Inserting New Employee, I am Creating Dynamic Options(which comes from Designation Table present in Database) of Select Menu for Designation. I can Create options as follow in xhtml:
<b:selectOneMenu value = "#{loginBean.designationVo}">
<f:selectItems value = "#{loginController.designationListByInsert()}" var = "field" itemLabel = "#{field.designation}" itemValue = "#{field.designationId}"/>
</b:selectOneMenu>
In Above Code, designationListByInsert() method will return List of Designation, Now I want to set DesignationName as itemLabel so user can see only Name of Designation along with it I want DesignationId as itemValue, but I can't set itemValue as shown in above code. It takes index value of List as itemValue. If I set DesignationId as itemLabel the it shows correct value but at same time If I Assign it to itemValue then it doesn't work. I can't understand what's wrong in my code. Please Someone help me to make it correct.

Try this it will surely work.
Replace
itemValue = "#{field.designationId}"
by
itemValue = "#{field}"

Related

PrimeFaces datatable default sortBy from backing bean

I have a data table with a POJO object:
<p:dataTable id="table" var="object" sortBy="#{object.name}" sortOrder="DESCENDING">
object has fields id, name, date, size for example. I am able to set default sort field using xhtml, but I want set it from backing bean.
I am able to parse column id when user creates sort request for example name.
public void sortEventListener(AjaxBehaviorEvent actionEvent) {
String id = ((SortEvent) actionEvent).getSortColumn().getColumnKey();
String[] idPath = id.split(":");
sortBy = idPath[idPath.length - 1];
sortOrder = ((SortEvent) actionEvent).isAscending();
}
My task detects which column user wants to sort and persists it to db. After reload the data table should be sorted by this column.
When I set
sortBy="#{bean.sortBy}" // sortBy = name
it's not working and data table is not sorted after rendering the page.
Please help.
If you bind your data table to a org.primefaces.component.datatable.DataTable object in your bean or find the table component in your bean, you can use the table object to set the sortBy value expression programmatically.
To get an idea how PrimeFaces is handling sorting, you can have a look at the source code at GitHub.
When you have the sort column, you can easily get the sort value expression. So, in your listener you could use something like:
UIColumn sortColumn = sortEvent.getSortColumn();
ValueExpression sortByVE = sortColumn.getValueExpression("sortBy");
By the way, you can replace the parameter type AjaxBehaviorEvent with SortEvent in your sort listener method.
Now, store the sortByVE expression, and set it as the sortBy value expression of the bound table when needed:
dataTable.setValueExpression("sortBy", sortByVE);
If you want to create the value expression from scratch, use something like:
FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory ef = context.getApplication().getExpressionFactory();
ValueExpression ve = ef.createValueExpression(context.getELContext(),
"#{object.name}",
Object.class);
dataTable.setValueExpression("sortBy", ve);
In this example "#{object.name}" is fixed. You should construct it based on the value you get from your sort listener.
If you want to find your table in your bean, OmniFaces Components might be helpful. It also offers a shortcut method to create value expressions.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?
Programmatically getting UIComponents of a JSF view in bean's constructor
How do I set the value of HtmlOutputTag in JSF?

xp:selectItem value property throws Conversion Error

Within xp:RadioGroup there is xp:selectItem which requires itemLabel.
Data-Properties of xp:selectItem is either itemValue or value:
itemValue - is the value returned to the server
value - specifies the contest of the selection item (Can be literal string or expression)
Loading values in both value & itemValue throws an error e.g.
<xp:radioGroup id="radioGroup"
layout="lineDirection"
dojoType="dijit/form/RadioButton"
styleClass="zmdi">
<xp:selectItem itemLabel='ssh'
itemValue="serverValue"
value="SelectionContentsValue">
</xp:selectItem>
</xp:radioGroup>
The error thrown is:
Exception
Conversion Error setting value ''{0}'' for ''{1}''.
java.lang.IllegalArgumentException: Conversion Error setting value ''{0}'' for ''{1}''.
com.sun.faces.util.Util.getSelectItems(Util.java:489)
com.sun.faces.renderkit.html_basic.SelectManyCheckboxListRenderer.encodeEnd(SelectManyCheckboxListRenderer.java:130)
com.ibm.xsp.renderkit.html_basic.RadioRenderer.encodeEnd(RadioRenderer.java:48)
com.ibm.xsp.renderkit.ReadOnlyAdapterRenderer.encodeEnd(ReadOnlyAdapterRenderer.java:180)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:1005)
com.ibm.xsp.component.UISelectOneEx.encodeEnd(UISelectOneEx.java:331)
com.ibm.xsp.util.FacesUtil.renderComponent(FacesUtil.java:858)
Removing the [ value="SelectionContentsValue" ] attribute from the xp:selectItem renders the radioGroup without issue.
ideas? thx
The value property of the is intended to be evaluated to something that is a UISelectItem.
Here, you are assigning it to a String "SelectionContentsValue".
During rendering, the Renderer asks the radioGroup "Please give me your all your selectItems".
It does this by iterating through it's children, and checking if each child is a UISelectItem.
For each UISelectItem that it finds, it evaluates the value property.
If value evaluates to null, it will then check for itemLabel, itemValue etc. and create the selectItem using these properties.
else if value evaluates to a UISelectItem, it will use this UISelectItem
else If value evaluates to something that is not a UISelectItem, it throws an IllegalArgumentException
Your situation is throwing the IllegalArgumentException because you have assigned a String to the 'value' property instead of a UISelectItem
So you should either use the itemLabel, itemValue properties and manually set those options that way. Or instead you can use the value property to compute the selectItem to one that has been prepared in some other place, or is dynamically loaded e.g. a managedBean or scoped variable.
I hope this helps let me know if any questions
Thank you Cameron, you comment sparked an idea to go track down, which I found here:
IBM Domino Designer> IBM Domino Designer 9.0.0> IBM Domino Designer User Guide> Designing XPages applications> Adding controls> Control reference selectItems - Selection Items
» Checkboxes can each checked "true", no mutual exclusivity with checkboxes.
Checkboxes are kinda like Hippies, "It's all cool man".
» Radio buttons are much more finicky:
» Radios require itemLabel with itemValue aliase optional, for each selection option:
» The 'value' data-property is for checkboxes. Value contains pipe-delimited selection values, in an array/vector of checkbox choices.
<xp:checkBoxGroup id="checkBoxGroup1" value="#{document1.fruit}">
<xp:selectItems>
<xp:this.value>
<![CDATA[${javascript:["Apples|apples|Garden of Eden|false","Oranges|oranges"]}]]>
</xp:this.value>
</xp:selectItems>
</xp:checkBoxGroup>

How to use the domino view entries tree node in the extension library outline

I've set var = ViewEntries2 viewName = ApplicationProfiles
If my first column's name = Xpage I've tried setting labelcolumn = myview.Xpage and I get an error. If I use labelColumn="1" I get empty nodes. I can't seem to find much documentation.
<xe:basicContainerNode
label="Créer nouvelle requête">
<xe:this.children>
<xe:dominoViewEntriesTreeNode
var="ViewEntries2" viewName="ApplicationProfiles" labelColumn="1" loaded="true"
rendered="true">
</xe:dominoViewEntriesTreeNode>
</xe:this.children></xe:basicContainerNode>
Any help would be appreciated.
Try setting labelColumn to the programmatic name of the view column (which can be a number such as $1 or can be a name - check the advanced tab of the view column properties).

Is it possible to use custom JSF converter for UISelectOne, depending on value of Bean property or UIInputText value?

I need to get help with a problem.
I have an application, which displays data from database.
In database I am using one column for identifying an object with changes in time (version of object).
I mean that rows have different IDs, but same OBJECT_ID.
They are also differentiated by validity DATE columns (VALID_FROM and VALID_TO), so i can find the proper version of object in selected time.
When page is shown to user, he sets DATE for displaying data.
There is form on page, with Select Boxes (their items are object from database).
I need to show only items, that satisfy the validity condition.
Everything is OK before form handling.
I am using custom converter, that converts String value (ID of row in database table) to Object of my model, but I need two values for row to find. (ID and DATE from user form).
Converter method getAsObject gets only one VALUE parameter (ID from Select Box).
How to get second parametr to work?
Can i find it from Bean or from JSF input object?
If there is some way that includes using <f:attribute/>, is there way to set <f:attribute/> by Java code in Bean? (Form is generated by bean method).
I am sorry for my English, i try my best :o)
P.S.: Date parameter is important in sqlMap because of JOIN used. I search by ID, but other tables are joined by OBJECT_ID (version) => and i need to get one row only
Thanks for answers and have a nice day
Worsik
Edit:
Select Boxes are generated by this code:
uiInput = new HtmlSelectOneMenu();
UISelectItems items = new UISelectItems();
items.setValueExpression("value", createValueExpression(
"#{myBean.referencedList." + item.getName() + "}",
List.class));
uiInput.getChildren().add(items);
Converter method looks like this:
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if (value.equals("")) {
return null;
}
Class entityType = component.getValueExpression("value").getType(
FacesContext.getCurrentInstance().getELContext());
// TODO: need to get date value from form
Date day = new Date();
return myService(context).findEntityById(entityType, Long.valueOf(value), day);
}
entityType is Class of model for object from database
myService calls Dao object and sqlMap is called
select * from object_table as t1
JOIN other_table as t2 ON t1.object_fk = t2.object_id // (or version_id)
where t1.id = #id:INTEGER#
and t2.valid_from <= #day:DATE#
and t2.valid_to >= #day:DATE#
I figured out how to do this by <f:attribute/> by code in converter:
Date day = (Date) component.getAttributes().get("dateForSearch");
and in bean method after generating SelectBox with code:
uiInput.getAttributes().put("dateForSearch", getSelectedDate());
and in pages without dynamic generating of forms i used:
<SelectOneMenu>
...
<f:attribute name="dateForSearch" value="#{myBean.selectedDate}" />
</SelectOneMenu>
I hope that this will not be only self answered question and will help somebody else

Is this a bug in primefaces autocomplete?

I'm trying to put an autocomplete that fetches suggestions as a list of Entry<String, Integer>
<p:autoComplete completeMethod="#{suggester.suggestTopics}"
var="x1" itemLabel="#{x1.key}" itemValue="#{x1.value.toString()}"
value="#{topicController.selected}" />
Manged bean code is as follows:
private int selected;
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected= selected;
}
But this fails saying the Integer class doesn't have method/property named key. If I remove the value attribute from autocomplete then it starts working properly. But when I put value attribute it starts expecting that the object inside var should be of the same type as that inside value attribute. I believe/expect it should be that the object inside itemValue should be of the same type as that inside value attribute.
I want to use POJOs for suggestions but pass just the entity Id to the value
Using :
Primefaces 3.1
JSF 2.1.6
I believe/expect it should be that the object inside itemValue should
be of the same type as that inside value attribute.
Yes this makes sense, and it is the same in the primefaces showcase:
<p:autoComplete value="#{autoCompleteBean.selectedPlayer1}"
id="basicPojo"
completeMethod="#{autoCompleteBean.completePlayer}"
var="p" itemLabel="#{p.name}" itemValue="#{p}"
converter="player" forceSelection="true"/>
As you see is var="p" and itemValue="#{p} where p is an instance of Player. And selectedPlayer1 is also an instance of Player.
I don't know if it works with a Map since the Primefaces example is called "Pojo support" and the suggestions should be a List of elements of the same type as in the value attribute.
I think you want to use the Simple auto complete , but instead you looked at the wrong example on the showcase of the Pojo Support
x1 refers to the int selected - while it expect to be referred to a POJO (with key and value properties.) , that's why you get the message
Integer class doesn't have method/property named key
Or simple use the Simple auto complete
As commented to Matt you dont need to rebuild Player(Pojo) from Db. You can set simply id property of Player(Pojo) and in action method may be utilize this id to fetch it from DB.
In your case in convertor you might do
Entry<String, Integer> e = new Entry<String, Integer>();
e.setId(value) // where value is passed in to convertor in method getAsObject.....
This value will be set to private Entry<String, Integer> selected
I have used Pojo autocomplete but not tried with generic classes.
Hope this helps.
I know the question is outdated but I've had the same problem.
The point is that you have to assign var to p (var="p"). I think it's terribly unobvious (documentation doesnot mention it has to be that way) 'cause I thought I can assign any var name I want.

Resources