dpyoh6553 2013-11-04 00:17
浏览 26
已采纳

Yii CActiveDataProvider基于项属性的不同_item视图

I'm using CActiveDataProvider to populate a webpage showing messages between users. I have a view php file that uses 'zii.widgets.CListView' in conjunction with the CActiveDataProvider.

I'm using a partial _item.php file to render each individual message. The thing is currently each message renders with solid line above each message, per specified by the _item.php file.

<hr style="border-bottom:solid 1px #efefef; border-top:solid 0px #fff;" />

I want to show this line only when the message being displayed previously is from a different user. I reason that to do this I need to be able to get information from the dataprovider about the previous item (or alternatively, the following item). How do I accomplish this?

What it looks like:


user 1: foobar blah blah


user 2: asdlkfj;ajd


user 2: aljs;dfjlkjk

What I want it to look like:


user 1: foobar blah blah


user 2: asdlkfj;ajd

user 2: aljs;dfjlkjk

This is what my controller looks like:

$dataProvider = new CActiveDataProvider('MailboxMessage', array(
                    'criteria' => array(
                    'condition' => 'conversation_id=:cid',
            'params' => array(
            ':cid' => $_GET['id']
            ),
                ),
        'sort' => array(
        'defaultOrder' => 'created DESC' // this is it.
         ),
                'pagination' => array('pageSize' =>20),
               ));  
  • 写回答

1条回答 默认 最新

  • dongqiangou5724 2013-11-04 04:54
    关注

    I assume your message history that have order like this

    user1: Hi Pete!
    --------------------------------
    user2: Hi Michael!
    user2: Do you think about our plan yet?
    --------------------------------
    user1: Yes, I do.
    

    Those records in the Message table look like

    -----------------------------------------------------------
    msg_id  | msg                                | user_id (FK) 
    -----------------------------------------------------------    
    12004     Hi Pete!                             1
    12005     Hi Michael!                          2
    12006     Do you think about our plan yet?     2
    12007     Yes, I do.                           1
    

    Add one property $show_line into your Message model and don't forget to make it as safe attribute

    $list_msg = Message:model->findAll(); // could be changed by your way to fetch all of messages & sort them by order of message
    
    if(count($list_msg)>2){
    for($i=0; $i<count($list_msg);$i++){
        if($i < count($list_msg)-1){
          //check if owner of current message item is owner of next message also
          $list_msg[$i]->show_line = $list_msg[$i]->user_id == $list_msg[$i+1]->user_id; // user_id in my case is FK on Message table. I am not sure what it was in your db but you can customize it to appropriately
        }
      }
    }
    
    //$dataProvider =  new CArrayDataProvider('Message');
    //$dataProvider->setData($list_msg);
    
    $dataProvider=new CArrayDataProvider($list_msg, array(
        'id'=>'msg',
        'sort'=>array(
            .....
        ),
        'pagination'=>array(
            'pageSize'=>20,
        ),
    ));
    

    Set that DataProvider into your list view Then in your item view, you shall capture the boolean show_line to display or hide the hr line

    <?php if($data->show_line) {?> <hr .../> <?php } ?>
    

    Above is one way how to make it work, it couldn't match your code exactly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效