0

Let me state first of all that there are a lot of articles out there asking about simply loading an Ajax call's data into a Bootstrap 4.0 popover; this question regards a slightly different topic.

The problem I have been having with my popover is that, after it is loaded, the element shows up in an unusual position on the page, as demonstrated below:

before clicking pop-over going off-screen

Rather than load above the sticky note icon, the popover appears below and proceeds to bleed off-screen (there are supposed to be seven entries inside it). The reason why seems to be because, when the popover initially appears as a view container, none of the inner data/html has been loaded yet -- and once that data does load, the popover displays it without adjusting itself. A trick I've found to adjusting the containerized view is to simply scroll the page, which essentially refreshes the popover itself.

The code for the sticky note button and popover container is as follows:

<script>
    $(function () {
        $('[data-toggle="popover"]').on('inserted.bs.popover', function (evt) {
            getViewNoteModal($(evt.target));
        });
    });
    function getViewNoteModal(caller) {
        var $caller = $(caller);
        var $noteContainer = $('#editNotes');
        // The $caller is used to retrieve parameters.
        ...
        $.ajax({
            // GetNoteViewer is a C# method that returns the view that goes into #editNotes.
            url: "@Url.Action("GetNoteViewer")",
            method: "GET",
            data: {
                // parameters...
            },
            success: function (data) {
                $noteContainer.html(data);
            }
        });
    }
</script>
...
<i class="fas fa-2x fa-sticky-note text-info show-dialog" data-placement="top"
    data-toggle="popover" data-html="true" data-trigger="click" 
    data-template='<div class=" popover card-primary" role="tooltip">
        <div class="arrow"></div>
        <h3 class="popover-header card-header"></h3>
        <div class="popover-body"></div>
    </div>'
   data-content="<div id='editNotes'>">
</i>

It seems the most logical thing to do would be to manually dispose of the popover and promptly show it via $('[data-toggle="popover"]').popover('show'); inside the success block of the Ajax function. The problem with this approach is that disposing the popover also removes the data inside it -- and just "showing" it without disposing it results in an endless loop of callbacks to the popover.

At this point, I am very close to getting the popover to show as it should, as all the functions inside it work perfectly and the formatting of the view inside is also formatted as intended. The only issue now is getting the popover to show up in the correct spot the first time without having to pull off any tricks to set it in the right position.

5
  • You're switching the content using .html() - what happens when you use the setContent method instead?
    – CBroe
    Commented Jul 8 at 7:15
  • @CBroe setContent is not a function as far as I’m concerned, and is only referenced in bootstrap files.
    – Freerey
    Commented Jul 8 at 12:31
  • @CBroe what you're referring to looks like it is a newer method. It's possible that I don't see this method because I'm on Bootstrap 4
    – Freerey
    Commented Jul 8 at 12:39
  • 1
    Okay, have you tried the update method then? That is supposed to update the position, getbootstrap.com/docs/4.6/components/popovers/#popoverupdate
    – CBroe
    Commented Jul 8 at 12:59
  • @CBroe I did not try that -- and now that I have, it worked as intended. Thank you! If you want to post your solution as the answer, I'll mark it as accepted.
    – Freerey
    Commented Jul 8 at 13:05

1 Answer 1

1

You're switching out the content in your AJAX callback, with

$noteContainer.html(data);

- but that is apparently not enough, to make the popover "re-adjust" itself.

The update method can be used to make it re-calculate its position, https://getbootstrap.com/docs/4.6/components/popovers/#popoverupdate:

.popover('update')
Updates the position of an element’s popover.

$('#element').popover('update')

Not the answer you're looking for? Browse other questions tagged or ask your own question.