Search code examples
javascriptjqueryruby-on-railstwitter-bootstrappopover

Popover not showing on first click for dynamically generated content


I'm trying to get a popover for my webpage. It doesn't work for me on the first click but however works fine thereafter. I realize that it gets instantiated on the first click and therefore doesn't show up. Is there a better way to instantiate it, given that I'm using id's that are generated at runtime. I have looked at similar questions but they don't seem to work for me. Here is the code I have written,

    <table id="userInfo">
  <thead>
       <tr>
        <th>UserGroup</th>
      </tr>
  </thead>

  <tbody>
  <% @userdetails.each do |user| %>
      <tr>
        <td>
          <a class='user_show_more' data-placement='left' id='<%= user %>'>[+]</a>
              <!-- popover table starts here -->
              <div id="<%= user %>_popover" style="display: none">
                <table>
                  <thead>
                  <tr>
                    <th>UserNames</th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr>
                    <td>
                      <%= user['name'] %>
                    </td>
                  </tr>
                  </tbody>
                </table>
              </div>
              <!-- popover ends here -->
          <% user['group'] %>
        </td>
      </tr>
  <% end %>
  </tbody>
</table>

And my javascript code looks like this,

    $('.user_show_more').on('click', function (e) {
        var $this = $(this)
        var popover_id = '#'+$this.attr("id");
        $(popover_id).popover({
            html : true,
            content: function() {
                var popover = '#'+$this.attr("id")+'_popover';
                return $(popover).html();
            }
        });
    });

Solution

  • You can add two click handlers to your popovers. The first one uses 'one' and the second one uses 'on'.

    https://jsbin.com/kegovebufu/1/edit?html,js,console,output

    HTML

      <button type="button"
              class="btn btn-lg btn-danger"
              data-toggle="popover" 
              id="Temp"
              >Stuff</button> 
    
    
      <script type="text/html" id="Temp_popover">
       <div>  
          <table>
            <thead>
              <tr>
               <th>UserNames</th> 
              </tr>
            </thead>
             <tbody>
              <tr>
               <td>blah</td>
              </tr>
             </tbody>
           </table>
        </div>      
      </script>
    

    Javascript

        function PopoverClick()
    {
      $(this).popover('toggle'); 
    }
    
    
    
    
    $('[data-toggle="popover"]').one('click',function(e){
    
      console.log('once');
      var _this = $(this);
    
    
      _this.popover(
      {
        html:true,
        content: function()
        {
          return $('#'+$(this).attr("id")+'_popover').html();         
        },
        trigger: 'manual',
        placement:'right'    
      }).popover('show');  
    
      _this.on('click',PopoverClick);
    
    });