【发布时间】:2014-02-12 07:39:07
【问题描述】:
我正在尝试让打印机在按钮上打印我的 CGridView。
我的视图
<p align="right">
<?php
echo CHtml::link('New Job',array('create'),array('class'=>'btn btn-info'));
echo CHtml::link('Print',array('print'),array('class'=>'btnPrint btn btn-info', 'style'=>'margin-left: 10px;'));
?>
</p>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'job-grid',
'dataProvider'=>$jobs->search(),
'filter'=>$jobs,
...)); ?>
我的控制器
public function actionPrint() {
$d = $_SESSION['all'];
$this->renderPartial('print',array('d'=>$d),false,true);
}
我的模型
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('date',$this->date,true);
$criteria->compare('department_id',$this->department_id);
$criteria->compare('section_id',$this->section_id);
$criteria->compare('team_id',$this->team_id);
$criteria->compare('created_date',$this->created_date,true);
$criteria->compare('updated_date',$this->updated_date,true);
$data = new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'pagination'=>false,
));
$_SESSION['all'] = $data;
$data = new CActiveDataProvider(get_class($this), array(
'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
Yii::app()->params['defaultPageSize']),),
'criteria'=>$criteria,
));
$_SESSION['limited'] = $data;
return $data;
}
我的观点 (print.php)
<div id="summary">
<table width="100%" border="1">
<tr>
<th>No.</th>
<th>Job No.</th>
<th>Date</th>
<th>Department</th>
<th>Section</th>
<th>Team</th>
</tr>
<?php
$i=1;
foreach($d->data as $item)
{
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->date; ?></td>
<td><?php echo $item->departmentsdep->name; ?></td>
<td><?php echo $item->departmentssec->name; ?></td>
<td><?php echo $item->departmentstea->name; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
但是使用上面的代码我收到一个未定义索引的错误:所有
这将如何运作。
【问题讨论】:
-
你在哪里声明了 $_SESSION['all']?您正在模型中使用它,但尚未定义它。
-
Yii::app()->session['all'].添加这条线怎么样?你试过这个吗?
-
类似地你可以像 $d=Yii::app()->session['var'] 一样使用它
-
@RafayZiaMir 它有效,但没有我正在尝试为 foreach 获取非对象的属性。
-
bcz "->" 用于访问对象的属性而不是数组。因为 data 是一个数组索引,所以你可以访问 Yiiapp()->session['all']['data']。如果这给出错误然后尝试调试会话。编写 CVarDumper::Dump(Yiiapp()->session['all'],100,true), die(), 它将显示会话数组的内容。然后发布你得到的结果(在你的问题中)
标签: php printing yii cgridview