Angular Popup - Show and Hide the Popup

API

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

To show or hide the Popup 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 Popup, respectively.

jQuery
JavaScript
$(function() {
    $("#popupContainer").dxPopup({
        title: "Popup Title",
        contentTemplate: function () {
            return $("<p />").text("Popup content");
        }
    });
    $("#showButton").dxButton({
        text: "Show the Popup", 
        onClick: function () {
            $("#popupContainer").dxPopup("show");
            // === or ===
            $("#popupContainer").dxPopup("toggle", true);
        } 
    });
    $("#hideButton").dxButton({
        text: "Hide the Popup", 
        onClick: function () {
            $("#popupContainer").dxPopup("hide");
            // === or ===
            $("#popupContainer").dxPopup("toggle", false);
        } 
    });
});
ASP.NET MVC Controls
Razor C#
Razor VB
@(Html.DevExtreme().Popup()
    .ID("popup")
    .Title("Popup Title")
    .ContentTemplate(@<text>
        <p>Popup content</p>
    </text>)
)

@(Html.DevExtreme().Button()
    .ID("showButton")
    .Text("Show the Popup")
    .OnClick(@<text>
        function () {
            $("#popup").dxPopup("show");
            // === or ===
            $("#popup").dxPopup("toggle", true);
        } 
    </text>)
)

@(Html.DevExtreme().Button()
    .ID("hideButton")
    .Text("Hide the Popup")
    .OnClick(@<text>
        function () {
            $("#popup").dxPopup("hide");
            // === or ===
            $("#popup").dxPopup("toggle", false);
        } 
    </text>)
)
@Code
    Html.DevExtreme().Popup() _
        .ID("popup") _
        .Title("Popup Title") _
        .ContentTemplate(Sub()
            @<text>
                <p>Popup content</p>
            </text>
        End Sub).Render()
    Html.DevExtreme().Button() _
        .ID("showButton") _
        .Text("Show the Popup") _
        .OnClick("showButton_click").Render()
    Html.DevExtreme().Button() _
        .ID("hideButton") _
        .Text("Hide the Popup") _
        .OnClick("hideButton_click").Render()
End Code

<script>
    function showButton_click() {
        $("#popup").dxPopup("show");
        // === or ===
        $("#popup").dxPopup("toggle", true);
    }
    function hideButton_click() {
        $("#popup").dxPopup("hide");
        // === or ===
        $("#popup").dxPopup("toggle", false);
    }
</script>

With Angular, Vue or React, use a different technique. Bind the visible property of the Popup UI component to a component property. After that, change this component property, and the Popup will appear or disappear.

Angular
HTML
TypeScript
<dx-popup
    title="Popup Title"
    [(visible)]="isPopupVisible">
    <div *dxTemplate="let data of 'content'">
        <p>Popup content</p>
    </div>
</dx-popup>
<dx-button
    text="Show the Popup"
    (onClick)="isPopupVisible = true">
</dx-button>
<dx-button
    text="Hide the Popup"
    (onClick)="isPopupVisible = false">
</dx-button>
import { DxPopupModule } from "devextreme-angular";
// ...
export class AppComponent {
    isPopupVisible: boolean = false;
}
@NgModule({
    imports: [
        // ...
        DxPopupModule
    ],
    // ...
})
Vue
<template>
    <div>
        <DxPopup
            title="Popup Title"
            v-model:visible="isPopupVisible">
            <template>
                <p>Popup content</p>
            </template>
        </DxPopup>
        <DxButton
            text="Show the Popup"
            @click="showPopup"
        />
        <DxButton
            text="Hide the Popup"
            @click="hidePopup"
        />
    </div>
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import { DxPopup } from 'devextreme-vue/popup';
import { DxButton } from 'devextreme-vue/button';

export default {
    components: {
        DxPopup,
        DxButton
    },
    data() {
        return {
            isPopupVisible: false
        }
    },
    methods: {
        showPopup() {
            this.isPopupVisible = true;
        },
        hidePopup() {
            this.isPopupVisible = false;
        }
    }
}

</script>
React
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { Popup } from 'devextreme-react/popup';
import { Button } from 'devextreme-react/button';

const renderContent = () => {
    return (
        <p>Popup content</p>
    );
};

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isPopupVisible: false
        };

        this.showPopup = this.showPopup.bind(this);
        this.hidePopup = this.hidePopup.bind(this);
    }

    showPopup() {
        this.setState({
            isPopupVisible: true
        });
    }

    hidePopup() {
        this.setState({
            isPopupVisible: false
        });
    }

    render() {
        return (
            <div>
                <Popup
                    title="Popup Title"
                    visible={this.state.isPopupVisible}
                    contentRender={renderContent}
                    onHiding={this.hidePopup}
                />
                <Button
                    text="Show the Popup"
                    onClick={this.showPopup}
                />
                <Button
                    text="Hide the Popup"
                    onClick={this.hidePopup}
                />
            </div>
        );
    }
}

export default App;

User Interaction

The Popup can also be hidden when a user clicks outside it. To control this behavior of the Popup, use the closeOnOutsideClick property.

jQuery
JavaScript
$(function() {
    $("#popupContainer").dxPopup({
        title: "Popup Title",
        visible: true,
        closeOnOutsideClick: true
    });
});
Angular
HTML
TypeScript
<dx-popup
    title="Popup Title"
    [(visible)]="isPopupVisible"
    [closeOnOutsideClick]="true">
</dx-popup>
import { DxPopupModule } from "devextreme-angular";
// ...
export class AppComponent {
    isPopupVisible: boolean = true;
}
@NgModule({
    imports: [
        // ...
        DxPopupModule
    ],
    // ...
})
Vue
<template>
    <DxPopup
        v-model:visible="isPopupVisible"
        :close-on-outside-click="true"
        title="Popup Title"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import { DxPopup } from 'devextreme-vue/popup';

export default {
    components: {
        DxPopup
    },
    data() {
        return {
            isPopupVisible: true
        };
    }
}
</script>
React
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { Popup } from 'devextreme-react/popup';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isPopupVisible: true
        };

        this.onHiding = this.onHiding.bind(this);
    }

    onHiding() {
        this.setState({
            isPopupVisible: false
        });
    }

    render() {
        return (
            <Popup
                visible={this.state.isPopupVisible}
                closeOnOutsideClick={true}
                title="Popup Title"
            />
        );
    }
}

export default App;

Events

To execute certain commands before or after the Popup 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 UI component, assign it to the corresponding onEventName property when you configure the UI component.

jQuery
JavaScript
$(function () {
    $("#popupContainer").dxPopup({
        // ...
        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
        }
    });
});
Angular
HTML
TypeScript
<dx-popup ...
    (onShowing)="popup_showing($event)"
    (onShown)="popup_shown($event)"
    (onHiding)="popup_hiding($event)"
    (onHidden)="popup_hidden($event)">
</dx-popup>
import { DxPopupModule } from "devextreme-angular";
// ...
export class AppComponent {
    popup_showing (e) {
        // Handler of the "showing" event
    }
    popup_shown (e) {
        // Handler of the "shown" event
    }
    popup_hiding (e) {
        // Handler of the "hiding" event
    }
    popup_hidden (e) {
        // Handler of the "hidden" event
    }
}
@NgModule({
    imports: [
        // ...
        DxPopupModule
    ],
    // ...
})
Vue
<template>
    <DxPopup ...
        @showing="onShowing"
        @shown="onShown"
        @hiding="onHiding"
        @hidden="onHidden"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import { DxPopup } from 'devextreme-vue/popup';

export default {
    components: {
        DxPopup
    },
    methods: {
        onShowing(e) {
            // Handler of the 'showing' event
        },
        onShown(e) {
            // Handler of the 'shown' event
        },
        onHiding(e) {
            // Handler of the 'hiding' event
        },
        onHidden(e) {
            // Handler of the 'hidden' event
        }
    }
}
</script>
React
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { Popup } from 'devextreme-react/popup';

class App extends React.Component {
    onShowing(e) {
        // Handler of the 'showing' event
    }

    onShown(e) {
        // Handler of the 'shown' event
    }

    onHiding(e) {
        // Handler of the 'hiding' event
    }

    onHidden(e) {
        // Handler of the 'hidden' event
    }

    render() {
        return (
            <Popup ...
                onShowing={this.onShowing}
                onShown={this.onShown}
                onHiding={this.onHiding}
                onHidden={this.onHidden}
            />
        );
    }
}

export default App;

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. This approach is more typical of jQuery.

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

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

$("#popupContainer").dxPopup("instance")
    .on("hidden", hiddenEventHandler1)
    .on("hidden", hiddenEventHandler2);
See Also