无法在p:fileUpload中使用widgetVar [英] widgetVar cannot be used in p:fileUpload

查看:72
本文介绍了无法在p:fileUpload中使用widgetVar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择错误类型的文件时删除错误消息.我点击了链接 http://forum.primefaces.org/viewtopic.php ?f = 3& t = 23853 .

I want to remove the error message when choosing a wrong type of file. I followed the link http://forum.primefaces.org/viewtopic.php?f=3&t=23853 .

<p:fileUpload fileUploadListener="#{userProfileUpdateController.upload}"         
    widgetVar="fileuplaodWgt"                     
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
    description="Select Images"
    update="userPhoto"
    mode="advanced"/>

然后导入<h:outputScript name="library.js" library="script"></h:outputScript>

在library.js中

In library.js

$(document).ready(function(){
   alert(fileuplaodWgt);
   fileuplaodWgt.buttonBar.find("button.cancel").bind("click", function(e){
   clearInvalidFileMsg();
    });
});

function clearInvalidFileMsg(){
    fileuplaodWgt.uploadContent.find("tr.ui-state-error").remove();
 }

完成上述操作后,文件上传未显示在我的页面中.跟踪我的library.js文件时,发现 fileuplaodWgt ReferenceError:未定义fileuplaodWgt .我在mozilla和chrome中尝试过.他们都没有显示我的文件上传.你能帮助我我做错了什么吗?

After writing above, file upload is not shown in my page. When I trace the my library.js file, I found fileuplaodWgt ReferenceError: fileuplaodWgt is not defined. I tried in mozilla and chrome. None of them shows my file upload. Can you help me what I am wrong???

推荐答案

PrimeFaces也可以通过$(function())在DOM就绪的情况下初始化小部件变量.包含命令的<script>直接在组件之后呈现.因此,如果您的$(document).ready()在小部件var之前 被调用,那么它将找不到小部件var,因为它是在以后初始化的.您需要确保在呈现组件后 调用$(document).ready().一种方法是通过target="body"将脚本重新定位到HTML <body>的末尾.

PrimeFaces initializes widget vars on DOM ready as well by $(function()). The <script> containing the command is rendered directly after the component. So, if your $(document).ready() is invoked before the widget var one, then it won't find the widget var because it's initialized later. You need to ensure that your $(document).ready() is invoked after the component is rendered. One of the ways is relocating the script to the end of HTML <body> by target="body".

<h:outputScript name="library.js" library="script" target="body" />

这样,在到达文档末尾(即在所有小部件var初始化之后 之后)加载并初始化脚本.

This way the script is loaded and initialized when the end of document is reached, thus after all widget var initializations.

另一种方法是使用$(window).load()而不是$(document).ready(),但这很笨拙,而且可能更慢,因为只有在加载文档中引用的所有图像时才调用它.

Another way is to use $(window).load() instead of $(document).ready(), but this is clumsy and potentially slower as it's only invoked when all images referenced in the document are loaded.

无关与具体问题无关,您使用资源library的方式不正确.请仔细阅读

Unrelated to the concrete problem, the way you're using the resource library is not right. Please carefully read What is the JSF resource library for and how should it be used? and just use

<h:outputScript name="script/library.js" target="body" />

顺便说一句,您在小部件的var名称中也遇到了令人不安的错字,与该问题没有进一步的关系.这是上载,而不是上载.

You've by the way also a disturbing typo in the widget var name not further related to the problem. It's upload, not uplaod.

这篇关于无法在p:fileUpload中使用widgetVar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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