简体   繁体   English

使用JQuery创建可重用的模式

[英]Create a reusable modal using JQuery

I know writing an IF-ELSEIF-ELSE statement in the loop below would work, however I want to avoid having to write multiple modals. 我知道在下面的循环中编写IF-ELSEIF-ELSE语句是可行的,但是我想避免不得不编写多个模式。 Instead I am looking for a JQuery modal to pop up when the image icon (basically a info image) is clicked. 相反,我正在寻找单击图像图标(基本上是信息图像)时弹出的JQuery模式。 I want to be able to pass in the error into the function, which will then display in the modal. 我希望能够将错误传递给函数,然后将其显示在模式中。

Example: 例:

Say I have a 400 Error and 500 Error, when I click the info icon, the definition should appear. 假设我遇到400错误和500错误,当我单击信息图标时,应该出现定义。

CODE BELOW: 下面的代码:

index.gsp index.gsp

<html>
    <%-- Some code (saving space for body) --%>
<body>
    <div id="content">
    <div id="content-header">
        <h1>Error Checking</h1>
    </div> <!-- #content-header -->

    <div id="content-container">
        <div class="portlet">
            <div class="portlet-content">
                <div class="table-responsive">
                    <table
                            class="table table-striped table-bordered table-hover table-highlight table-checkable"
                            data-provide="datatable"
                            data-display-rows="25"
                            data-info="true"
                            data-search="true"
                            data-length-change="true"
                            data-paginate="true">
                        <thead>
                        <tr>
                            <th data-filterable="true" data-sortable="true" data-direction="desc">User ID</th>
                            <th data-filterable="true" data-sortable="true" data-direction="desc">Task ID</th>
                            <th data-filterable="true" data-sortable="true" data-direction="desc">Error Message</th>
                        </tr>
                        </thead>
                        <tbody>
                        <g:each in="${lists}" var="list">
                            <tr>
                                <td>${list.userId}</td>
                                <td>${list.taskId}</td>
                                <td>
                                   **%{--WANT TO PLACE MODAL CALL HERE--}%**
                                    **<a href=""><i class="fa fa-exclamation-triangle ui-popover pull-left" style="color:#f0ad4e;"></i></a>
                                    ${list.errorMsg}**
                                </td>
                            </tr>
                        </g:each>
                        </tbody>
                    </table>
                </div> <!-- /.table-responsive -->
            </div> <!-- /.portlet-content -->
        </div> <!-- /.portlet -->
    </div> <!-- /#content-container -->
</div> <!-- #content -->

The MODAL I want to pop up: 我想弹出的MODAL:

<div id="styledFreqLargerModal" class="modal modal-styled fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3 class="modal-title">Issue</h3>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-tertiary" data-dismiss="modal">Close</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

You can generate dynamic id's using your list item's id. 您可以使用列表项的ID生成动态ID。 Then you can place your modal at the place you have mentioned. 然后,您可以将模态放置在您提到的位置。 The following is a code example: 以下是一个代码示例:

<div id="styledFreqLargerModal${list.id}" class="modal modal-styled fade">
    [..]
</div><!-- /.modal -->

If you are using jQuery-UI 如果您使用的是jQuery-UI

<a class="openDialog" href="#styledFreqLargerModal${list.id}"><i class="fa fa-exclamation-triangle ui-popover pull-left" style="color:#f0ad4e;"></i></a>

In your javascript do: 在您的JavaScript中执行以下操作:

$(document).on("click", "a.openDialog", function() {
   $($(this).attr("href")).dialog();
}

If you are using bootstrap 如果您正在使用引导程序

<a data-toggle="modal" href="#styledFreqLargerModal${list.id}"><i class="fa fa-exclamation-triangle ui-popover pull-left" style="color:#f0ad4e;"></i></a>

Solved the problem... Used Jquery modal. 解决了问题...使用了Jquery模态。 This prevented me from having to create multiple modals. 这使我不必创建多个模态。 In the modal I have a "click" method that determines which item got clicked, then outputs that specific message. 在模式中,我有一个“单击”方法,该方法确定单击了哪个项目,然后输出该特定消息。 See code below. 请参见下面的代码。

index.gsp index.gsp

<html>
    <%-- Some code (saving space for body) --%>
<body>
  <div id="content">
  <div id="content-header">
    <h1>Error Checking</h1>
  </div> <!-- #content-header -->

  <div id="content-container">
    <div class="portlet">
        <div class="portlet-content">
            <div class="table-responsive">
                <table
                        class="table table-striped table-bordered table-hover table-highlight table-checkable"
                        data-provide="datatable"
                        data-display-rows="25"
                        data-info="true"
                        data-search="true"
                        data-length-change="true"
                        data-paginate="true">
                    <thead>
                    <tr>
                        <th data-filterable="true" data-sortable="true" data-direction="desc">User ID</th>
                        <th data-filterable="true" data-sortable="true" data-direction="desc">Task ID</th>
                        <th data-filterable="true" data-sortable="true" data-direction="desc">Error Message</th>
                    </tr>
                    </thead>
                    <tbody>
                    <g:each in="${lists}" var="list">
                        <tr>
                            <td>${list.userId}</td>
                            <td>${list.taskId}</td>
                            <td>
                               <g:if test="${(list.errorMsg).contains("400")}">
                                  <a id="modalAlert-${list.taskId}" class="modal-alert-null" ><i class="fa fa-exclamation-triangle ui-popover pull-left" style="color:#f0ad4e;"></i></a>
                                  ${list.errorMsg}
                               </g:if>
                               <g:elseif test="${(list.errorMsg).contains("500")}">
                                  <a id="modalAlert-${list.taskId}" class="modal-alert-outOfBounds" ><i class="fa fa-exclamation-triangle ui-popover pull-left" style="color:#f0ad4e;"></i></a>
                                  ${list.errorMsg}
                                </g:elseif>
                            </td>
                        </tr>
                    </g:each>
                    </tbody>
                </table>
            </div> <!-- /.table-responsive -->
        </div> <!-- /.portlet-content -->
    </div> <!-- /.portlet -->
  </div> <!-- /#content-container -->
</div> <!-- #content -->

<r:require modules="jquery"/>
<script type="text/javascript">
    $(document).ready(function() {
        // Null pointer exception
        $(".modal-alert-null").click(function() {
            modalAlert("400 error message");
        });
        // outOfBoundsException
        $(".modal-alert-outOfBounds").click(function() {
            modalAlert("500 error message");
            });

    });

    function modalAlert(description)
    {
        $("body").append(buildAlertModal());

        $("#alert-modal .modal-description").html(description);

        $("#alert-modal").modal("show");
    }

    function buildAlertModal() {
        return "<div id='alert-modal' class='modal modal-styled fade'>" +
                "   <div class='modal-dialog'>" +
                "       <div class='modal-content'>" +
                "           <div class='modal-header'>" +
                "               <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>" +
                "               <h3 class='modal-titl'>Exception Info</h3>" +
                "           </div>" +
                "           <div class='modal-body'>" +
                "               <p class='modal-description'></p>" +
                "           </div>" +
                "           <div class='modal-footer'>" +
                "               <button type='button' class='btn btn-tertiary' data-dismiss='modal'>Close</button>" +
                "           </div>" +
                "       </div>" +
                "   </div>" +
                "</div>";
    }

    function closeModalAlert() {
        $("#alert-modal").modal("hide");
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM