twitter-bootstrap 如何在引导模式中加载动态内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38502572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:08:31  来源:igfitidea点击:

How to load dynamic content in bootstrap modal

javascriptjqueryhtmltwitter-bootstrapoctobercms

提问by tan

i have create a bootstrap modal but i need the content to be dynamic. Each time i select a different table row, the content of that row should popup. The content inside bootstrap modal is a dynamic table. Is possible to show a simple sample of how by selecting from the row of a table, i can get a dynamic table in the popup modal. Please help. Thanks

我已经创建了一个引导模式,但我需要内容是动态的。每次我选择不同的表格行时,该行的内容应该弹出。bootstrap modal 里面的内容是一个动态表。可以显示一个简单的示例,说明如何通过从表的行中进行选择,我可以在弹出模式中获得一个动态表。请帮忙。谢谢

  while($row = $results->fetch_assoc()): ?>
       <tr>
         <td class="list-answer"
               data-toggle="modal"
               href="#content-confirmation">
                <?= $row["name"]; ?>
           </td>
        </tr>
  <?php endwhile ?>

 <div class="control-popup modal fade" id="content-confirmation"
 tabindex="-1" role="dialog">
<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>

            <h4 class="modal-title">Are you sure you wanna do
            that?</h4>
        </div>
        <div class="modal-body">
          load dynamic table content.......

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default"
            data-dismiss="modal">Cancel</button>
            <button type="button" class="btn btn-primary"
            data-dismiss="modal">Save</button>
        </div>
    </div>
</div>

回答by Mightee

Are you trying to give your table column a link with href ? because it would not work like that.

你想给你的表格列一个带有 href 的链接吗?因为它不会那样工作。

To your question, there are multiple ways of doing this. I would give the table's row an id and handle the row click or column click event in js code. after that i would get that rows content either with an ajax request or directly from the table's columns.

对于您的问题,有多种方法可以做到这一点。我会给表格的行一个 id 并在 js 代码中处理行点击或列点击事件。之后,我将通过 ajax 请求或直接从表的列中获取该行的内容。

So your code would look like:

所以你的代码看起来像:

 while($row = $results->fetch_assoc()): ?>
   <tr data-id="<?= $row["id"]; ?>">
     <td class="list-answer">
            <?= $row["name"]; ?>
       </td>
    </tr>

your javascript code:

你的 javascript 代码:

$(function(){
$(".list-answer").click(function() {
    var row_id = $(this).parents('tr').attr('data-id');

    get_data(row_id);

    $('#my-modal').modal();     
});
});

function get_data(row_id) {

    //either get data with ajax request or row
    //fill modals content with $('#input-field-id').val('vlaue') or .html()
}

回答by Ahmed Essam

You can do that in OctoberCMS backend. I'll give you a quick example. I put that in a controller _list_toolbar.htm

您可以在 OctoberCMS 后端执行此操作。我会给你一个简单的例子。我把它放在一个控制器 _list_toolbar.htm

<button
        href="javascript:;"
        data-control="popup"
        data-size="large"
        disabled="disabled"
        data-trigger-action="enable"
        data-trigger=".control-list input[type=checkbox]"
        data-trigger-condition="checked"
        onClick="$(this).popup({ handler: 'onAssignTechnical', extraData: { checked: $('.control-list').listWidget('getChecked') } })"
        href="javascript:;"
        class="btn btn-default oc-icon-plus">
        <?= e(trans('lilessam.maintenancesystem::lang.work_orders.assigntechnical_button')) ?>
    </button>

And in the onAssignTechnical() in the controller I'll make a bootstrap modal.

在控制器的 onAssignTechnical() 中,我将创建一个引导模式。

public function onAssignTechnical() {
        /** Check if this is even set **/
        if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {

            foreach($checkedIds as $workorder_checked_id) {
                $check_work_order = \Lilessam\Maintenancesystem\Models\WorkOrder::where(['id' => $workorder_checked_id, 'assigned_to_id' => null])->count();
            }

            /** Check if there's more than one record checked */
            if(count($checkedIds) == 1 && $check_work_order != 0):
                /**Labels **/
                $modal_title = Lang::get('lilessam.maintenancesystem::lang.assign_technical_title');
                $assign_label = Lang::get('lilessam.maintenancesystem::lang.assign_label');
                $name_label = Lang::get('lilessam.maintenancesystem::lang.assign_modal_name_label');
                $action_label = Lang::get('lilessam.maintenancesystem::lang.assign_modal_action_label');

                /** Getting all workers IDS by their group id 2 **/
                $allWorkers_ids = DB::table('backend_users_groups')->where('user_group_id', 2)->get();
                /** Workers Array **/
                $workers_array = [];

                // Looping to get all workers
                foreach($allWorkers_ids as $worker_id) {

                    //Fetching the user
                    $worker = User::find($worker_id->user_id);

                    //Pushing the worker to workers' array
                    array_push($workers_array, [
                                                'login'         => $worker->login,
                                                'first_name'    => $worker->first_name,
                                                'last_name'     => $worker->last_name,
                                                'id'            => $worker->id,
                                                ]);
                }

                #setting the table header
                $workers_code = '<div class="control-list list-unresponsive">
                                <table class="table data" data-control="rowlink">
                                    <thead>
                                        <tr>
                                            <th><a href="javascript:;">'.$name_label.'</a></th>
                                            <th style="width: 150px"><span>'.$action_label.'</span></th>
                                        </tr>
                                    </thead>
                                    <tbody>';

                #Adding workers to the table                  
                foreach($workers_array as $worker_row) {
                    foreach($checkedIds as $workorder_id):
                        $workers_code .= '<tr>
                                            <td>
                                                <a href="javascript:;">
                                                    '.$worker_row['first_name']." ".$worker_row['last_name'].'
                                                </a>
                                            </td>
                                            <td>
                                            <form method="post" data-request="onAssignNow">
                                                <input type="hidden" name="workorder_id" value="'.$workorder_id.'">
                                                <input type="hidden" name="id" value="'.$worker_row['id'].'">
                                                <button type="submit" class="btn btn-info">'.$assign_label.'</button>
                                            </form>
                                            </td>

                                        </tr>';
                    endforeach;

                }

                #Adding Table Fotter
                $workers_code .= "</tbody>
                                    </table>
                                        </div>";

                #Returning all modal
              return '
                    <div class="control-popup modal fade in" id="content-confirmation" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h4 class="modal-title">'.$modal_title.'</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="form-group  textarea-field span-full" data-field-name="idea" id="Form-field-Idea-idea-groupc" style="height: 395px;
                                        overflow-x: hidden;
                                        margin-bottom: 20px;
                                        overflow-y: scroll;">
                                        <div class="qa-message-list" id="wallmessages">
                                        '.$workers_code.'

                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">X</button>
                                </div>
                            </div>
                        </div>
                    </div>
                ';
            else:
                /** Labels **/
                $warning_title = Lang::get('lilessam.maintenancesystem::lang.assign_technical_warning_title');
                $warning_body = Lang::get('lilessam.maintenancesystem::lang.assign_technical_warning');

                #Returning all modal
                return '
                        <div class="control-popup modal fade in" id="content-confirmation" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                        <h4 class="modal-title">'.$warning_title.'</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group  textarea-field span-full" data-field-name="idea" id="Form-field-Idea-idea-groupc" style="height: 395px;
                                            overflow-x: hidden;
                                            margin-bottom: 20px;
                                            overflow-y: scroll;">
                                            <div class="qa-message-list" id="wallmessages">
                                            '.$warning_body.'
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">&#10006</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    ';    
            endif;

        }


     }

Excuse me for the long code but I'm sure It will help you.

请原谅我的长代码,但我相信它会帮助你。

回答by Mayank Pandeyz

$.ajax({
    url : 'url',
    method : 'post',
    data: {

    },
    success : function(response)
    {
        $('#my-modal').html(response);
        // put your html response in the popup div

        $('#my-modal').modal();
    }
});

<div id="my-modal"></div>

回答by sleakerz

I don't know if i understand well, but i think you need something like me :

我不知道我是否理解得很好,但我认为你需要像我这样的东西:

$("table").on('click', 'tr', function() {
        var json_data = {
            "controller": "NameOfController",
            "action": "action",
            "contentofRow": $(this).text()
        };
        $.post('ajax_handler.php', json_data, function (res) {
            if (res.indexOf('ERREUR') != -1) {
                alert(res);
                return false;
            }
            else {

                $('#mymodalID').modal();
                $("#myModalLabel").html("Content of the row : " + res[0]);
                $.each(res[1], function (index, valeur) {
                    $("#bodySupp").append($('<tr>')
                            .css("text-align", "center")
                            .css("fontSize", "12px")

                            .append($('<th>')
                                .html(valeur['indexOfValue'])
                        )
                            .append($('<th>')
                                .html(index)
                        )
                            .append($('<th>')
                                .html(valeur['indexOfValue2'])
                        )
                            .append($('<th>')
                                .html(valeur['indexOfValue3'])
                        )
                    )
                });

            }
        }, 'json');

    });

$("table").on('click', 'tr', function()

$("table").on('click', 'tr', function()

You open the modal on the click on the row of the table,

您单击表格行打开模态,

"contentofRow": $(this).text()

"contentofRow": $(this).text()

You take the content of the current row, this is what you will display :)

您获取当前行的内容,这就是您将要显示的内容:)

And then you have the controller :

然后你有控制器:

class NameOfController implements AjaxControler {

    public function execute($data)
    {
        switch($data['action']){
            default :
                $contentRow= $data['yourcontent'];
                $res = array();
                $res[] = $contentRow

                return json_encode($res);
        }
    }

}