primefaces selectOneMenu not sorted - java

I have a selectonemenu in my page and i get its contents from my backing bean. But contents of selectonemenu is not sorted.Here is the code...
<p:selectOneMenu id="Department" value="#{search.selectedDepartment}">
<f:selectItem itemLabel="Select Department" itemValue="" />
<f:selectItems value="#{search.departments}" />
</p:selectOneMenu>
and my bean...
public class SearchBean implements Serializable {
private Map<String, Map<String, String>> courseData = new HashMap<String, Map<String, String>>();
private Map<String, String> departments = new HashMap<String, String>();
private String selectedDepartment;
departments.put("department1",
"department1");
departments.put("department2",
"department2");
departments.put("department3", "department3");
departments.put("department4", "department4");
departments.put("department5", "department5");
//getters setters...
}
and contents of selectonemenu is not department1,department2,department3,department4,department5 respectively.It is not sorted.

It's not sorted because HashMap doesn't have a predictable iteration order. Try LinkedHashMap instead.

with LinkedHashMap it is ordered not sorted i.e you get it in order which you put the items, but if you use TreeMap then it is sorteted alphabetically

Related

selectItems from PrimeFaces simply won't work

I'm trying to a populate a selectItems from a query and It simply won't WORK. I've tried with List list, List list, Map<Long,String> list... None of then works... There's no error, the prime faces won't show the list. Simply as that.
<p:selectOneMenu id="pessoa" value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa" itemValue="" />
<f:selectItems
value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#"
/>
</p:selectOneMenu>
Controller:
#Getter
#Setter
#Autowired
private List<Person> pessoaLista;
public void findAllPersons() {
pessoaLista = matriculaService.carregarPessoas();
}
My query function is working, the pessoaLista is populated, the HTML doesn't break. It simply comes blank.
And I've copied from the PrimeFaces docs... (docs)
Use List<Person> and add itemValue="#{pessoa.id}" to your <f:selectItems>
<p:selectOneMenu id="pessoa"
value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa"
itemValue="" />
<f:selectItems value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#{pessoa.id}"/>
</p:selectOneMenu>

Hasmap is not being displayed on selectOneMenu

I've been strugling with selectOneMenu dinamically populated by a HashMap without success for a day now, and can´t find what´s going on.
Followed the steps on
How to populate options of h:selectOneMenu from database?
but still no luck
Here´s my bean:
private Paciente selectedPaciente;
private Map<String, String> itensPacientes;
#PostConstruct
public void init() {
itensPacientes = new LinkedHashMap<String, String>();
itensPacientes.put("1","teste1");
itensPacientes.put("2","teste1");
itensPacientes.put("3","teste1");
}
public Map<String, String> getItensPacientes() {
return itensPacientes;
}
public Paciente getSelectedPaciente(){
return selectedPaciente;
}
public void setSelectedPaciente(Paciente selectedPaciente){
this.selectedPaciente = selectedPaciente;
}
and here's the jsf portion
<h:selectOneMenu value="#{beanAgenda.selectedPaciente}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="--select--" />
<f:selectItems value="#{beanAgenda.itensPacientes}"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
</h:selectOneMenu>
But when I run the code, I can only see the "--select--" option on the combobx.
Is there something I'm overlooking?
Thanks in advance
Try this:
<f:selectItems value="#{beanAgenda.itensPacientes.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
Answer and explanation from this post

JSF: <f:selectItems value="#{Bean.method([abc])}" />

I have a bean and there is cache and a method to get value from it.
public List<SelectItem> getSelectItemList(String key){
return cache.get(key).getValue();
}
May I know how can I get this list in JSF?
I have tried..
<f:selectItems value="#{Bean.getSelectItemList(abc)}" />
but it does not work, because it is expecting a property.
Note: I using JSF 1.2 , EHCache
JSF Code:
<t:selectOneMenu id="testId" value="#{testBean.selectedItem}" >
<f:selectItems value="#{testBean.selectItemList}" />
</t:selectOneMenu>
Manage Bean code:
private String selectedItem;
private List selectItemList;
public List<SelectItem> getselectItemList() {
if(null == selectItemList || selectItemList.isEmpty()){
selectUserList = new ArrayList<SelectItem>();
selectItemList.add(new SelectItem(this.selectedItem, cache.get(this.selectedItem).getValue()));
}
return selectItemList;
}

JSF - UISelectItems problem

Strange error i received from compiler:
Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectOne(siachoice). Found javax.faces.component.UISelectItems.
So, if he was expecting UISelectItems, and found UISelectItems, then where is the error?
My JSP implementation:
<h:selectOneMenu id="siachoice" value="#{dbSelectBean.currentOption}">
<f:selectItems value="#{dbSelectBean.dbs}" />
</h:selectOneMenu>
Method, where i am setting UISelectItem to UISelectItems:
private UISelectItems populateDatabases(String databaseString) {
UISelectItems selects = new UISelectItems();
List<UISelectItem> result = new ArrayList<UISelectItem>();
StringTokenizer tokeniz = new StringTokenizer(databaseString, GlobalConstants.DELIMITER);
while(tokeniz.hasMoreTokens()){
String tempDB = tokeniz.nextToken();
UISelectItem item = new UISelectItem();
item.setItemValue(tempDB);
item.setItemLabel(tempDB);
result.add(item);
}
selects.setValue(result);
return selects;
}
Then, of course, i am setting it to the bean variable dbs.
Help?
You must return a Collection of javax.faces.model.SelectItem
List list = new ArrayList();
list.add(new SelectItem(value, label));
return list;
The <f:selectItems value="#{bean.items}" /> expects one of the following values:
public SelectItem[] getItems() {}
public List<SelectItem> getItems() {}
public Map<String, Object> getItems() {}
The commonly used one is indeed the List<SelectItem>.
Edit: as response to the comment: UISelectItem represents the <f:selectItem> component. The same applies to UISelectItems and <f:selectItems>. E.g.
<f:selectItem binding="#{bean.selectItem}" />
<f:selectItems binding="#{bean.selectItems}" />
which are bound as
private UISelectItem selectItem;
private UISelectItems selectItems;
// +getter +setter
this way you can control the components programmatically -as for every other UIComponent.
<h:form>
<h:selectOneListbox size="5" >
<f:selectItems value="#{userManager.Test()}" />
</h:selectOneListbox>
</h:form>
import javax.faces.model.SelectItem;
import tn.com.ttnproject.entity.*;
#Name("userManager")
#Scope(ScopeType.CONVERSATION)
public class UserManager {
public List <SelectItem> listAllUsersNames;
SelectItem element;
public List<SelectItem> Test(){
listAllUsersNames = new ArrayList<SelectItem>();
for (int i=1;i<=10;i++)
{
element=new SelectItem(
new Integer(i),
i+".00 euros",
"Produit à "+i+".00 euros");
listAllUsersNames.add(element);
}
return listAllUsersNames;
}
}
The problem is UISelectItem is a component clas so it has to be paired with jsf tag by binding attribute. If you want to have pure values you have to use SelectItem(s) classes.

JSF/SEAM: How to pre-select checkboxes in a form

I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...
You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.
Here's a basic example:
<h:selectManyCheckbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Bean:
private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.
public Bean() {
// Preset the selected items.
this.selectedItems = new ArrayList<String>();
this.selectedItems.add("valueToBePreselected1");
this.selectedItems.add("valueToBePreselected2");
// Those values should be exactly the same as one of the SelectItem values.
// I.e. the Object#equals() must return true for any of them.
}
Populate the property you use in "value" before rendereing the page (for example using a phase listener)
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="China" itemValue="China" />
<f:selectItem itemLabel="Germany" itemValue="Germany" />
<f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>

Categories

Resources