我正在使用CActiveDataProvider来填充显示用户之间消息的网页。我有一个视图php文件,它使用'zii.widgets.CListView'和CActiveDataProvider。
我正在使用部分_item.php文件来呈现每条消息。事情是当前每条消息在每条消息上方呈现实线,由_item.php文件指定。
<hr style="border-bottom:solid 1px #efefef; border-top:solid 0px #fff;" />
仅当之前显示的消息来自不同用户时,我才希望显示此行。我的理由是,要做到这一点,我需要能够从数据提供者那里获取有关上一项(或者,下一项)的信息。我该如何实现此目的?
它看起来像:
用户 1:福巴等等等等
用户 2: asdlkfj;ajd
User 2: aljs;dfjlkjk
我希望它看起来像什么:
用户 1:福巴等等等等
用户 2: asdlkfj;ajd
User 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),
));
我假设您的消息历史记录具有这样的顺序
user1: Hi Pete!
--------------------------------
user2: Hi Michael!
user2: Do you think about our plan yet?
--------------------------------
user1: Yes, I do.
消息表中的那些记录如下所示
-----------------------------------------------------------
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 } ?>
以上是如何使其工作的一种方法,它不能完全匹配您的代码。