How to handle bootstrap modal in Java Server Faces?

5.2k views Asked by At

I like the principle from composite-components but this and bootstraps modal wont work together.

Whats the best Practice to manage multiple Custom Composite Components Dialogs and use like this Example in a JSF-Table. Pass the Managed Bean Value from selected row to the Dialog.

This works only for me if i wrote all in one Page File. See the last one.

bootstrapModal.xhtml the modal wrapped in a composite component

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite">

<cc:interface>

    <cc:attribute name="title" />
    <cc:attribute name="linkNameLable" />
    <cc:attribute name="linkNameValue" />
    <cc:attribute name="urlNameLable" />
    <cc:attribute name="urlNameValue" />
    <cc:attribute name="saveButtonText" />
    <cc:attribute name="saveButtonAction" 
                  method-signature="java.lang.String action()" />

</cc:interface>

<cc:implementation>

    <div id="#{cc.clientId}" class="modal fade myModal" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true" >
        <div class="modal-dialog">  
            <div class="modal-content">
                <div class="modal-body">
                    <h:form>
                        <h:outputLabel   value="#{cc.attrs.linkNameLable}" />
                        <h:inputText     value="#{cc.attrs.linkNameValue}" />
                        <h:outputLabel   value="#{cc.attrs.urlNameLable}" />
                        <h:inputText     value="#{cc.attrs.urlNameValue}"  /> 
                        <h:commandButton value="#{cc.attrs.saveButtonText}" action="#{cc.attrs.saveButtonAction}" />
                    </h:form>
                </div>
            </div>
        </div>
    </div>

</cc:implementation>
</html>

the wrapping div doest not work.

<div id=#{cc.clientId}>...</div>

I tried also to pass the id to the form inside.

<h:form id=#{cc.clientId}

view.xhtml with composite component that does not work. f:ajax cant render the id from composite component


...
<script>
    function showModal() {
        $('#myModal').modal('show');
    }
</script>
...
<h:dataTable id="myTable" value="#{linkController.linkList}" var="o">   
...          
    <h:column>
        <f:facet name="header">Actions</f:facet>
        <h:form>
            <h:commandLink value="edit" onclick="showModal()" action="#{linkController.setLinkFromParam}">
                <f:ajax render="myModal value1 value2" />
                <f:param name="name" value="#{o.name}"  />
                <f:param name="url"  value="#{o.url}"   />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

<!-- 1.outside the Table rendering works fine -->

<h:outputText id="value1" value="#{linkController.name}" /><br />
<h:outputText id="value2" value="#{linkController.url}" />


<!-- 2.The render for this id does not work -->

<mahi:bootstrapModal    title="Edit Link" 
                        id="myModal" 
                        linkNameLable="Link Name:" 
                        linkNameValue="#{linkController.name}"
                        urlNameLable="URL:"
                        urlNameValue="#{linkController.url}"
                        saveButtonText="Save"
                        saveButtonAction="#{linkController.updateLink(link)}" />

view.xhtml with the content from Composite Component works fine. Because i can render the h:form directly with the id="myModalForm".

...
<script>
    function showModal() {
        $('#myModal').modal('show');
    }
</script>
...
<h:dataTable id="myTable" value="#{linkController.linkList}" var="o">   
...          
    <h:column>
        <f:facet name="header">Actions</f:facet>
        <h:form>
            <h:commandLink value="edit" onclick="showModal()" action="#{linkController.setLinkFromParam}">
                <f:ajax render="myModalForm" />
                <f:param name="name" value="#{o.name}"  />
                <f:param name="url"  value="#{o.url}"   />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

<!-- The Content from the Custom Composite Component works -->

<div id="myModal" class="modal fade myModal" tabindex="-1" role="dialog" aria-labelledby="myLinkModal" aria-hidden="true" >
    <div class="modal-dialog">  
        <div class="modal-content">
            <div class="modal-body">
                <h:form id="myModalForm">
                    <h:outputLabel   value="Name:" />
                    <h:inputText     value="#{linkController.name}" />
                    <h:outputLabel   value="URL:" />
                    <h:inputText     value="#{linkController.url}" /> 
                    <h:commandButton value="Save" action="#{linkController.saveLink(link)}" />
                </h:form>
            </div>
        </div>
    </div>
</div>
1

There are 1 answers

0
Ayoun On BEST ANSWER

simple solution :

add class to your modal in order to call it, and call your modal as class not as id like :

modal :>

<div id="#{cc.clientId}" class="modal fade myModal"

script :>

<script>
function showModal() {
    $('.myModal').modal('show');
}