【问题标题】:Yii CActiveDataProvider different _item view based on item attributesYii CActiveDataProvider 基于item属性的不同_item视图
【发布时间】:2013-11-04 00:17:41
【问题描述】:

我正在使用 CActiveDataProvider 填充显示用户之间消息的网页。我有一个使用 'zii.widgets.CListView' 和 CActiveDataProvider 的视图 php 文件。

我正在使用部分 _item.php 文件来呈现每条单独的消息。事情是当前每条消息在每条消息上方用实线呈现,由 _item.php 文件指定。

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

我只想在之前显示的消息来自其他用户时才显示此行。我认为要做到这一点,我需要能够从数据提供者那里获取有关前一项(或下一项)的信息。我该如何做到这一点?

外观:


用户 1:foobar blah blah


用户 2:asdlkfj;ajd


用户 2:aljs;dfjlkjk

我想要的样子:


用户 1:foobar blah blah


用户 2:asdlkfj;ajd

用户 2:aljs;dfjlkjk

这是我的控制器的样子:

$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),
               ));  

【问题讨论】:

    标签: php view yii cactivedataprovider


    【解决方案1】:

    我假设你的消息历史有这样的顺序

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

    Message 表中的那些记录看起来像

    -----------------------------------------------------------
    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
    

    将一个属性$show_line 添加到您的Message 模型中,不要忘记将其设置为safe 属性

    $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,
        ),
    ));
    

    DataProvider 设置到您的列表视图中 然后在您的项目视图中,您应捕获布尔 show_line 以显示或隐藏 hr

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

    以上是使其工作的一种方法,它无法与您的代码完全匹配。

    【讨论】:

    • 感谢您的回复!抱歉,我是 Yii 的新手,所以我有几个问题。这是否需要我更改数据库架构以包含 $show_line 的列?
    • 另请参阅上面关于我如何启动数据提供者的编辑。你能评论一下我将如何修改我的控制器吗?
    • 不,你不需要在你的数据库模式中添加更多的列,你只需在包含它的模型的规则中将它设置为“安全”,它可以是 array('show_line', '安全');
    • 如果你不知道如何使用CArrayDataProvider,这里是yiiframework.com/doc/api/1.1/CArrayDataProvider
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多