0

Using bootstrap to include modal within dynamic php loop. Working wonderfully by dynamically changing the div ID by adding a unique field from each line in the iteration:

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. 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:

<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.

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...

MikeiLL
  • 6,282
  • 5
  • 37
  • 68
  • I'm not sure what your question actually is. What are you asking? – George Marques Mar 18 '14 at 20:21
  • @GeorgeMarques I think the question is, "Is there a better way to do this than dynamically generating unique IDs with PHP and styling a button to look like a link?" I have a feeling ajax is a more straight-forward approach, but am not exactly sure why (yet). – MikeiLL Mar 18 '14 at 21:33

1 Answers1

0

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.

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:

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):

<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):

<!--- 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).

Joyrex
  • 1,103
  • 14
  • 24
  • I think this is the input I was looking for. So would the URL I'm passing be another script or another function in the same script that would retrieve the variable(s) based on parameters generated by the first loop and included in the sent URL? – MikeiLL Mar 18 '14 at 21:35
  • Yes - they could come from a database query, form input from a previous screen, etc. Also notice that the "template" for the modal is just the outside DIVs - the script you insert into the modal template will contain the rest of the modal HTML structure (hope that makes sense) – Joyrex Mar 18 '14 at 21:50
  • so then the modal DIVs will be divided, with the outer one on the hosting script and the inner DIVs on the called script? I am getting a modal body open but there's an empty DIV in it. `` I have tested with just a regular link and am seeing the desired data on the called page. There's no Javascript error in the console and I'm not sure where to look for clues. – MikeiLL Mar 18 '14 at 22:46
  • `alert("I am an alert box!");` added to troubleshoot. pops up below `$(document).ready(function() { $.ajaxSetup({ cache: false });` but not after `$('[data-toggle="modal"].ajax').click(function(e) {` I know that he first two lines mean "when the document is fully loaded perform this function: 1. set up ajax and tell it not to cache contents". Am guessing the third line calls an ajax function or class called modal? – MikeiLL Mar 18 '14 at 22:58
  • [link](http://stackoverflow.com/questions/12449890/reload-content-in-modal-twitter-bootstrap) had some relevant code which combined with above has this mostly working. will post solution, however the problem at the moment is that the modal window is spanning the entire width of the browser window, as opposed to just part of. – MikeiLL Mar 18 '14 at 23:41
  • sounds like something wrong in your java script for it to do that... you could try $(".modal").css('height',$( window ).height()*0.6); $(".modal").css('width',$( window ).width()*0.6); to see if it makes any diff – V H Mar 19 '14 at 21:31
  • Mike iLL: Yes, the calling script contains the outer empty DIV for the modal, and the script containing the modal contents go into it. Regarding the width, see my code - it has a inline style attached to the modal container that if you remove it, the size will be Bootstrap's normal size - sorry for not stripping that out beforehand. – Joyrex Mar 20 '14 at 21:16