代码之家  ›  专栏  ›  技术社区  ›  user2707039

带远程Modal的Bootstrap 3

  •  72
  • user2707039  · 技术社区  · 12 年前

    我刚刚开始了一个新项目,发布了新的Twitter引导程序:引导程序3。 我无法使Modal在远程模式下工作。我只想当我点击一个链接时,它会显示带有远程url内容的模态。 它有效,但模态布局被完全破坏了。

    这里有一个指向jsfiddle的链接: http://jsfiddle.net/NUCgp/5/

    代码:

    <a data-toggle="modal" href="http://fiddle.jshell.net/Sherbrow/bHmRB/0/show/" data-target="#myModal">Click me !</a>
    
    <!-- Modal -->
    <div class="modal fade" id="myModal" 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-hidden="true">&times;</button>
                     <h4 class="modal-title">Modal title</h4>
    
                </div>
                <div class="modal-body"><div class="te"></div></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>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
    

    有人能让这个简单的例子奏效吗?

    9 回复  |  直到 9 年前
        1
  •  112
  •   Ian Kemp    9 年前

    关于模态的远程选项,来自 the docs :

    如果提供了远程URL,则将通过jQuery的加载加载内容 方法和注射 转换为模态元素的根 .

    这意味着您的远程文件应该提供完整的模态结构,而不仅仅是您想要在主体上显示的内容。

    Bootstrap 3.1更新:

    在v3.1中,上述行为已更改,现在远程内容已加载到 .modal-content

    看看这个 Demo fiddle

    Boostrap 3.3更新:

    自v3.3.0版本以来,此选项已被弃用,并且已在v4版本中删除。 我们建议改用客户端模板或数据绑定框架,或者调用 jQuery.load 你自己

        2
  •  29
  •   zorkle    11 年前

    用于引导程序3

    我必须处理的工作流程是加载带有可能更改的url上下文的内容。因此,默认情况下,使用javascript或href设置您想要显示的默认上下文的模态:

    $('#myModal').modal({
            show: false,
            remote: 'some/context'
    });
    

    破坏模态对我来说不起作用,因为我不是从同一个遥控器加载的,因此我必须:

    $(".some-action-class").on('click', function () {
            $('#myModal').removeData('bs.modal');
            $('#myModal').modal({remote: 'some/new/context?p=' + $(this).attr('buttonAttr') });
            $('#myModal').modal('show');
    });
    

    当然,这很容易被重构到js库中,并且在加载模式方面给了你很大的灵活性

    我希望这能为某人节省15分钟的时间。

        3
  •  18
  •   MM. trayzey    12 年前

    如果你不想发送完整的模态结构,你可以复制旧的行为,这样做:

    // this is just an example, remember to adapt the selectors to your code!
    $('.modal-link').click(function(e) {
        var modal = $('#modal'), modalBody = $('#modal .modal-body');
    
        modal
            .on('show.bs.modal', function () {
                modalBody.load(e.currentTarget.href)
            })
            .modal();
        e.preventDefault();
    });
    
        4
  •  13
  •   TheAdventurist    11 年前

    这是我的解决方案(利用上面的一些),它利用BS3自己的结构来恢复旧的远程加载行为。它应该是无缝的。

    我将保持代码变量的沉重性和描述性,以使事情易于理解。 我还假设存在JQuery。Javascript重载类型将方便地简化代码。

    为了参考,这里有一个调用BS3模态的链接:

    <li><a data-toggle="modal" href="terms.html" data-target="#terms">Terms of Use</a></li>
    

    在您的Javascript中,您将需要以下内容。

    // Make sure the DOM elements are loaded and accounted for
    $(document).ready(function() {
    
      // Match to Bootstraps data-toggle for the modal
      // and attach an onclick event handler
      $('a[data-toggle="modal"]').on('click', function(e) {
    
        // From the clicked element, get the data-target arrtibute
        // which BS3 uses to determine the target modal
        var target_modal = $(e.currentTarget).data('target');
        // also get the remote content's URL
        var remote_content = e.currentTarget.href;
    
        // Find the target modal in the DOM
        var modal = $(target_modal);
        // Find the modal's <div class="modal-body"> so we can populate it
        var modalBody = $(target_modal + ' .modal-body');
    
        // Capture BS3's show.bs.modal which is fires
        // immediately when, you guessed it, the show instance method
        // for the modal is called
        modal.on('show.bs.modal', function () {
                // use your remote content URL to load the modal body
                modalBody.load(remote_content);
            }).modal();
            // and show the modal
    
        // Now return a false (negating the link action) to prevent Bootstrap's JS 3.1.1
        // from throwing a 'preventDefault' error due to us overriding the anchor usage.
        return false;
      });
    });
    

    我们就在那里。你可能想做的一件事是用最大高度来设置模态主体的样式,这样长的内容就会滚动。

    在CSS中,您需要以下内容:

    .modal-body{
        max-height: 300px;
        overflow-y: scroll;
    }
    

    为了参考,我将包括模态的HTML,它是你见过的每一个Bootsrap模态示例的翻版:

    <div id="terms" class="modal fade">
      <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>
            <h3 id="termsLabel" class="modal-title">TERMS AND CONDITIONS</h3>
          </div>
          <div class="modal-body">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    
        5
  •  9
  •   MBHNYC    11 年前

    我这样做了:

    $('#myModal').on 'shown.bs.modal', (e) ->  
      $(e.target).find('.modal-body').load('http://yourserver.com/content')
    
        6
  •  8
  •   esvendsen    12 年前

    尽管我不喜欢修改Bootstrap代码(这会使升级更加困难),但您可以简单地在modal.js中的load语句中添加“.find('.modal body'),如下所示:

    // original code
    // if (this.options.remote) this.$element.load(this.options.remote)
    
    // modified code
    if (this.options.remote) this.$element.find('.modal-body').load(this.options.remote)
    
        7
  •  5
  •   just a slime    10 年前

    这是我使用的方法。它不需要页面上的任何隐藏DOM元素,只需要一个具有模态分部href的锚标记和一个类“modalTrigger”。当模态关闭(隐藏)时,它将从DOM中删除。

      (function(){
            // Create jQuery body object
            var $body = $('body'),
    
            // Use a tags with 'class="modalTrigger"' as the triggers
            $modalTriggers = $('a.modalTrigger'),
    
            // Trigger event handler
            openModal = function(evt) {
                  var $trigger = $(this),                  // Trigger jQuery object
    
                  modalPath = $trigger.attr('href'),       // Modal path is href of trigger
    
                  $newModal,                               // Declare modal variable
    
                  removeModal = function(evt) {            // Remove modal handler
                        $newModal.off('hidden.bs.modal');  // Turn off 'hide' event
                        $newModal.remove();                // Remove modal from DOM
                  },
    
                  showModal = function(data) {             // Ajax complete event handler
                        $body.append(data);                // Add to DOM
                        $newModal = $('.modal').last();    // Modal jQuery object
                        $newModal.modal('show');           // Showtime!
                        $newModal.on('hidden.bs.modal',removeModal); // Remove modal from DOM on hide
                  };
    
                  $.get(modalPath,showModal);             // Ajax request
    
                  evt.preventDefault();                   // Prevent default a tag behavior
            };
    
            $modalTriggers.on('click',openModal);         // Add event handlers
      }());
    

    要使用,只需使用模态分部的href创建一个标记:

    <a href="path/to/modal-partial.html" class="modalTrigger">Open Modal</a>
    
        8
  •  5
  •   Tim Kretschmer vee    9 年前

    另一个简单易行的方法是 失明的 布局中的模态,必要时调用它。

    JS公司

      var remote_modal = function(url) {
        // reset modal body with a spinner or empty content
        spinner = "<div class='text-center'><i class='fa fa-spinner fa-spin fa-5x fa-fw'></i></div>"
    
        $("#remote-modal .modal-body").html(spinner)
        $("#remote-modal .modal-body").load(url);
        $("#remote-modal").modal("show");
      }
    

    和您的HTML

     <div class='modal fade' id='remote-modal'>
        <div class='modal-dialog modal-lg'>
          <div class='modal-content'>
            <div class='modal-body'></div>
            <div class='modal-footer'>
              <button class='btn btn-default'>Close</button>
            </div>
          </div>
        </div>
      </div>
    </body>
    

    现在你可以简单地打电话 remote_modal('/my/url.html') 并且内容显示在模态内部

        9
  •  0
  •   yardpenalty.com    9 年前

    我是这样做的:

    <!-- global template loaded in all pages // -->
         <div id="NewsModal" class="modal fade" tabindex="-1" role="dialog" data-ajaxload="true" aria-labelledby="newsLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 class="newsLabel"></h3>
                        </div>
                        <div class="modal-body">
                                <div class="loading">
                                    <span class="caption">Loading...</span>
                                   <img src="/images/loading.gif" alt="loading">
                            </div>
                        </div>
                        <div class="modal-footer caption">
                            <button class="btn btn-right default modal-close" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
    

    这是我的a href:

    <a href="#NewsModal" onclick="remote=\'modal_newsfeed.php?USER='.trim($USER).'&FUNCTION='.trim(urlencode($FUNCTIONCODE)).'&PATH_INSTANCE='.$PATH_INSTANCE.'&ALL=ALL\'
                                            remote_target=\'#NewsModal .modal-body\'" role="button" data-toggle="modal">See All Notifications <i class="m-icon-swapright m-icon-dark"></i></a>