Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在yii中实现一个简单的带有通知的收件箱_Yii_Notifications_Inbox - Fatal编程技术网

在yii中实现一个简单的带有通知的收件箱

在yii中实现一个简单的带有通知的收件箱,yii,notifications,inbox,Yii,Notifications,Inbox,我想用yii实现一个简单的收件箱。它从数据库表中读取消息并显示 但我不知道如何以不同的样式显示已读和未读消息,以及如何实现新消息的通知 我搜索了很多,但只找到了一些扩展,我不想使用它们 找到如何以不同的方式显示未读邮件是非常重要的 任何最初的想法都会对我有帮助 邮箱扩展代码的一部分: public function actionInbox($ajax=null) { $this->module->registerConfig($this->getAction()->

我想用yii实现一个简单的收件箱。它从数据库表中读取消息并显示

但我不知道如何以不同的样式显示已读和未读消息,以及如何实现新消息的通知

我搜索了很多,但只找到了一些扩展,我不想使用它们

找到如何以不同的方式显示未读邮件是非常重要的

任何最初的想法都会对我有帮助 邮箱扩展代码的一部分:

public function actionInbox($ajax=null)
{
    $this->module->registerConfig($this->getAction()->getId());
    $cs =& $this->module->getClientScript();
    $cs->registerScriptFile($this->module->getAssetsUrl().'/js/mailbox.js',CClientScript::POS_END);
    //$js = '$("#mailbox-list").yiiMailboxList('.$this->module->getOptions().');console.log(1)';

    //$cs->registerScript('mailbox-js',$js,CClientScript::POS_READY);


    if(isset($_POST['convs']))
    {
        $this->buttonAction('inbox');
    }
    $dataProvider = new CActiveDataProvider( Mailbox::model()->inbox($this->module->getUserId()) );
    if(isset($ajax))
        $this->renderPartial('_mailbox',array('dataProvider'=>$dataProvider));
    else{
        if(!isset($_GET['Mailbox_sort']))
            $_GET['Mailbox_sort'] = 'modified.desc';

        $this->render('mailbox',array('dataProvider'=>$dataProvider));
    }
}

首先,脚本应该在视图中。对于你的问题,我会这样做

在控制器中

$mailbox = Mailbox::model()->inbox($this->module->getUserId()); //I assume this returns the mailbox from that user? 

$this->renderPartial('_mailbox',compact('mailbox ')); //compact is the same as array('mailbox'=>$mailbox) so use whatever you prefer.
在我看来,我只会这样做

<?php foreach($mailbox->messages as $message):
     $class = ''; //order unread if you want to give both a different class name 
     if($message->read): //if this is true 
           $class = 'read'; 
     endif; ?>
     <div id='<?= $message->id ?>'class='message $class'> <!-- insert whatever info from the message --></div>
<?php endforeach; ?>

这只是让div读取类,它应该与读取类的其他项目一样。

您如何查看外观?您的数据库是什么样子的?如果你的数据库知道消息是什么时候被读取的,如果它被读取了,你能不能在视图中做一个简单的检查,比如($model->read){//change color}或者{//don change color}或者类似的事情?我的数据库有一个消息表。在这个表中,我存储发送者和接收者id、标题,消息文本和已读/未读字段,读取消息时为1。我如何以不同的方式显示未读消息,以及未读消息如何在控制器中变为已读消息?我没有任何意见,只是回答了一个问题。对于原始问题和问题“控制器中未读消息如何变成已读消息?”。但是,我在控制器中不这样做。要在控制器(您指的是数据库?)中执行此操作,只需在从数据库获取消息时将read更新为1即可。
$(".message").on("click", function() {
    var id = $(this).attr('id');
    $.ajax {
        type:"POST",
        url: "controller/action/"+id; //the controller action that fetches the message, the Id is the action variable (ex: public function actionGetMessage($id) {})
        completed: function(data) {
            //data = the message information, you might do type: 'JSON' instead. Use it however you want it. 
            if(!$(this).hasClass("read"))
                $(this).addClass("read"); //give it the class read if it does not have it already
        }
    }
});