Primefaces小部件Var会干扰ui:repeat或ui:datatable [英] Primefaces widgetVar interfering with ui:repeat or ui:datatable
问题描述
我有一个<ui:repeat>
,它在List<String>
上进行迭代,并在<p:lightBox>
中使用当前String的值创建一个<p:commandButton>
.
但是,当我在<p:lightBox>
的属性中添加widgetVar
时,<p:commandButton>
的值始终是上一次迭代的字符串.
I have a <ui:repeat>
that iterates over a List<String>
and creates a <p:commandButton>
with the value of the current String in a <p:lightBox>
.
But when I add widgetVar
to my <p:lightBox>
's attributes the value of the <p:commandButton>
is always the String from the last iteration.
有人可以解释会发生什么,并且(我需要widgetVar
)可以指出解决方案吗?
Can someone explain what happens and (as I need widgetVar
) maybe point out a solution?
这是我的html:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<ui:repeat var="thing" value="#{bugBean.things}">
<p:lightBox widgetVar="whatever">
<h:outputLink>
<h:outputText value="#{thing}" />
</h:outputLink>
<f:facet name="inline">
<h:form>
<p:commandButton action="#{bugBean.writeThing(thing)}"
value="#{thing}" />
</h:form>
</f:facet>
</p:lightBox>
</ui:repeat>
</h:body>
</html>
这是支持bean:
package huhu.main.managebean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class BugBean implements Serializable {
private static final long serialVersionUID = 1L;
List<String> things = new ArrayList<String>();
public BugBean(){
things.add("First");
things.add("Second");
things.add("Third");
}
public void writeThing(String thing){
System.out.println(thing);
}
public List<String> getThings() {
return things;
}
public void setThings(List<String> things) {
this.things = things;
}
}
推荐答案
widgetVar
基本上会生成一个窗口范围的JavaScript变量.您现在在JavaScript上下文中有效执行的操作是:
The widgetVar
basically generates a window scoped JavaScript variable. What you're effectively doing now in JavaScript context is:
window['whatever'] = new Widget(lightboxElement1);
window['whatever'] = new Widget(lightboxElement2);
window['whatever'] = new Widget(lightboxElement3);
// ...
这样,JS中的whatever
变量将仅引用最后一个变量.
This way the whatever
variable in JS would only refer the last one.
基本上,您应该给它们一个唯一的名称,例如,通过添加迭代索引:
You should basically be giving them each an unique name, for example by adding the iteration index:
<ui:repeat var="thing" value="#{bugBean.things}" varStatus="iteration">
<p:lightBox widgetVar="whatever#{iteration.index}">
这种方式变得有效:
window['whatever0'] = new Widget(lightboxElement1);
window['whatever1'] = new Widget(lightboxElement2);
window['whatever2'] = new Widget(lightboxElement3);
// ...
通过这种方式,您可以按whatever0
,whatever1
,whatever2
等引用各个灯箱.
This way you can refer the individual lightboxes by whatever0
, whatever1
, whatever2
, etc.
不相关与具体问题:使用单个灯箱并在每次点击时更新其内容不是更容易吗?
Unrelated to the concrete problem: isn't it easier to use a single lightbox and update its content on every click instead?
这篇关于Primefaces小部件Var会干扰ui:repeat或ui:datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!