weixin_33701251 2015-02-16 14:01 采纳率: 0%
浏览 15

Bootstrap模态Ajax

I have a bootstrap table with data. Need to edit that data and set the value of some fields that are not displayed with the table.

I added a edit button to each row, and a modal form. The button is loading the modal form with no issue.

I have 3 questions.

  1. How to load the modal with the data for the row associated with the button.
  2. How to save the edited data in the modal when the save button is clicked.
  3. How to refresh the table after the modal closes when the save button was clicked.

I'm assuming that I'd be better off with a tutorial, but I'll be danged if I can find one.

The table code is just basic bootstrap table.

Current code for the button.

<button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#checkInModal">Check In</button>

Current code for the modal. (for brevity sake i removed all the fields from the snippet.)

  <!-- Modal -->
    <div class="modal fade" id="checkInModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Check In</h4>
                </div>
                <div class="modal-body">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
  • 写回答

3条回答 默认 最新

  • weixin_33695450 2015-02-16 14:23
    关注

    When the edit button is clicked you would need to find the closest element relative to the button. In jQuery you can do it like this:

    <button type="button" data-toggle="modal" data-target="#myModal"></button>
    
    
    $('.btn').on('click', function(){
       var $row = $(this).closest('tr');
    })
    

    Then you can loop through all the cells and retrieve the contents and place them into the modal body area.

    var $row = $(this).closest('tr');
    
    var $modalContentArea = $('#myModal .modal-body');
    
    $row.find('td').each(function(){
    
       var $cell = $(this);
    
       var cellContents = $cell.text();
    
       $modalContentArea.append('<input type="text" value="' + cellContents +'"/>');
    
    });
    

    The save button would need to be connected to an ajax request of some sort. You would need to construct a JSON object from the data collected. Then you could use the $get function of jQuery to send the request with the JSON object as the parameter.

    $('btn-primary').on('click', function(){
    
        $.get( "process.php", function( data ) {
    
            //The ajax callback can populate the table with the new values.
    
        })
    
    });
    

    In the backend you would need to retrieve the JSON object, parse it, and save it to a database. Then return the new values to the ajax callback.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: