简体   繁体   English

具有链接的引导程序模式替换按钮-动态Div ID

[英]Bootstrap modal Replace Button with Link - Dynamic Div IDs

Using bootstrap to include modal within dynamic php loop. 使用引导程序将模式包含在动态php循环中。 Working wonderfully by dynamically changing the div ID by adding a unique field from each line in the iteration: 通过在迭代的每一行中添加唯一字段来动态更改div ID,从而实现出色的工作:

foreach($classes as $class => $details)
    {
     $unique = $class->['ID'];
     $name = $class->['Name';
     $description = $class->['Description';

    ?>
    <button class="btn btn-xs" data-toggle="modal" data-target="#myModal<?php echo $sclassid ?>">
<?php echo $name ?></button>
        <!-- Small modal -->
        <div id="myModal<?php echo $sclassid ?>" class='modal fade bs-example-modal-sm' tabindex='-1' role='dialog' aria-labelledby='mySmallModalLabel' aria-hidden='true'>
      <div class='modal-dialog modal-sm'>
        <div class='modal-content'>
                <?php echo $classDescription?>  
            <button type="button" class="btn btn-xs" data-dismiss="modal">Close</button>  
        </div>
      </div>
  </div>

If you're not generating unique IDs (#myModal-somevalue) then every instance of the modal will open up on each click. 如果您没有生成唯一的ID(#myModal-somevalue),则每次点击都会打开模态的每个实例。 Ouch. 哎哟。

At any rate - I could style the buttons to look like links, but is there a way to send the same information to the bootstrap jscript code using a link similar to this: 无论如何-我可以将按钮设置为看起来像链接的样式,但是有一种方法可以使用类似于以下的链接将相同的信息发送到bootstrap jscript代码:

<a href="javascript:void(0)" class="md-trigger" onclick="$('.bs-example-modal-sm').modal('show')"><?php echo $className ?>

Adding data-target="#myModal<?php echo $sclassid ?>" didn't work. 添加data-target="#myModal<?php echo $sclassid ?>"无效。

Is there any reason that one way or the other would be better. 有任何理由认为一种方法更好。

I look forward to your insights and feedback. 我期待着您的见解和反馈。

Off to read Eloquent Javascript by Marijn Haverbeke... 开始阅读Marijn Haverbeke的Eloquent Javascript ...

Instead of looping over the modal itself, why not set up -one- modal, and use jQuery's ajax() to inject another page into the modal container - no need for unique ID tomfoolery. 为什么不设置模态本身,而不是设置模态本身,为什么不设置-one-modal,并使用jQuery的ajax()将另一页注入到模态容器中-不需要唯一的ID假象。

Here's how I do it (this is in ColdFusion, so the # signs are the equivalent to PHP's variable markers) - also note this is Bootstrap 2.1.0, but a few structure/class changes will get it working in 3.1.1: 这是我的操作方法(这是ColdFusion中的符号,因此#符号等效于PHP的变量标记)-还请注意,这是Bootstrap 2.1.0,但是对结构/类进行一些更改后,它才能在3.1.1中工作:

Here's the URL to launch the modals (this could be output by your looping script, dynamically setting the url parameters to pass to the script that contains your modal content): 这是启动模式的URL(可以由循环脚本输出,动态设置url参数以传递到包含模式内容的脚本):

<a href="/modal/showDocHistory_Modal.cfm?docPropID=#GetMetaData.docPropID#&File=#URLEncodedFormat(name)#" data-target="#histModal" data-toggle="modal" rel="tooltip" data-placement="left" title="Click to view document history" class="btn btn-mini ajax"><i class="icon icon-list-alt"></i></a>

Next, you have the container for the modal: 接下来,您有模态的容器:

<!--- DIV container for history modal --->
<div class="modal hide fade" id="histModal" style="width:80%; margin-left:-40%;"></div>

Lastly, a bit of jQuery to ensure the URL passed to the modal is unique (otherwise, the last clicked link will always appear the next time the modal is launched): 最后,使用一些jQuery来确保传递给模式的URL是唯一的(否则,最后一次单击的链接将始终在下次启动模式时出现):

<!--- script to ensure ajax modal always loads currently clicked link --->
<script type="text/javascript">
$(document).ready(function() {
    $.ajaxSetup({ cache: false });
    $('[data-toggle="modal"].ajax').click(function(e) {
      e.preventDefault();
      var url = $(this).attr('href');
      var target = $(this).data('target');
      if (url.indexOf('#') == 0) {
      $(target).modal('open');
      } else {
          $.get(url, function(data) {
                              $(target).html(data);
                              $(target).modal();
          }).success(function() { $('input:text:visible:first').focus(); });
      }
    });
});
</script>

Now, all you need to do is loop over your HREF and provide the parameters to feed to the external script that contains your modal content (the external script will just have the modal body tags, since the main modal container will be in the calling script). 现在,您所需要做的就是遍历HREF并提供参数以馈送至包含模态内容的外部脚本(外部脚本将仅具有模式主体标签,因为主模式容器将位于调用脚本中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM