douyunjiaok300404 2017-03-11 14:48
浏览 29
已采纳

确认Dialog在回调中传递动作

for my laravel app on bootstrap 3 I am attempting to do a basic application wide confirm dialog box using the code below:

<button type="button" class="btn btn-default" 
     data-toggle="modal" 
     data-target="#confirmActionModal" 
     data-backdrop="false"
     data-title="Cancel Subscription" 
     data-body="Please confirm the cancellation of your subscription."
     data-modal-primary-button-text="Cancel Subscription"
>
   Cancel Subscription
</button>

Modal:

<div class="modal fade" id="confirmActionModal" 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="modalTitle"> Default Title </h4>
            </div>
            <div class="modal-body" id="modalBody">
                Default 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">Default Button Text</button>
            </div>
        </div>
    </div>
</div>

JS:

 $('#confirmActionModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget) ;// Button that triggered the modal
            var modalTitle = button.data('title'); // Extract info from data-* attributes
            var modalBody = button.data('body');
            var modalActionButtonText = button.data('modal-primary-button-text');
            var modal = $(this);
            modal.find('.modal-title').text(modalTitle);
            modal.find('.modal-body').text(modalBody);
            modal.find('.btn-primary').text(modalActionButtonText);
        })

Since the confirm dialog is generic across the application, I would like the ability to pass in the call back ajax function name also. So for instance when someone clicks the primary action button (in this case cancel subscription), I would like to also pass in the ajax call back as I am passing in title and body for a javascript function like cancelSubscription() that basically does the action. Any tips or is my approach flawed?

  • 写回答

1条回答 默认 最新

  • dsfsdfsd34324 2017-03-11 15:08
    关注

    You have the ability to pass variables to the either the JQuery function or the html markup via your controllers. For example:

    PageController.php

    public function page()
    {
        return view('template', [
            'ajax-function' => "myAjaxFunctionName"
        ]);
    }
    

    template.blade.php

    <button type="button" class="btn btn-default" 
         data-toggle="modal" 
         data-target="#confirmActionModal" 
         data-backdrop="false"
         data-title="Cancel Subscription" 
         data-body="Please confirm the cancellation of your subscription."
         data-modal-primary-button-text="Cancel Subscription"
         data-ajax-function="{!! $ajax-function !!}"
    >
       Cancel Subscription
    </button>
    

    You could therefore use your controller to determine the name of each function you wish to use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作