What's the best way to get Bootstrap popover content using Ajax?


Aycolmbro

I'm developing a WordPress plugin that displays a "special button" on the front end via a WordPress shortcode. I'm using Bootstrap 4, the buttons use Bootstrap popovers, I need to retrieve/get the popover title and content from the database without refreshing the page, so I need to use Ajax. I read that using async Ajax calls is deprecated (it slows down the page) So what is the best way to use Ajax with Bootstrap popups?

Edit: After Alex_89's reply, I wrote:

$( document ).ready( function() {
    // read EDIT 2 for working version...
} );

EDIT 2: ...after several tests it seems to work now!

$( document ).ready( function() {
    let buttons_with_popover = $( 'button[data-gdpr-popover="true"]' );

    buttons_with_popover.popover( {
        container: 'div.my-container',
        html: true,
        placement: 'top',
        title: '<div class="d-flex justify-content-between font-weight-bolder"><span class="header-text">Please wait...</span><span class="close-popover float-right ml-3"><i class="fas fa-times"></i></span></div>',
        content: '<div class="d-flex justify-content-center"><div class="spinner-border text-success" role="status"><span class="sr-only">Loading...</span></div></div>',
        trigger: 'click'
    } );

    buttons_with_popover.on( 'shown.bs.popover', function () {
        $( '.close-popover' ).on( 'click', function () {
            buttons_with_popover.popover( 'hide' );
        } );

        let this_button = $( this );
        this_button.data( 'popover_id', this_button.attr( 'aria-describedby' ) );
        let popover_id = this_button.data( 'popover_id' );
        let popover_header_text = $( '#' + popover_id + ' h3.popover-header span.header-text' );
        let popover_body = $( '#' + popover_id + ' div.popover-body' );

        $.ajax( {
            type: 'post',
            url: ajax.url,
            data: {
                action: 'queries_database',
                _nonce: ajax.nonce,
            },
            success: function ( queried_data ) {
                let options = $.parseJSON( queried_data ).options;

                popover_header_text.html( options.popover_title );
                popover_body.html( '<div><p class="text-justify">' + options.popover_body + '</p></div>' );

                this_button.popover( 'update' );
            },
            error: function () {
                popover_body.html( '<p class="text-warning">Ops! Something is wrong... <i class="far fa-tired"></i></p>' );
            }
        } );
    } );

    buttons_with_popover.on( 'hidden.bs.popover', function () {
        $( 'div.ccwhatsapp-container .popover' ).remove();
    } );

} );
Alex_89

Here is a snippet of a simple AJAX request that displays the result in a Bootstrap Popover . It should serve as an example of what you want to achieve. By the way, I don't know who would say that asynchronous AJAX requests are a bad practice, let alone deprecated. As @Damocles told you, this is completely wrong.

query code:

$(document).ready(function() {  
    let popoverBtn = $("#popover-button");
    let dataProviderURL = "url-to-your-wp-data-provider-function";

    popoverBtn.popover({
        title: "your-popover-title",
        placement: "top",
        html: true,
        content: "<div id='popover-body'><img src='path-to-your-preloader' alt='Loading...'></div>"
    });

    popoverBtn.on("shown.bs.popover", function () {
        $(".popover-close-btn").click(function () {
            popoverBtn.popover("hide");
        });

        let popoverTitle = $(".popover-header");
        let popoverBody = $(".popover-body");

        $.ajax({
            url: dataProviderURL,
            type: "GET",
            data: {id: "some-identifier-for-the-data-you-want-to-retrieve"},
            success: function (result) {
                popoverTitle.html(result.title);
                popoverBody.html(result.content);
                popoverBtn.popover("update");
            },
            error: function () {
                popoverBody.html("<em class='text-danger'>some-error-message.</em>");
            }
        });
    });
});

In this snippet, the initial content in the popover body is a preloader , which is an animated icon to indicate that data is being loaded (it's always a good practice to provide some feedback to the user), but it 's not required . You can leave it blank. I recommend checking out the Popper.js documentation for the different events you can attach functions to .

Related


What's the best way to center a list item using bootstrap?

Mohamed Sobhy I have this problem how to solve? Here is the HTML code: HTML <nav class="navbar navbar-default " role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header">

What's the best way to center a list item using bootstrap?

Mohamed Sobhy I have this problem how to solve? Here is the HTML code: HTML <nav class="navbar navbar-default " role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header">

What is the best way to get data using Ajax in AngularJS

Paid application I have a controller that MoviesListsCtrlis applied to 3 elements in an html document using ng-controller. If I use the service $httpinside a controller , it executes 3 times for the same data. What is the best way to retrieve json data once wi

What is the best way to get data using Ajax in AngularJS

Paid application I have a controller that MoviesListsCtrlis applied to 3 elements in an html document using ng-controller. If I use the service $httpinside a controller , it executes 3 times for the same data. What is the best way to retrieve json data once wi

What's the best way to get emoji images in chat using DiscordJS?

dusk My main goal is to be able to run a similar little command that $yoinkwill extract the last emoji sent in the chat into an image that I can download. I've got something like this, however, it can only detect regular images and embedded images (using it to

What's the best way to get emoji images in chat using DiscordJS?

dusk My main goal is to be able to run a similar little command that $yoinkwill extract the last emoji sent in the chat into an image that I can download. I've got something like this, however, it can only detect regular images and embedded images (using it to

What's the best way to get emoji images in chat using DiscordJS?

dusk My main goal is to be able to run a similar little command that $yoinkwill extract the last emoji sent in the chat into an image that I can download. I've got something like this, however, it can only detect regular images and embedded images (using it to

What's the best way to dynamic cell content?

nwice13 I'm using a collectionView to create a feed app and I've done most of the work, but I have some issues to improve my code: For example, I have a state cell like this: Normally, for dynamic cell content, I would resize the cell in the delegate sizeForIt

What's the best way to dynamic cell content?

nwice13 I'm using a collectionView to create a feed app, and I've got most of the work done, but I have some issues to improve my code: For example, I have a state cell like this: Normally, for dynamic cell content, I would resize the cell in the delegate size

twitter bootstrap popover that loads content on ajax

ardb I'm trying to assign a popover via twitter bootstrap to an element loaded via ajax, I have this: $(document).on("click", ".checkRezolvataTrecut", function(event){ switch(dataCheckTrecut[1]) { case '02': var lun

What's the best way to get started with OSGI?

Nicholas Trandem: What makes a module/service/bit of an application function a particularly good candidate for an OSGi module? I am interested in using OSGi in my application . We are a Java shop and we use Spring extensively, so I tend to use Spring dynamic m

What's the best way to get the current OS?

Ben Dorn I'm looking for something similar to python's sys.platform, which returns "linux", "windows", etc., or even better, python's platform module that gives you the OS, distro, distro, etc. def- when defined windows: echo "I'm on Windows!" elif defined l

What's the best way to get started with OSGI?

Nicholas Trandem: What makes a module/service/bit of an application function a particularly good candidate for an OSGi module? I am interested in using OSGi in my application . We are a Java shop and we use Spring extensively, so I tend to use Spring dynamic m

What's the best way to get the results of an action?

spacious I'm curious, if getting an action result is a valid approach, how does this work? For example, let's say I have a page with a form for creating an entity, after a successful entity creation I want to redirect the user to the entity's details view, oth

What's the best way to get the current OS?

Ben Dorn I'm looking for something similar to python's sys.platform, which returns "linux", "windows", etc., or even better, python's platform module that gives you the OS, distro, distro, etc. def- when defined windows: echo "I'm on Windows!" elif defined l

What's the best way to get started with OSGI?

Nicholas Trandem: What makes a module/service/bit of an application function a particularly good candidate for an OSGi module? I am interested in using OSGi in my application . We are a Java shop and we use Spring extensively, so I tend to use Spring dynamic m

What's the best way to get the results of an action?

spacious I'm curious, if getting an action result is a valid approach, how does this work? For example, let's say I have a page with a form for creating an entity, after successfully creating the entity I want to redirect the user to the entity's details view,