Show and Hide the Popover

User Interaction

To specify when the Popover should be shown and hidden, set the showEvent and hideEvent options. These options can accept several events at once as well as an object.

JavaScript
$(function() {
    $("#popoverContainer").dxPopover({
        target: "#image",
        showEvent: 'dxhoverstart',
        hideEvent: 'dxhoverend'
    });
});

The Popover can also be hidden when a user clicks outside it or presses the Back button on the device. To control this behavior of the Popover, use the closeOnBackButton and closeOnOutsideClick options.

JavaScript
$(function() {
    $("#popoverContainer").dxPopover({
        target: "#image",
        showEvent: 'dxhoverstart',
        hideEvent: 'dxhoverend',
        closeOnBackButton: false,
        closeOnOutsideClick: false
    });
});

API

NOTE
In this article, the Button widget is used to demonstrate how to show and hide the Popover. This choice is made for purely demonstrational purposes, and you can do the same operations using another widget following the same guidelines.

To show or hide the Popover programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Popover, respectively.

JavaScript
$(function() {
    $("#popoverContainer").dxPopover({
        target: "#image",
        contentTemplate: function () {
            return $("<p />").text("Popover content");
        }
    });

    $("#showButton").dxButton({
        text: "Show the Popover", 
        onClick: function () {
            $("#popoverContainer").dxPopover("show");
            // ---------- or ----------
            $("#popoverContainer").dxPopover("toggle", true);
        } 
    });

    $("#hideButton").dxButton({
        text: "Hide the Popover", 
        onClick: function () {
            $("#popoverContainer").dxPopover("hide");
            // ---------- or ----------
            $("#popoverContainer").dxPopover("toggle", false);
        } 
    });
});

The show() method called without arguments shows the Popover for the target specified beforehand. If you need to change the target once, call the show(target) method.

JavaScript
$(function() {
    // ...
    $("#showButton").dxButton({
        text: "Show the Popover", 
        onClick: function () {
            $("#popoverContainer").dxPopover("show", "#newTarget");
        } 
    });
});

With AngularJS or Knockout, use a different technique. Bind the visible property of the Popover widget to a scope property (in AngularJS) or an observable variable (in Knockout). After that, change this scope property or observable variable, and the Popover will appear or disappear.

AngularJS

HTML
<div ng-controller="DemoController">
    <div dx-popover="{
        target: '#image',
        bindingOptions: {
            visible: 'isPopoverVisible'
        }
    }">
        Popover Content
    </div>
    <div dx-button="{
        text: 'Show the Popover',
        onClick: showPopover
    }"></div>
    <div dx-button="{
        text: 'Hide the Popover',
        onClick: hidePopover
    }"></div>
</div>
JavaScript
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.isPopoverVisible = false;
        $scope.showPopover = function () {
            $scope.isPopoverVisible = true;
        };
        $scope.hidePopover = function () {
            $scope.isPopoverVisible = false;
        };
    });

Knockout

HTML
<div data-bind="dxPopover: {
    target: '#image',
    visible: isPopoverVisible
}">
    Popover Content
</div>
<div data-bind="dxButton: {
    text: 'Show the Popover',
    onClick: function (e) {
        e.model.isPopoverVisible(true);
    }
}"></div>
<div data-bind="dxButton: {
    text: 'Hide the Popover',
    onClick: function (e) {
        e.model.isPopoverVisible(false);
    }
}"></div>
JavaScript
var viewModel = {
    isPopoverVisible: ko.observable(false)
};

ko.applyBindings(viewModel);

Events

To execute certain commands before or after the Popover was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the widget, assign it to the corresponding onEventName option when you configure the widget.

JavaScript
$(function () {
    $("#popoverContainer").dxPopover({
        // ...
        onShowing: function (e) {
            // Handler of the "showing" event
        },
        onShown: function (e) {
            // Handler of the "shown" event
        },
        onHiding: function (e) {
            // Handler of the "hiding" event
        },
        onHidden: function (e) {
            // Handler of the "hidden" event
        }
    });
});

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method.

JavaScript
var hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#popoverContainer").dxPopover("instance")
    .on('hidden', hiddenEventHandler1)
    .on('hidden', hiddenEventHandler2);
See Also