且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JavaScript中获取primefaces widgetVar并进行更新

更新时间:2023-02-26 18:26:57

PrimeFaces 3.5的id和widgetVar之间没有映射.在3.5中,将小部件添加到窗口"对象中,而不存储在其他位置.这意味着widgetVar已经是您可以直接使用的变量.

There is not mapping between the id and a widgetVar for PrimeFaces 3.5. In 3.5 the widgets are added to the 'window' object and not stored somewhere else. This means that the widgetVar is already directly a variable u can use.

<p:panel id="errorsPanel" widgetVar="errorsPanelWidget">

3.5/4.0:(在4.0中仍然有效,但不推荐使用PF('..')方式) errorsPanelWidget.close()

3.5/4.0: (in 4.0 this still works but is deprecated in favour of the PF('..') way) errorsPanelWidget.close()

5.0及更高版本: PF('errorsPanelWidget').close()(除非您配置传统模式,否则它将表现为3.5/4.0)

5.0 and up: PF('errorsPanelWidget').close() (unless you configure the legacy mode after which it will behave like 3.5/4.0)

然后最简单的方法是使用一种约定,即您将小部件名称命名为id,但后缀为id_widget.您可以(建议)将widgetVar留在外面,然后该widget与id相同(在某些情况下,这可能会引起类结,这就是PF放弃此约定的原因).

Easiest is then to use a convention that you name your widgetVar like your id but with a postfix e.g. id_widget. You could (advise against it) leave the widgetVar out and then the widget is idententical to the id (this can cause classhes in cases, that is why PF dropped this convention).

并且您仍然可以检查3.5中的widgetVar值是否以一种或另一种方式添加到了小部件的外部html元素中.然后,您可以使用一些jquery检索它.就我个人而言,我在以前的PF版本中使用了约定"方式(将"Widget"附加到id中的值上,并将其放在widgetVar属性中)

And you could still check if in 3.5 the widgetVar value is in one way or another added to the outer html element of the widget. You could then retrieve it with some jquery. Personally, I used the 'convention' way in previous PF versions (appending 'Widget' to the value that is also in the id and putting that in the widgetVar attribute)

如果更新"窗口小部件,则意味着无法更新窗口小部件的内容.没有refresh()类型的方法可以执行此操作.您可以做的是将id(!)发送到服务器,然后通过requestContext在服务器上执行update(..)

And if with 'updating' the widget, you mean updating the content of the widget, you can't. There is no refresh() kind of method that does this. What you could do is send the id(!) to the server and execute an update(..) there via the requestContext like this

RequestContext context = RequestContext.getCurrentInstance();
context.update("errorsPanel");

但是直到更新列表服务器端时,这样做可能会更好.因此,可能是您陷入了XY陷阱.

But it might be even better to just to this the moment you update the list serverside. So it might be that you've fallen into the XY trap.

但是,如果确实需要,可以使用

But if it is really needed, here is an example that works, using the PrimeFaces extensions remoteCommand since that is the easiest way)

xhtml:

<pe:remoteCommand id="refreshWidgetCommand" name="refreshWidget" process="@this" actionListener="#{updateWidgetController.refresh}">  
    <pe:methodSignature parameters="java.lang.String"/>  
    <pe:methodParam name="id"/>  
</pe:remoteCommand>  

Bean:

@ManagedBean  
@RequestScoped  
public class UpdateWidgetController {  

    public void refresh(final String id) {  

        RequestContext context = RequestContext.getCurrentInstance();
        context.update(id);
    }
}

呼叫refreshWidget('errrosPanel');将实现您想要的.

calling refreshWidget('errrosPanel'); will achieve what you want.

不要使用PFE(或由于版本不兼容而不能使用它),您可以使用

Don't wan't to use PFE (or can't use this due to version incompatibilities), you can achieve the same with PF remoteCommand But the specific example for 3.5 might differ a little