Can you copy an existing div to a modal dialog

2019-04-13 20:06发布

问题:

I have a dashboard with multiple panels to display different information and I would like to be able to add a button to display the panel in a modal.

I am using bootstrap and all I can find is modals already written. I would like to copy the content of a div tag which is a panel and then display that in the model, but I am unsure as how to go about it. The html for the panel can be seen below. The panel does contain a table but I have cut the code down. I have looked around but I can't seem to find a solution for what I need. I understand that I need to use jQuery, copy the panel and add it to the contents of the modal dialog. I have tried to do this but had no luck. I have included a fiddle to show the approach I have taken Here this does launch the modal but the content is not displayed nor the button to close it.

Any help would be greatly appreciated.

HTML code for the panel:

<div class="panel sample panel-yellow message">
    <div class="panel-heading">
        <h3 class="panel-title">
            <i class="fa fa-flask fa-fw"></i>Sample Updates 
            <i class="fa fa-spinner fa-spin sample "></i>
            <a id="refreshMessage" class="refreshButton pull-right" href="#"><span class="fa fa-refresh"></span></a>
            <a id="hideMessage" class="refreshButton pull-right" href="#"><span class="fa fa-arrow-up"></span></a>
            <a id="focusMessage" class="focusButton pull-right" href="#"><span class="fa fa-eye"></span></a>
        </h3>
    </div>
    <div class="panel-body">
        <center>
            <h5 class="text-warning">Table shows samples created or modified in the last ten minutes</h5>
        </center>
    </div>
</div>

Screenshot of the panel I want to display:

回答1:

Please Try this

<div class="panel sample panel-yellow message">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="fa fa-flask fa-fw"></i>Sample Updates <i class="fa fa-spinner fa-spin sample ">
                        </i>
                        <a id="refreshMessage" class="refreshButton pull-right" href="#"><span class="fa fa-refresh"></span></a>
                        <a id="hideMessage" class="refreshButton pull-right" href="#"><span class="fa fa-arrow-up"></span></a>
                        <a id="focusMessage" class="focusButton pull-right" href="#"><span class="fa fa-eye"></span></a>
                    </h3>

                </div>
                <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-condensed table-hover table-bordered table-responsive">
                        <thead>
                            <tr>
                                <th>
                                    New Samples
                                </th>
                                <th>
                                    Modified Samples
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <span class="sampleCount"></span>
                                </td>
                                <td>
                                    <span class="sampleCount2"></span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    </div>
                    <center>
                        <h5 class="text-warning">
                            Table shows samples created or modified in the last ten minutes</h5>
                    </center>
                </div>
            </div>


<div id="SampleModal" class="modal fade" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-body">
    </div>

Only Change in Mark Up is I removed the class hide

$('.focusButton').click(function(){
    var newRow = $(this).parent().parent().parent('.sample').clone();
    $('#SampleModal .modal-body').html(newRow);
    $('#SampleModal').modal();
});

Check This Fiddle: http://jsfiddle.net/hoja/qsbkqc9m/12/



回答2:

You can achieve this by using the following code

Using JQuery:

var html = $(id).html();

Using Javascript:

var html = document.getElementById(id).innerHTML;

Hope this helps.