在CodeIgniter视图中使用jQuery noty进行flashdata(页面重新加载)


Using jQuery noty within CodeIgniter view for flashdata (page reload)

我一直在寻找很长时间,但无济于事。我正在尝试使用 jQuery noty 来提供失败通知(而不是我一直使用的标准引导通知)。我一辈子都无法理解这应该如何工作。我希望根据闪存数据中存在错误来显示通知...

我仍然处于JavaScript/jQuery的学习阶段,所以这可能是一个愚蠢的新手错误,我将不胜感激可以提供的任何帮助!

控制器:

elseif ($usr_result['status'] == FALSE) {
    $this->session->set_flashdata('flashError', 'User not active, please contact your administrator');
    redirect('account/login');
}

视图:

<?if($this->session->flashdata('flashError')):?>
    <script type="text/javascript">
        $.noty.defaults.killer = true;
        noty({
            text: '<?=$this->session->flashdata('flashError')?>',
            layout: 'top',
            type: 'error',
            closeButton: ['true']});
     </script>
<?endif?>

我已经用按钮测试了noty:

<button class="btn btn-primary noty" data-noty-options='{"text":"This is an alert","layout":"top","type":"alert","closeButton":"true"}'><i class="halflings-icon white bell"></i> Bottom Full Width with Close Button</button>

它工作正常(所以jQuery和noty都正确加载)

我不确定这是否是实现它的最佳方式,但是在研究了 JavaScript 之后(关于 noty 的文档远远不够,对不是 JavaScript 专业人士的人来说几乎没有帮助),在我看来,我最终这样做是为了让它工作:

<?if($this->session->flashdata('flashError')):?>
<script type="text/javascript">
    function notify() {
        noty({
            text: '<?=$this->session->flashdata('flashError')?>',
            layout: 'top',
            type: 'error',
            closeButton: true,
            timeout: false
        });
    }
    window.onload = notify;
</script>
<?endif?>