【问题标题】:Primefaces widgetVar interfering with ui:repeat or ui:datatablePrimefaces widgetVar 干扰 ui:repeat 或 ui:datatable
【发布时间】:2012-11-02 16:45:59
【问题描述】:

我有一个 <ui:repeat> 迭代 List<String> 并创建一个 <p:commandButton><p:lightBox> 中的当前字符串的值。
但是当我将widgetVar 添加到<p:lightBox> 的属性中时,<p:commandButton> 的值始终是上次迭代的字符串。

有人可以解释发生了什么并且(我需要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>

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

}

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    widgetVar 基本上生成一个窗口范围的 JavaScript 变量。你现在在 JavaScript 上下文中有效地做的是:

    window['whatever'] = new Widget(lightboxElement1);
    window['whatever'] = new Widget(lightboxElement2);
    window['whatever'] = new Widget(lightboxElement3);
    // ...
    

    这样JS中的whatever变量只会引用最后一个。

    您基本上应该给它们一个唯一的名称,例如通过添加迭代索引:

    <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);
    // ...
    

    这样您就可以通过whatever0whatever1whatever2 等引用各个灯箱。


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

    【讨论】:

    • 太棒了!感谢您的启发性回答和质疑我的设计。 ;) 当然,只有一个更新内容的灯箱会更有效。我会那样做。打开灯箱的链接已经在灯箱标签内,这让我很恼火——现在我意识到这不是障碍。还有一个问题:我在哪里可以(重新)阅读您刚刚向我指出的关于我的问题的内容?
    • 不客气。我刚刚观察了生成的 HTML 代码(在这种特殊情况下,通过查看 JSF 代码,我已经从头顶知道了)。
    【解决方案2】:

    最近我在升级到 primefaces 4.0 -> 5.1 时遇到了类似的问题

    我不得不使用语法:

    PF('whatever0')
    

    由于 primefaces 命名 widgetvar 的方式发生了变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多