Primefaces Lightbox的widgetVar干扰ui:重复

Les*_*ter 3 primefaces jsf-2

我有一个<ui:repeat>迭代a List<String><p:commandButton>使用当前String的值创建一个<p:lightBox>.
但是当我添加widgetVar到我<p:lightBox>的属性时,它的值<p:commandButton>始终是最后一次迭代的String.

有人可以解释发生了什么,并(我需要widgetVar)可能指出一个解决方案?

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是支持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;
   }

}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 8

widgetVar基本上生成作用域JavaScript变量的窗口.您现在在JavaScript上下文中实际做的是:

window['whatever'] = new Widget(lightboxElement1);
window['whatever'] = new Widget(lightboxElement2);
window['whatever'] = new Widget(lightboxElement3);
// ...
Run Code Online (Sandbox Code Playgroud)

这样whateverJS中的变量只会引用最后一个变量.

您基本上应该为每个人提供一个唯一的名称,例如通过添加迭代索引:

<ui:repeat var="thing" value="#{bugBean.things}" varStatus="iteration">
    <p:lightBox widgetVar="whatever#{iteration.index}">
Run Code Online (Sandbox Code Playgroud)

这样它变得有效:

window['whatever0'] = new Widget(lightboxElement1);
window['whatever1'] = new Widget(lightboxElement2);
window['whatever2'] = new Widget(lightboxElement3);
// ...
Run Code Online (Sandbox Code Playgroud)

这样,您就可以通过参考个人的灯箱whatever0,whatever1,whatever2,等.


具体问题无关:使用单个灯箱并在每次点击时更新其内容是不是更容易?