3

所以我在集成纯 JavaScript 和 primefaces 对话框组件时遇到了问题。用户应该获得一个覆盖对话框(单个)作为对许多动态 JavaScript 生成的 Div 元素(多个)的单击响应。对话框的内容应取决于用户单击的 div 元素。

模拟该问题的简化结构如下:

javascript(谷歌关闭):

// this code is in a loop that creates multiple div elements
// in the page dom structure.

var iconDiv = goog.dom.createDom('div', {
        'id': nodeId, // **
        'class': 'icondiv',
        'style': iconDivStyle   // **
    }, goog.dom.createDom('img', {
        'src': 'layer_icons/unused.png',
        'class': 'iconDivImg',
        'style': 'width:100% ;position:absolute; height: auto; '
    }));

       goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
    // in an ideal situation, the stringField is set to 'this.id' here ...
            dlg1.show();
        });

//** these are the code lines that mark the difference between the
//   divs created in the loop.

primefaces 对话框:

这是当用户单击在上述 javascript 中创建的任何图标时显示的对话框组件。

<p:dialog id="DialogId" dynamic="true" widgetVar="dlg1"> 
    <h:form id="formId">
    <p:outputLabel id="clickedOnDiv_Id" value=" #{managedBean.stringField}"/>
<p:outputLabel id="somePropertyOfThisDiv_Id" value="#{managedBean.stringFieldSetByMethod}"/>
    </h:form>
</p:dialog>

托管豆:

class managedBean {

private String stringField ;  // getter and setter

private String stringFieldSetByMethod ;  // getter and setter

public void method(){

// uses the stringField in some way to set 'stringFieldSetByMethod' ...

}

}

以上是我想要实现的,如果你已经想出了实现的方法,请指教。

接下来是我到目前为止所尝试的:

我添加了以下 javascript 函数:

populateDialog = function(divId){
document.getElementById('formId:hiddenInput').value = divId;
document.getElementById('formId:hdnBtn').click();
}

因此 onClick 事件处理程序更改为以下内容:

goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
divId = this.id;   // where divId is a variable accessible to populateDialog()
dlg1.show();
        });  // this is inside a loop, so it is set for each created div

然后我将 populateDialog() 添加为对话框的 OnShow 回调,并将其更改为:

<p:dialog id="DialogId" dynamic="true" widgetVar="dlg1" onShow="populateDialog();">
       <h:form id="formId">
             <h:inputHidden id="hiddenInput" value="#{nodesBean.stringField}" />
             <p:commandButton id="hdnBtn" ajax="true" actionListener="#{managedBean.method()}" style="display: none;" update=":formId"/>

        <p:outputLabel id="clickedOnDiv_Id" value="#{managedBean.stringField}"/>
        <p:outputLabel id="somePropertyOfThisDiv_Id" value="#{managedBean.stringFieldSetByMethod}"/>
       </h:form>
</p:dialog>

结果是 managedBean.method 永远不会被调用,并且在加载对话框时所有字段都是空的,我可以跟踪 JavaScript 的进度,直到调用 populateDialog(),divId 是正确的,并且我没有 JavaScript 错误. 然而,服务器对我正在做的所有客户端驼峰和跳跃完全一无所知,非常感谢对真正发生的事情的解释。

提前致谢!

4

1 回答 1

2

所以解决方案是使用:

在对话框中:

`<h:inputHidden id="hiddenInput" value="#{nodesBean.stringField}" />
<p:remoteCommand name="remoteCommandFunction" process="hiddenInput" update=":formId"/>`

在 onclick 处理程序中(每个动态创建的 div)

goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
divId = this.id;   // where divId is a variable accessible to populateDialog()
dlg1.show();
    });  // this is inside a loop, so it is set for each created div

称为对话框 onshow 回调的 javascript 函数:

populateDialog = function(divId){
document.getElementById('formId:hiddenInput').value = divId;
remoteCommandFunction(); // this is cool cause it solves my problem, not cool cause you really have to know what you're looking for in the documentation to find it >:-/
}

最后,在托管 bean 中:

class managedBean {

private String stringField ;  // getter and setter

private String stringFieldSetByMethod ;  // getter and setter

 public void setStringField(String sf){
         this.stringField = sf;

         method();
      }

public void method(){

// uses the stringField in some way to set 'stringFieldSetByMethod' ...

                   }

}
于 2013-07-02T16:11:10.403 回答