如何给 toastr.js 通知分配 ID 并根据需要更新它

4
在我的项目中,我需要保持通知一直打开,除非用户点击它,如果在触发通知和用户点击之间有更新,我需要在toast通知上更新值。我没有找到任何关于如何更新通知的参考资料。有人知道吗?我正在使用这个github repo:toastr.js。请建议。
4个回答

6
您可以通过在全局范围内使用toast.options设置timeOut值为0来无限期保持通知的打开状态。
或者,您可以使用toast方法的第三个参数来设置它。
例如: toastr.success("message body", "title", {timeOut:0}) 对于您的第二个问题,您可以在创建时捕获其引用,然后在创建后进行更改来更新现有的通知。
例如:
var myToast = toastr.success("message body", "title", {timeOut:0});
myToast.find(".toast-title").text("new title");
myToast.find(".toast-message").text("new message");

如果用户在你完成操作前将鼠标悬停在弹出提示框上,您可能还希望将extendedTimeOut设置为0,如下所示:

var myToast = toastr.success("message body", "title", {timeOut:0, extendedTimeOut:0});

完成后,您可以通过编程方式隐藏提示:

$(myToast).fadeOut();

能否将超时时间设置为0,同时仍然显示进度条以指示有进展? - Koscik
1
目前还没有显示不确定进度的方法。 - Tim Ferrell
谢谢您的回复,先生。您帮我节省了一些时间。 - Koscik
我们可能还需要注意,如果所需的吐司不可用怎么办。 - Jajikanth pydimarla

3
我假设每个toast都有一个唯一的id。以下代码可以完成这个任务:
var t = toastr.warning("message", "title");
t.attr('id', 'your unique id');

之后,您可以像这样简单地选择每个toastr:
t = $('#id')

1

我需要识别我的toastr,我找到了一个解决方案:

    toastr["error"]("Message", "Alert", {
        own_id: 666,
        onCloseClick: function(a, b) {
            // here you can update notification identified by own_id
            console.log(this.own_id);
        }
    })

1
有一个简单的解决方案,像这样 -
toastr.options.timeOut = 0;

演示代码-

$(function() {

    function Toast(type, css, msg) {
        this.type = type;
        this.css = css;
        this.msg = 'This is positioned in the ' + msg + '. You can also style the icon any way you like.';
    }

    var toasts = [
        new Toast('error', 'toast-bottom-full-width', 'This is positioned in the bottom full width. You can also style the icon any way you like.'),
        new Toast('info', 'toast-top-full-width', 'top full width'),
        new Toast('warning', 'toast-top-left', 'This is positioned in the top left. You can also style the icon any way you like.'),
        new Toast('success', 'toast-top-right', 'top right'),
        new Toast('warning', 'toast-bottom-right', 'bottom right'),
        new Toast('error', 'toast-bottom-left', 'bottom left')
    ];

    toastr.options.positionClass = 'toast-top-full-width';
    toastr.options.extendedTimeOut = 0; //1000;
    toastr.options.timeOut = 0;
    toastr.options.fadeOut = 250;
    toastr.options.fadeIn = 250;

    var i = 0;

    $('#tryMe').click(function () {
        $('#tryMe').prop('disabled', true);
        delayToasts();
    });

    function delayToasts() {
        if (i === toasts.length) { return; }
        var delay = i === 0 ? 0 : 2100;
        window.setTimeout(function () { showToast(); }, delay);

        // re-enable the button        
        if (i === toasts.length-1) {
            window.setTimeout(function () {
                $('#tryMe').prop('disabled', false);
                i = 0;
            }, delay + 1000);
        }
    }

    function showToast() {
        var t = toasts[i];
        toastr.options.positionClass = t.css;
        toastr[t.type](t.msg);
        i++;
        delayToasts();
    }
})
body {
    margin: 5em;
}
li {
    font-size: 18px;
    padding: 4px;
}

#toast-container > .toast {
    background-image: none !important;
}

#toast-container > .toast:before {
    position: fixed;
    font-family: FontAwesome;
    font-size: 24px;
    line-height: 18px;
    float: left;
    color: #FFF;
    padding-right: 0.5em;
    margin: auto 0.5em auto -1.5em;
}        
#toast-container > .toast-warning:before {
    content: "\f003";
}
#toast-container > .toast-error:before {
    content: "\f001";
}
#toast-container > .toast-info:before {
    content: "\f005";
}
#toast-container > .toast-success:before {
    content: "\f002";
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" />
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" />
    <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet" />
    <link href="style.css" rel="stylesheet" />

</head>
    <body>
        <h1>Toastr with FontAwesome Icons</h1>
        <ul class="icons-ul">
            <li><i class="icon-li icon-ok"></i>Embedded icon using the &lt;i&gt; tag</li>
            <li><i class="icon-li icon-ok"></i>Doesn't work with background-image</li>
            <li><i class="icon-li icon-ok"></i>We can use the :before psuedo class</li>
            <li><i class="icon-li icon-ok"></i>Works in IE8+, FireFox 21+, Chrome 26+, Safari 5.1+, most mobile browsers</li>
            <li><i class="icon-li icon-ok"></i>See <a href="http://caniuse.com/#search=before">CanIUse.com</a> for browser support</li>
        </ul>
        <button class="btn btn-primary" id="tryMe">Try Me</button>        
        
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
        <script src="script.js"></script>
    </body>
</html>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接