mercredi 27 avril 2016

Dynamically rendered h:inputFile using checkbox

Current Goal: I just want to be able to show and hide h:inputFile tag. My overall goal would be to change the cover of a book by using h:inputFile and clicking on a save button to reflect my changes into a database.

Problem: Trying to display h:inputFile tag based on if a checkbox for a row will display the option to upload a file. The issue arises when I want to uncheck a row (meaning to stop editing the row) and the row is still shown in edit mode.

Before editing a row:

Before clicking on checkbox

While editing a row:

While editing a row

After editing a row (issue here):

Current issue

I just want to display it back to the first picture as if the user didn't want to change the book cover.


Code In Question:

<h:inputFile id="file" label="file" style="margin-top: 10px"
    value="#{libraryFileBean.part}"
    validator="#{libraryFileBean.validateFile}"
    rendered="#{book.editable}"/>


Additional Info:

Bean Scopes at the moment:

  • LibraryBean is SessionScoped
  • LibraryFileBean is RequestScoped
    • Tried SessionScoped with no avail

XMLNS Libraries:

<html xmlns="http://ift.tt/lH0Osb"
      xmlns:h="http://ift.tt/J1LPWj"
      xmlns:f="http://ift.tt/19DXC4H"
      xmlns:ui="http://ift.tt/KsEgXx">


Full Code:

<h:form id="frmlibrary" enctype="multipart/form-data">
    <ui:fragment rendered="#{not empty libraryBean.books}">
        <div class="panel panel-default">
            <h:dataTable value="#{libraryBean.books}" class="table table-bordered"
                         var="book" border="1" id="dtLibrary" rendered="#{not empty libraryBean.books}">
                <h:column>
                    <f:facet name="header">Edit</f:facet>
                    <h:selectBooleanCheckbox class="checkbox" value="#{book.editable}" onclick="submit()" />
                </h:column>
                <h:column>
                    <f:facet name="header">Book Cover</f:facet>
                    <image src="ImageServlet?fileID=#{book.libID}"
                           height="150" width="100" />
                    <h:inputFile id="file" label="file" style="margin-top: 10px"
                                 value="#{libraryFileBean.part}"
                                 validator="#{libraryFileBean.validateFile}"
                                 rendered="#{book.editable}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">Title</f:facet>
                    <h:inputText value="#{book.title}" class="form-control"
                                 rendered="#{book.editable}" size="20"/>
                    <h:outputText value="#{book.title}" 
                                  rendered="#{not book.editable}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">Author</f:facet>
                    <h:inputText value="#{book.author}" class="form-control"
                                 rendered="#{book.editable}" size="20"/>
                    <h:outputText value="#{book.author}" 
                                  rendered="#{not book.editable}"/>
                </h:column>
            </h:dataTable>
        </div>
    </ui:fragment>
    <h:commandButton value="Save Library" class="btn btn-info"
                     action="#{libraryBean.save()}" rendered="#{not empty libraryBean.books}"/>
</h:form>




Aucun commentaire:

Enregistrer un commentaire