API
To show or hide the Toast 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 Toast, respectively.
$(function() { $("#toastContainer").dxToast({ message: "Connection problem", type: "error" }); $("#buttonContainer").dxButton({ text: "Show the Toast", onClick: function () { $("#toastContainer").dxToast("show"); // ---------- or ---------- $("#toastContainer").dxToast("toggle", true); } }); });
With Angular, AngularJS, or Knockout, use a different technique. Bind the visible property of the Toast widget to a component property (in Angular), a scope property (in AngularJS), or an observable variable (in Knockout). After that, change this property or variable, and the Toast will appear or disappear.
Angular
<dx-toast [(visible)]="isVisible" type="error" message="Connection problem"> </dx-toast> <dx-button text="Show the Toast" (onClick)="isVisible = true"> </dx-button>
import { DxToastModule, DxButtonModule } from "devextreme-angular"; // ... export class AppComponent { isVisible: boolean = false; } @NgModule({ imports: [ DxButtonModule, DxToastModule, // ... ], // ... })
AngularJS
<div ng-controller="DemoController"> <div dx-toast="{ message: 'Connection problem', type: 'error', bindingOptions: { visible: 'isToastVisible' } }"></div> <div dx-button="{ text: 'Show the Toast', onClick: showToast }"></div> </div>
angular.module('DemoApp', ['dx']) .controller('DemoController', function DemoController($scope) { $scope.isToastVisible = false; $scope.showToast = function () { $scope.isToastVisible = true; } });
Knockout
<div data-bind="dxToast: { message: 'Connection problem', type: 'error', visible: isToastVisible }"></div> <div data-bind="dxButton: { text: 'Show the Toast', onClick: function (e) { e.model.isToastVisible(true); } }"></div>
var viewModel = { isToastVisible: ko.observable(false) }; ko.applyBindings(viewModel);
User Interaction
Because the Toast is supposed to notify a user when something happens, it cannot be invoked from the UI. However, it may be hidden from the UI in many different ways. To decide which of them are available to the user, specify the following options.
Option | Description |
---|---|
closeOnClick | Hides the Toast when a user clicks/presses it. |
closeOnOutsideClick | Hides the Toast when a user clicks/presses outside of it. |
closeOnSwipe | Hides the Toast when a user swipes it out of the screen. |
Events
To execute certain commands before or after the Toast 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. For example, in the following code, a handler of the hidden event is assigned to the onHidden option. This handler counts down from three replacing the message in the Toast at the same time.
jQuery
$(function() { var counter = 3; $("#toastContainer").dxToast({ message: counter, displayTime: 500, onHidden: function (e) { counter--; if (counter != 0) { e.component.option("message", counter); } else { e.component.option({ message: "Go!", onHidden: undefined, displayTime: 3000, type: "success" }); } e.component.show(); } }); $("#buttonContainer").dxButton({ text: "Start the Countdown", onClick: function () { $("#toastContainer").dxToast("show"); } }); });
Angular
<dx-toast [(visible)]="isVisible" [displayTime]="displayTime" [message]="message" (onHidden)="onHidden($event)" [type]="type"> </dx-toast> <dx-button text="Start the Countdown" (onClick)="isVisible = true"> </dx-button>
import { DxToastModule, DxButtonModule } from "devextreme-angular"; // ... export class AppComponent { isVisible: boolean = false; counter: number = 3; displayTime: number = 500; type = "info"; message: string = this.counter.toString(); onHidden (e) { this.counter--; if (counter != 0) { this.message = this.counter.toString(); } else { this.message = "Go!"; this.onHidden = undefined; this.displayTime = 3000; this.type = "success"; } this.isVisible = true; } } @NgModule({ imports: [ DxButtonModule, DxToastModule, // ... ], // ... })
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.
var hiddenEventHandler1 = function (e) { // First handler of the "hidden" event }; var hiddenEventHandler2 = function (e) { // Second handler of the "hidden" event }; $("#toastContainer").dxToast("instance") .on("hidden", hiddenEventHandler1) .on("hidden", hiddenEventHandler2);
See Also
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- Toast - Customize the Content
- Toast - Resize and Relocate
- Toast Demos
- Toast API Reference
If you have technical questions, please create a support ticket in the DevExpress Support Center.