Search code examples
jsffile-uploadrichfaces

RichFaces 4 fileupload clear and clear all buttons


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


Solution

  • 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;
      }
    }