在javascript中获取primefaces widgetVar并更新它 [英] Get primefaces widgetVar in javascript and update it

查看:16
本文介绍了在javascript中获取primefaces widgetVar并更新它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的primefaces组件:

I have a primefaces component like this:

<p:panel styleClass="errorsPanelClass" id="errorsPanel" header="Messages" toggleable="true" effect="drop" toggleSpeed="0" closeSpeed="0" widgetVar="errorsPanelWidget">

我正在尝试在 javascript 上下文中获取组件并更新它.

and i'm trying to get the component in javascript context and update it.

我已经尝试过:

function getWidgetVarById(id) {
   for (var propertyName in PrimeFaces.widgets) {
     if (PrimeFaces.widgets[propertyName].id === id) {
       return PrimeFaces.widgets[propertyName];
     }
   }
}

如何查找widgetVar?

使用 JavaScript 获取 widgetVar 或检查/取消选中所有其他 PrimeFaces 复选框

如果我能找到按类获取 widgetVar 的解决方案,那就太好了,但欢迎任何其他解决方案.

It will be great if I'll find a solution to take the widgetVar by class, but any other solutions are welcomed.

我也试过:

function getWidgetVarById() {
    for ( var propertyName in PrimeFaces.widgets) {
        alert(PrimeFaces.widgets[propertyName].id);
    }
}

但我收到一个 javascript 错误'PrimeFaces.widgets.length' 为空或不是对象"

but I get an javascript error "'PrimeFaces.widgets.length' is null or not an object"

有什么想法吗?

推荐答案

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)

最简单的方法是使用一个约定,将widgetVar 命名为您的id,但带有后缀,例如id_widget.您可以(建议不要这样做)将 widgetVar 保留在外面,然后小部件与 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.

但如果真的需要,这里有一个例子,使用 PrimeFaces 扩展 remoteCommand 因为这是最简单的方法)

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>  

豆子:

@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(或由于版本不兼容而无法使用),您可以使用 PF remoteCommand 但是3.5的具体例子可能有点不同

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

这篇关于在javascript中获取primefaces widgetVar并更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆