简体   繁体   English

上传文件p:fileUpload后渲染p:commandButton

[英]Render p:commandButton after uploading a file p:fileUpload

I have a page with a p:fileUpload and a p:commandButton the first time the page is visited the button should be hidden and just after a file was uploaded the button should be rendered. 我有一个带有p:fileUploadp:commandButton的页面,第一次访问该页面时,该按钮应该被隐藏,并且在上传文件后应该呈现该按钮。 My code looks like follows, any ideas on how to achieve this? 我的代码如下所示,关于如何实现这一点的任何想法?

    <h:form enctype="multipart/form-data" >

        <p:fileUpload
            fileUploadListener="#{fileUploadController.handleFileUpload}"
            mode="advanced"
            multiple="true"
            sizeLimit="2000000000"
            allowTypes="/(\.|\/)(txt|csv)$/"
            required="true"
            label="Seleccionar"
            uploadLabel="Subir a servidor"
            cancelLabel="Cancelar">
        </p:fileUpload>

        <p:commandButton id="btnValidar" value=" Validar "
                         rendered="#{fileUploadController.btnRendered}"
                         style="margin-left: 430px;"/>

    </h:form>

Just use the update attribute the same way as on all other PrimeFaces ajax components. 只需使用update属性,就像在所有其他PrimeFaces ajax组件上一样。

<p:fileUpload update="@form" />

I of course assume that you've set btnRendered to true inside handleFileUpload method. 我当然假设你在handleFileUpload方法中设置了btnRenderedtrue

Please note that you can't set it to the ID of the button itself, simply because it's not present in the HTML DOM tree when the btnRendered defaults to false . 请注意,您无法将其设置为按钮本身的ID,因为当btnRendered默认为false时,它不会出现在HTML DOM树中。 It's namely JavaScript which does the updating job in the HTML DOM tree based on the retrieved Ajax response. 它就是JavaScript,它根据检索到的Ajax响应在HTML DOM树中执行更新作业。 If you want to update alone the button, not the form, then wrap it in another component which is always present in the HTML DOM tree: 如果要单独更新按钮而不是表单,请将其包装在HTML DOM树中始终存在的另一个组件中:

<h:panelGroup id="btnValidar">
    <p:commandButton rendered="#{fileUploadController.btnRendered}" />
</h:panelGroup>

and update it as follows 并按如下方式更新

<p:fileUpload update="btnValidar" />

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM