p:selectOneMenu在存在noSelectionOption项目时预选择上一个项目 [英] p:selectOneMenu preselects previous item when noSelectionOption item is present
问题描述
添加f:selectItem
时p:selectOneMenu
选择存在问题.
查看:
<p:selectOneMenu value="#{selectionTest.selectedName}">
<f:selectItem itemLabel="Select" noSelectionOption="true" />
<f:selectItems value="#{selectionTest.allNames}" var="varName" itemLabel="#{varName}" itemValue="#{varName}" />
</p:selectOneMenu>
型号:
private List<String> allNames;
private String selectedName;
public MenuSelectionTestBean(){
allNames = new ArrayList<String>();
allNames.add("Ahmed");
allNames.add("Mohamed");
allNames.add("Ibrahim");
allNames.add("Walid");
selectedName ="Walid";
}
结果:
应选择"Walid"项目,但应选择"Ibrahim".我认为PrimeFaces会根据列表中的索引而不是项目的值来选择该项目.
The item "Walid" should be selected, however "Ibrahim" is selected instead. I think that PrimeFaces selects the item depending on its index in the list and not the value of the item.
这是怎么引起的,我该如何解决?
How is this caused and how can I solve it?
推荐答案
我可以重现您的问题.我查看了PrimeFaces源代码.这是PrimeFaces SelectOneMenuRenderer
中的一个错误,该错误仅在使用带有noSelectionOption="true"
的项目时才公开.渲染器不会为此生成背景<select>
元素的HTML <option>
元素,但会为此生成可视<ul>
列表的<li>
.换句话说,在您的情况下,<select>
包含4个项目,而<ul>
包含5个项目. JavaScript会根据您怀疑的<option selected>
索引选择<li>
.那解释了你的问题.
I could reproduce your problem. I looked in the PrimeFaces source code. It's a bug in PrimeFaces SelectOneMenuRenderer
which exposes only when an item with noSelectionOption="true"
is been used. The renderer won't generate the HTML <option>
element of the backing <select>
element for that, but it will generate the <li>
of the visual <ul>
list for that. In other words, the <select>
contains in your case 4 items and the <ul>
5 items. The <li>
is selected by JavaScript based on the index of the <option selected>
, exactly as you suspected. That explains your problem.
您最好的选择是将其报告为错误给PrimeFaces家伙.渲染器还应该为此生成<option>
,或者JavaScript应该基于值而不是基于索引选择选项.
Your best bet is to report it as a bug to PrimeFaces guys. The renderer should also generate the <option>
for that, or the JavaScript should select the option based on value instead of on index.
同时,将JSF配置为将空字符串提交的值解释为null
时,用itemValue="#{null}"
替换noSelectionOption="true"
,或者用itemValue=""
替换itemValue=""
应该可以解决您的问题.
In the meanwhile, replacing the noSelectionOption="true"
by itemValue="#{null}"
, or also itemValue=""
when JSF is configured to interpret empty string submitted values as null
, should solve your problem.
这篇关于p:selectOneMenu在存在noSelectionOption项目时预选择上一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!