Another solution:
$target.find('.myPopOver').mouseenter(function() { if($(this).data('popover') == null) { $(this).popover({ animation: false, placement: 'right', trigger: 'manual', title: 'My Dynamic PopOver', html : true, template: $('#popoverTemplate').clone().attr('id','').html() }); } $(this).popover('show'); $.ajax({ type: HTTP_GET, url: "/myURL" success: function(data) {
The idea here is to manually trigger a PopOver display with mouseenter and mouseleave events .
In mouseenter , if PopOver ( if ($ (this) .data ('popover'))> null) ) is not created for your element, create This. Interestingly, you can define your own PopOver content by passing it as an argument ( template ) to the popover () function. Remember to set the html parameter to true .
Here I just create a hidden template called popovertemplate and clone it using jQuery. Remember to remove the id attribute after cloning it , otherwise you will get duplicate identifiers in the DOM. Also note that style = "display: none" to hide the template on the page.
<div id="popoverTemplateContainer" style="display: none"> <div id="popoverTemplate"> <div class="popover" > <div class="arrow"></div> <div class="popover-inner"> //Custom data here </div> </div> </div> </div>
After the creation step (or if it has already been created) you simply display popOver with $ (this) .popover ('show');
Then a classic Ajax challenge. If successful, you need to clear the old popover content before entering new fresh data from the server . How can we get the current popover content? Using the .popover.in selector! The .in class indicates that a popover is currently being displayed, that the trick is here!
To complete the mouseleave event, just hide the popover.
doanduyhai May 01 '12 at 21:31 2012-05-01 21:31
source share