Search results

Prevent duplicate toast display

You can prevent identical same toast displaying in a screen by the event function and terminate the toast displaying process by setting the cancel event property in the beforeOpen event.

The following sample demonstrates preventing duplicate title toast element displaying with custom code blocks.

tagHelper
controller.cs
<div class="control-section" style="width:400px;margin:0 auto;">
    <ejs-toast id="element" title="Sample Toast Title" content="Sample Toast content" beforeOpen="onBeforeOpen">
        <e-toast-position X="Center"></e-toast-position>
    </ejs-toast>
    <ejs-button id="showToast" content="Show Toast" cssClass="e-btn"></ejs-button>
</div>
<script type="text/javascript">
    var toasts = [
        { title: 'Warning !', content: 'There was a problem with your network connection.' },
        { title: 'Success !', content: 'Your message has been sent successfully.' },
        { title: 'Error !', content: 'A problem has been occurred while submitting your data.' },
    ];
    var prevDuplicates = false;
    var toastFlag = 0;
    setTimeout(
        () => {
            var toastObj = document.getElementById('element').ej2_instances[0];
            toastObj.target = document.body;
            toastObj.show(toasts[toastFlag]);
            ++toastFlag;
        }, 1000);
    function onBeforeOpen(e) {
        e.cancel = preventDuplicate(e);
    }
    function preventDuplicate(e) {
        var toastEle = e.element;
        var toasts = e.toastObj.element.children;
        for (var i = 0; i < toasts.length; i++) {
            if ((toasts[i]).querySelector('.e-toast-title').isEqualNode(toastEle.querySelector('.e-toast-title'))) {
                return true;
            }
        }
        return false;
    }
    document.getElementById("showToast").addEventListener('click', function () {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.show(toasts[toastFlag]);
        ++toastFlag;
        if (toastFlag === (toasts.length)) {
            toastFlag = 0;
        }
    });
</script>
public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }
}