RichFaces 4 fileupload clear and clear all buttons

2019-08-05 19:10发布

Well currently I have this:

<rich:fileUpload addLabel="Agregar" clearAllLabel="Quitar todos"
                 clearLabel="Quitar" deleteLabel="Quitar"
                 doneLabel="Completado" uploadLabel="Subir archivos"
                 fileUploadListener="#{uploadBean.doUpload}"
                 acceptedTypes="txt, csv"
                 noDuplicate="true">
    <a4j:ajax event="uploadcomplete" render="validationButton"/>
    <a4j:ajax event="clear" listener="#{uploadBean.doClearFilesList}"
              render="validationButton"/>
</rich:fileUpload>

On the backing bean I have a list of the files uploaded. When I click on Clear/Clear all button the event clear is fired and the method doClearFilesList (which just clears the list of files uploaded) is perfectly when the user hits the Clear All button, but If the user clicks on Clear button It should just delete the item on the list corresponding to the file cleared.

What can I do on my UploadBean.doClearFilesList method to delete a single file from the list? Should be something like:

public void doClearFilesList(){
    files.clear(); //when CLEAR ALL is clicked
    files.remove(oneFile); //when CLEAR is clicked
    validationButtonRendered = false;
}

Any idea?

Cheers

UPDATE

RichFaces 4.1.0 Final JSF Mojarra 2.1.6 Tomcat 7

2条回答
神经病院院长
2楼-- · 2019-08-05 19:54

I am not clear at which point you failed to run the sample described at https://community.jboss.org/message/727544#727544

However I hope following would work for you which is very similar to above sample.

Page:

<h:head>
<script>
  function clear(event) {
    var files = new Array();
    var data = event.rf.data;
    for (var i in data) {
      files[i] = data[i].name;
    }
    clearFunc(files);
  }
</script>
</h:head>
<body>
  <h:form>
    <rich:fileUpload onclear="clear(event);"/>
    <a4j:jsFunction name="clearFunc" action="#{del.clearFile}" ajaxSingle="true">
      <a4j:param name="fName" assignTo="#{del.fileNames}" />
    </a4j:jsFunction>
  </h:form>
</body>

Class:

public class Del {
  String[] fileNames;
  public void clearFile() {
    for(String name : fileNames) {
      System.out.println(">>" + name);
      //Do file removing part here
    }
  }
  public String[] getFileNames() {
    return fileNames;
  }
  public void setFileNames(String[] fileNames) {
    this.fileNames = fileNames;
  }
}
查看更多
女痞
3楼-- · 2019-08-05 19:58

Add "onclear" attribute to your <rich:fileUpload/> component and call a <a4j:jsFunction/> and pass the file name to it as below.

<rich:fileUpload onclear="clearFunc(event.memo.entry.fileName);" ..../>

Your <a4j:jsFunction/> should be as below.

<a4j:jsFunction name="clearFunc" actionListener="#{uploadBean.clearFile}" ajaxSingle="true">
   <a4j:actionparam name="fName" />
</a4j:jsFunction>

Inside the listener method you can access the file name as below.

public void clearFile(ActionEvent event) {
  FacesContext context = FacesContext.getCurrentInstance();
  String fileName = context.getExternalContext().getRequestParameterMap().get("fName").toString();
  System.out.println("fileName = " + fileName);}
查看更多
登录 后发表回答