drmcm84800
2017-04-01 10:10
浏览 66

使用bootstrap动态模态进行删除确认(laravel blade)

My goal is to use bootstrap modal as delete confirmation. But the problem is I need to transfer a dynamic variable (which contains item ID) value to bootstrap modal for submit post request to delete that specific item.
Please check Modal Part where value="foo" is set now tell me how can I make this "foo" as dynamic variable which will be coming from laravel blade template part ("<td><a href="{{'#'}}" data-toggle="modal" data-target="#myModal">Delete</a></td>") Whats the solution? I prefer javascript solution.

Bootstrap Modal Part

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <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">Are you sure?</h4>
                </div>
                <form method="post">
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary" name="delete_dividend" value="foo">Delete</button>
                </div>
                </form>
            </div>
        </div>
    </div>

Laravel Blade Template Part

@foreach($dividends_10 as $_dividend_data)
                    <tr>
                      <td>{{Portfolio\StockModel::select('short_name')->where('id',$_dividend_data['stock_id'])->value('short_name')}}</td>
                      <td>{{$_dividend_data['amount']}}</td>
                      <td>{{$_dividend_data['date']}}</td>
                      <td><a href="{{'dividends'}}">Edit</a></td>
                      <td><a href="{{'#'}}" data-toggle="modal" data-target="#myModal">Delete</a></td>
                    </tr>
                  @endforeach
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanao6704 2017-04-01 10:28
    已采纳

    Assuming id field as Primary key of your table.
    First replace your Laravel Blade Template Part with below code::

    @foreach($dividends_10 as $_dividend_data)
    <tr>
      <td>{{Portfolio\StockModel::select('short_name')->where('id',$_dividend_data['stock_id'])->value('short_name')}}</td>
      <td>{{$_dividend_data['amount']}}</td>
      <td>{{$_dividend_data['date']}}</td>
      <td><a href="{{'dividends'}}">Edit</a></td>
      <td><a href="{{'#'}}" class="delete-mdoal" data-value="{{$_dividend_data['id']}}" data-toggle="modal" data-target="#myModal">Delete</a></td>
    </tr>
    @endforeach
    

    After that use below jQuery to made delete button value dynamic:

    $(document).ready(function (e) {
        $(document).on("click", ".delete-mdoal", function (e) {
            var delete_id = $(this).attr('data-value');
            $('button[name="delete_dividend"]').val(delete_id);
        });
    });
    
    点赞 打赏 评论

相关推荐 更多相似问题