如何隐藏一个按钮,直到一个行动完成? Primefaces [英] How to hide a button until an action is completed? Primefaces

查看:560
本文介绍了如何隐藏一个按钮,直到一个行动完成? Primefaces的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个文件上传系统,目前我有一个按钮,让用户到下一页,但即使用户没有上传任何东西,这是可见的,如果用户按下这个上传任何东西之前,它会抛出一个错误,看起来不好,所以我想要做的就是隐藏这个按钮,直到文件上传成功实现,任何想法如何?

 < p:fileUpload widgetVar =uploadfileUploadListener =#{fileUploadController.handleFileUpload}
mode = 高级
multiple =false
update =messages
label =选择文件
sizeLimit =100000000
allowTypes =/(\\ \\。| \ /)(gif | jpe?g | png | doc | docx | txt | pdf | html)$ /
auto =true/>
<! - 选择了auto =true,这已经被选中以防止用户在测试时发现
错误,一些用户按下了上传按钮,并想知道为什么没有工作,而不是
选择文件,现在停止这个 - >



上传文件后请按下面的按钮继续


下一个动作的命令按钮是我希望禁用直到文件上传完成的那个



编辑:

 < p:commandButton action =#{navigationBean。 naviagtion}value =Nextajax =Falsedisabled =#{fileUploadController.UploadComplete}/> 

是我的命令按钮,它指向fileUploadContoller,这是文件上传发生的地方等等,



问题是当我运行应用程序时,我总是在页面加载时收到一个活动按钮。

一个布尔到我的fileUploadController:

pre $公共无效handleFileUpload(FileUploadEvent事件){
//System.out.println( DesintationPDF:+ destinationPDF);
System.out.println(被调用的句柄文件);
System.out.println(Destination is:+ configProp.getProperty(destination));

FacesMessage msg = new FacesMessage(Succesful,event.getFile()。getFileName()+is uploaded。); //在网页上显示给用户
FacesContext.getCurrentInstance()。addMessage(null,msg);


try {
copyFile(event.getFile()。getFileName(),event.getFile()。getInputstream());
} catch(IOException e){
//处理异常
e.printStackTrace();



$ b public boolean isUploadComplete(){
return uploadComplete;
}

public void setUploadComplete(boolean uploadComplete){
this.uploadComplete = uploadComplete;
}

但仍然收到错误

编辑2:

 < p:commandButton action =#{navigationBean.naviagtion}value =Next disabled =#{fileUploadController.uploadComplete}/> 

控制台

  INFO:复制文件前上传完整值false 
INFO:上传完成值为:true

所以它将值更改为true

但不改变按钮的生存状态

解决方案

要禁止按钮直到上传完成,只需将 disabled 属性绑定到bean中的属性即可。在我看来,禁用似乎比突然渲染更直观。用户也会知道在后台发生了什么事情。

 < p:commandButton action =#{navigationBean.naviagtion}value =Nextdisabled =#{bean .disable}ajax =False/> 

由于您使用PrimeFaces,所以此解决方案最为简单。用你的bean的名字替换 bean


$ b 编辑:

  public class YourNavigationBean {

private boolean uploadComplete; //< ---这就是属性

// ...你的bean的内容,像构造函数和东西..
// ...

//需要setter和getter,在这里它们是
public boolean isUploadComplete(){
return uploadComplete;
}
public void setUploadComplete(boolean uploadComplete){
this.uploadComplete = uploadComplete;
}
}


i currently have a file upload system in place, and currently i have a button to take the user to the next page, but this is visible even if the user has not uploaded anything, the danger is here if the user presses this before uploading anything it will throw an error and look bad, so what i am trying to do is hide this button until file upload is successfully achieved, any ideas how ?

<p:fileUpload widgetVar="upload" fileUploadListener="#{fileUploadController.handleFileUpload}"
                                  mode="advanced" 
                                  multiple="false" 
                                  update="messages"
                                  label="Select File"
                                  sizeLimit="100000000" 
                                  allowTypes="/(\.|\/)(gif|jpe?g|png|doc|docx|txt|pdf|html)$/"
                                  auto="true"/> 
                    <!-- selected auto="true" this has been selected to prevent user error as was discovered 
                    when testing, some users pressed the upload button and wondered why nothing worked instead of
                    select file, now this stops this -->


                    <p:growl id="messages" showDetail="true"/>

                    Please press the button below once you have uploaded the file, to continue


                    <p:commandButton action="#{navigationBean.naviagtion}"   value="Next"  ajax="False"/> 

the command button of action next is the one i wish to disable untill file upload is complete

EDIT :

<p:commandButton action="#{navigationBean.naviagtion}"   value="Next"  ajax="False" disabled="#{!fileUploadController.UploadComplete}"/> 

Is my command button, it is pointing to the fileUploadContoller, this is where the file upload happens etc,

the issue is when i run the app i get a live button always on page load

i have added a boolean onto my fileUploadController :

    public void handleFileUpload(FileUploadEvent event) {
        //System.out.println("DesintationPDF : " + destinationPDF);
        System.out.println("called handle file");
        System.out.println("Destination is : " + configProp.getProperty("destination"));

        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); //Displays to user on the webpage
        FacesContext.getCurrentInstance().addMessage(null, msg);


        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
//handle the exception
            e.printStackTrace();
        }

    }

    public boolean isUploadComplete() {
        return uploadComplete;
    }

    public void setUploadComplete(boolean uploadComplete) {
        this.uploadComplete = uploadComplete;
    }

but still get the error

EDIT 2 :

     <p:commandButton action="#{navigationBean.naviagtion}"   value="Next" disabled="#{!fileUploadController.uploadComplete}"/> 

the console

INFO: Upload complete value before copy file false
INFO: upload complete value is : true

so it is changing the value to true

but is not changing the button to live

解决方案

To disable your button until the upload finished, just bind the disabled attribute to a property in a bean. In my opinion disabling seems much more intuitive than suddenly rendering it. Also the user will know that there's something going on in the background.

 <p:commandButton action="#{navigationBean.naviagtion}" value="Next" disabled="#{bean.disable}" ajax="False"/> 

Since you use PrimeFaces, this solution is the easiest. Just replace bean with the name of your bean.

Edit:

public class YourNavigationBean {

    private boolean uploadComplete; // <--- that's the property

    // ... your bean content, like constructors and stuff..
    // ...

    //a setter and a getter is needed, to here they are
    public boolean isUploadComplete() {
       return uploadComplete;
    }
    public void setUploadComplete(boolean uploadComplete) {
       this.uploadComplete = uploadComplete;
    }
}

这篇关于如何隐藏一个按钮,直到一个行动完成? Primefaces的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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