0

I have a bunch of elements dynamically created and I'm able to show the popover calling this script:

$('.gantt').popover({
    selector: '.bar',
    title: 'Test title',
    content: 'Test content',
    trigger: 'hover',
    placement: function() { return 'top' },
    container: 'body'
});

Now, I need to dynamically pass title and content from within a function and then I did like so:

[...]
onItemMouseover: function(elm, data) {
    elm.popover({
        selector: '.bar',
        title: data.title,
        content: data.content,
        trigger: 'hover',
        placement: function() { return 'top' },
        container: 'body'
    });
}
[...]

But the popover is not opening. Is there something missing?

If i call elm.remove() the element is being removed, so elm is triggering correctly the DOM element.

Mark
  • 645
  • 2
  • 9
  • 27
  • @Raviteja yes i am – Mark Nov 23 '16 at 10:44
  • Could you please create a [fiddle](https://jsfiddle.net/) ? – Raviteja Nov 23 '16 at 10:46
  • @Raviteja unfortunately I'm not using an online library so it might be difficult for me to replicate everything online – Mark Nov 23 '16 at 10:48
  • is the popover opening ? if yes sound like your data var is not correct, console.log it – Eric V. Nov 23 '16 at 10:54
  • FYI http://stackoverflow.com/questions/21459042/can-i-use-dynamic-content-in-a-bootstrap-popover – Raviteja Nov 23 '16 at 10:58
  • @EricV. no, problem is that it is not opening at all – Mark Nov 23 '16 at 11:05
  • @Raviteja following the example from your link, i should retrieve the data with Ajax "on the run". This will for sure cause latency. I already have my data inside the second example. I only need to know how to open the popover... – Mark Nov 23 '16 at 11:08
  • put alerts and check if your function is getting called. Also check `data.title, data.content` – Raviteja Nov 23 '16 at 11:10
  • @Raviteja calling `elm.remove()` or logging data.title works correctly so: function is being called, elm contains the correct element, data contains the correct data – Mark Nov 23 '16 at 11:15
  • try adding `elem.popover('show');` after `container: 'body' });` – Raviteja Nov 23 '16 at 11:31
  • @Raviteja doesn't work – Mark Nov 23 '16 at 11:54

1 Answers1

0

This is a patch, probably there's a better and official way of doing this, but I solved this way:

  • created a popoverContent global variable
  • assigned data.content to popoverContent inside onItemMouseover
  • called the first version of the script and retrieved the content value from the popoverContent variable

If you have a better solution, please share.


UPDATE

Here's the code:

var popoverContent

$(document).ready(function () {
    createGantt(data);

    $('.gantt').popover({
        [...]
        content: function () {
            return popoverContent;
        },
        [...]
    });
}

function createGantt(data) {
    $('.gantt').gantt({
        [...]
        onItemMouseover: function(elm, data) {
            popoverContent = data.detailContent;
        },
    });
};
Mark
  • 645
  • 2
  • 9
  • 27