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.

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

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现