2013-12-18 14 views
1

我想在yii中实现一个简单的收件箱。它从数据库表读取消息并显示它。在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)); 
    } 
} 
+0

你好看吗?你的数据库是怎样的?如果你的数据库知道消息何时被读取,你能不能简单地在视图中做一个简单的检查,如if($ model-> read){//改变颜色} else {//不改变颜色}或者类似的东西? – Jeroen

+0

我的数据库有messages.in这个表我存储发件人和接收者ID,标题,消息文本和一个字段的读取/未读,当消息被读取时为1。我如何以不同的方式显示未读消息以及未读消息如何在控制器中成为读取消息?我还没有任何视图 – user3019375

+0

已添加答案。对于原始问题和“未读消息如何在控制器中读取消息?”。但是我不这样做在控制器中。要在控制器(数据库中的意思是?)中执行此操作,只需将读取更新为1,同时从数据库中获取消息。 – Jeroen

回答

0

首先所有的脚本事情应该在视图中。对于你的问题,我会做类似

在控制器

$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; ?> 

因此,现在将增加阅读的每一个消息类已阅读。然后在CSS中,你可以简单地改变它的风格。我希望这是足够的信息?我使用foreach():endforeach; if():endif;在视图文件中,但你可以使用foreach(){},但我更喜欢foreach,因为它看起来更好地结合HTML。

编辑关于你的第二个问题,他们如何阅读。你可以用JQUERY做这件事。例。

$(".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 
     } 
    } 
}); 

这只是给读取的类的div,它应该看起来像读取类的其他项目。